Java Code Examples for java.sql.PreparedStatement.setShort()

The following are Jave code examples for showing how to use setShort() of the java.sql.PreparedStatement class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: mycat-src-1.6.1-RELEASE   File: GoodsInsertJob.java   Source Code and License Vote up 8 votes
private int insert(Connection con,List<Map<String, String>> list) throws SQLException {
	PreparedStatement ps;
	String sql = "insert into goods (id,name ,good_type,good_img_url,good_created ,good_desc, price ) values(?,? ,?,?,? ,?, ?)";
	ps = con.prepareStatement(sql);
	for (Map<String, String> map : list) {
		ps.setLong(1, Long.parseLong(map.get("id")));
		ps.setString(2, (String) map.get("name"));
		ps.setShort(3, Short.parseShort(map.get("good_type")));
		ps.setString(4, (String) map.get("good_img_url"));
		ps.setString(5, (String) map.get("good_created"));
		ps.setString(6, (String) map.get("good_desc"));
		ps.setDouble(7, Double.parseDouble(map.get("price")));
		ps.addBatch();
	}
	ps.executeBatch();
	return list.size();
}
 
Example 2
Project: morpheus-core   File: SQL.java   Source Code and License Vote up 7 votes
/**
 * Binds arguments to prepared statement
 * @param stmt  the prepared statement reference
 * @return      the same as arg
 * @throws SQLException if binding fails
 */
private PreparedStatement bindArgs(PreparedStatement stmt) throws SQLException {
    for (int i=0; i<args.length; ++i) {
        final Object value = args[i];
        if      (value instanceof Boolean)          stmt.setBoolean(i+1, (Boolean)value);
        else if (value instanceof Short)            stmt.setShort(i+1, (Short)value);
        else if (value instanceof Integer)          stmt.setInt(i+1, (Integer)value);
        else if (value instanceof Float)            stmt.setFloat(i+1, (Float)value);
        else if (value instanceof Long)             stmt.setLong(i+1, (Long)value);
        else if (value instanceof Double)           stmt.setDouble(i+1, (Double)value);
        else if (value instanceof String)           stmt.setString(i+1, (String)value);
        else if (value instanceof java.sql.Date)    stmt.setDate(i+1, (java.sql.Date)value);
        else if (value instanceof Timestamp)        stmt.setTimestamp(i+1, (Timestamp)value);
        else if (value instanceof LocalDate)        stmt.setDate(i + 1, java.sql.Date.valueOf((LocalDate)value));
        else if (value instanceof LocalTime)        stmt.setTime(i+1, Time.valueOf((LocalTime)value));
        else if (value instanceof LocalDateTime)    stmt.setTimestamp(i+1, Timestamp.valueOf((LocalDateTime)value));
        else if (value instanceof ZonedDateTime) {
            final ZonedDateTime zonedDateTime = (ZonedDateTime)value;
            final LocalDateTime dateTime = zonedDateTime.toLocalDateTime();
            stmt.setTimestamp(i+1, Timestamp.valueOf(dateTime));
        } else {
            throw new RuntimeException("Unsupported argument, cannot be bound to SQL statement: " + value);
        }
    }
    return stmt;
}
 
Example 3
Project: dble   File: GoodsInsertJob.java   Source Code and License Vote up 6 votes
private int insert(Connection con, List<Map<String, String>> list) throws SQLException {
    PreparedStatement ps;
    String sql = "insert into goods (id,name ,good_type,good_img_url,good_created ,good_desc, price ) values(?,? ,?,?,? ,?, ?)";
    ps = con.prepareStatement(sql);
    for (Map<String, String> map : list) {
        ps.setLong(1, Long.parseLong(map.get("id")));
        ps.setString(2, (String) map.get("name"));
        ps.setShort(3, Short.parseShort(map.get("good_type")));
        ps.setString(4, (String) map.get("good_img_url"));
        ps.setString(5, (String) map.get("good_created"));
        ps.setString(6, (String) map.get("good_desc"));
        ps.setDouble(7, Double.parseDouble(map.get("price")));
        ps.addBatch();
    }
    ps.executeBatch();
    return list.size();
}
 
Example 4
Project: lams   File: SmallIntTypeDescriptor.java   Source Code and License Vote up 5 votes
@Override
public <X> ValueBinder<X> getBinder(final JavaTypeDescriptor<X> javaTypeDescriptor) {
	return new BasicBinder<X>( javaTypeDescriptor, this ) {
		@Override
		protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException {
			st.setShort( index, javaTypeDescriptor.unwrap( value, Short.class, options ) );
		}
	};
}
 
Example 5
Project: dev-courses   File: TestOdbcTypes.java   Source Code and License Vote up 5 votes
public void testSmallIntComplex() {
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        ps = netConn.prepareStatement(
            "INSERT INTO alltypes(id, si) VALUES(?, ?)");
        ps.setInt(1, 3);
        ps.setShort(2, (short) 395);
        assertEquals(1, ps.executeUpdate());
        ps.setInt(1, 4);
        assertEquals(1, ps.executeUpdate());
        ps.close();
        netConn.commit();
        ps = netConn.prepareStatement(
            "SELECT * FROM alltypes WHERE si = ?");
        ps.setShort(1, (short) 395);
        rs = ps.executeQuery();
        assertTrue("Got no rows with si = 395", rs.next());
        assertEquals(Integer.class, rs.getObject("si").getClass());
        // Nb. HyperSQL purposefully returns an Integer for this type
        assertTrue("Got only one row with si = 395", rs.next());
        assertEquals((short) 395, rs.getShort("si"));
        assertFalse("Got too many rows with si = 395", rs.next());
    } catch (SQLException se) {
        junit.framework.AssertionFailedError ase
            = new junit.framework.AssertionFailedError(se.getMessage());
        ase.initCause(se);
        throw ase;
    } finally {
        try {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
        } catch(Exception e) {
        }
    }
}
 
Example 6
Project: dacapobench   File: Operation.java   Source Code and License Vote up 4 votes
/**
 * Payment by customer last name. Section 2.5.2 The CUSTOMER row will be
 * fetched and then updated. This is due to the need to select the specific
 * customer first based upon last name (which will actually fetch and hence
 * lock a number of customers).
 */
public void payment(Display display, Object displayData, short w, short d, short cw, short cd, String customerLast, String amount) throws Exception {

  PreparedStatement pyCustomerByName = prepareStatement("SELECT C_ID " + "FROM CUSTOMER WHERE C_W_ID = ? AND C_D_ID = ? AND C_LAST = ? " + "ORDER BY C_FIRST");

  // Since so much data is needed for the payment transaction
  // from the customer we don't fill it in as we select the
  // correct customer. Instead we just fetch the identifier
  // and then execute a payment by identifier.
  try {
    pyCustomerByName.setShort(1, cw);
    pyCustomerByName.setShort(2, cd);
    pyCustomerByName.setString(3, customerLast);
    ResultSet rs = pyCustomerByName.executeQuery();

    nameList.clear();
    while (rs.next()) {
      nameList.add(rs.getObject("C_ID"));
    }
    reset(pyCustomerByName);
    if (nameList.isEmpty())
      throw new SQLException("Payment by name - no matching customer " + customerLast);

    // Customer to use is midpoint (with round up) (see 2.5.2.2)
    int mid = nameList.size() / 2;
    if (mid != 0) {
      if (nameList.size() % 2 == 1)
        mid++;
    }

    int c = ((Integer) nameList.get(mid)).intValue();

    paymentById(display, displayData, w, d, cw, cd, c, amount);
  } catch (SQLException e) {
    conn.rollback();
    throw e;
  }

  if (display != null)
    ;
}
 
Example 7
Project: tangyuan2   File: ShortTypeHandler.java   Source Code and License Vote up 4 votes
@Override
public void setNonNullParameter(PreparedStatement ps, int i, Short parameter, JdbcType jdbcType) throws SQLException {
	ps.setShort(i, parameter);
}
 
Example 8
Project: jsf-core   File: ConvertableEnumTypeHandler.java   Source Code and License Vote up 4 votes
@Override
protected void setValue(PreparedStatement ps, int i, Short value)
        throws SQLException {
    ps.setShort(i, value);
}
 
Example 9
Project: sqlify   File: Sqlify.java   Source Code and License Vote up 4 votes
private PreparedStatement applyParameterMapToPreparedStatement(
    PreparedStatement preparedStatement, 
    Map<String, Object> parameterMap, 
    List<String> parametersInSqlSorted) {
  try {
    for (int i = 0; i < parametersInSqlSorted.size(); i++) {
      Object value = parameterMap.get(parametersInSqlSorted.get(i));
      int positionInPreparedStatement = i + 1; // jdbc parameters start with 1...
      
      if (value instanceof BigDecimal) {
        preparedStatement.setBigDecimal(positionInPreparedStatement, (BigDecimal) value);
      } else if (value instanceof Boolean) {
        preparedStatement.setBoolean(positionInPreparedStatement, (Boolean) value);
      } else if (value instanceof Date) {
        preparedStatement.setDate(positionInPreparedStatement, (Date) value);
      } else if (value instanceof Double) {
        preparedStatement.setDouble(positionInPreparedStatement, (Double) value);
      } else if (value instanceof Float) {
        preparedStatement.setFloat(positionInPreparedStatement, (Float) value);
      } else if (value instanceof Integer) {
        preparedStatement.setInt(positionInPreparedStatement, (Integer) value);
      } else if (value instanceof Long) {
        preparedStatement.setLong(positionInPreparedStatement, (Long) value);
      } else if (value instanceof Short) {
        preparedStatement.setShort(positionInPreparedStatement, (Short) value);
      } else if (value instanceof String) {
        preparedStatement.setString(positionInPreparedStatement, (String) value);
      } else if (value instanceof Time) {
        preparedStatement.setTime(positionInPreparedStatement, (Time) value);
      } else if (value instanceof Timestamp) {
        preparedStatement.setTimestamp(positionInPreparedStatement, (Timestamp) value);
      } else if (value instanceof URL) {
        preparedStatement.setURL(positionInPreparedStatement, (URL) value);
      } else {
        // Kind of a fallback. If you expect some other behavior feel
        // free to implement it.
        preparedStatement.setObject(positionInPreparedStatement, value);
      }
    }
  } catch (SQLException ex) {
    throw new SqlifyException("Ops. An error occurred.", ex);
  }
  return preparedStatement;
}
 
Example 10
Project: Lucid2.0   File: PlayerNPC.java   Source Code and License Vote up 4 votes
public void saveToDB() {
    Connection con = DatabaseConnection.getConnection();
    try {

        if (getNPCFromWZ() == null) {
            destroy(true);
            return;
        }
        destroy();
        PreparedStatement ps = con.prepareStatement("INSERT INTO playernpcs(name, hair, face, skin, x, y, map, charid, scriptid, foothold, dir, gender, pets, job, elf, faceMarking) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
        int k = 0;
        ps.setString(++k, getName());
        ps.setInt(++k, getHair());
        ps.setInt(++k, getFace());
        ps.setInt(++k, getSkinColor());
        ps.setInt(++k, getTruePosition().x);
        ps.setInt(++k, getTruePosition().y);
        ps.setInt(++k, getMapId());
        ps.setInt(++k, getCharId());
        ps.setInt(++k, getId());
        ps.setInt(++k, getFh());
        ps.setInt(++k, getF());
        ps.setInt(++k, getGender());
        String[] pet = {"0", "0", "0"};
        for (int i = 0; i < 3; i++) {
            if (pets[i] > 0) {
                pet[i] = String.valueOf(pets[i]);
            }
        }
        ps.setString(++k, pet[0] + "," + pet[1] + "," + pet[2]);
        ps.setShort(++k, getJob());
        ps.setInt(++k, getElf());
        ps.setInt(++k, getFaceMarking());
        ps.executeUpdate();
        ps.close();

        ps = con.prepareStatement("INSERT INTO playernpcs_equip(npcid, charid, equipid, equippos) VALUES (?, ?, ?, ?)");
        ps.setInt(1, getId());
        ps.setInt(2, getCharId());
        for (Entry<Byte, Integer> equip : equips.entrySet()) {
            ps.setInt(3, equip.getValue());
            ps.setInt(4, equip.getKey());
            ps.executeUpdate();
        }
        ps.close();
    } catch (SQLException se) {
    }
}
 
Example 11
Project: uavstack   File: DAOFactory.java   Source Code and License Vote up 2 votes
public void setParameter(PreparedStatement ps, int i, Object parameter) throws SQLException {

            ps.setShort(i, ((Short) parameter).shortValue());
        }