List of usage examples for java.sql PreparedStatement setFloat
void setFloat(int parameterIndex, float x) throws SQLException;
float
value. From source file:Main.java
public static void main(String[] argv) throws Exception { Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root"); String sql = "INSERT myTable VALUES(?,?,?,?)"; PreparedStatement prest = con.prepareStatement(sql); prest.setString(1, "A"); prest.setInt(2, 5);/*from w ww.j a v a2s .co m*/ prest.setDouble(3, 2.0); prest.setFloat(4, 4.2f); int row = prest.executeUpdate(); System.out.println(row + " row(s) affected)"); }
From source file:Main.java
public static void main(String[] args) throws Exception { String id = "0001"; float floatValue = 0001f; double doubleValue = 1.0001d; Connection conn = null;/*w w w .j av a 2 s.c o m*/ PreparedStatement pstmt = null; try { conn = getConnection(); String query = "insert into double_table(id, float_column, double_column) values(?, ?, ?)"; pstmt = conn.prepareStatement(query); pstmt.setString(1, id); pstmt.setFloat(2, floatValue); pstmt.setDouble(3, doubleValue); // execute query, and return number of rows created int rowCount = pstmt.executeUpdate(); System.out.println("rowCount=" + rowCount); } finally { pstmt.close(); conn.close(); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { String driverName = "com.jnetdirect.jsql.JSQLDriver"; Class.forName(driverName);//from ww w . j av a2s . co m String serverName = "127.0.0.1"; String portNumber = "1433"; String mydatabase = serverName + ":" + portNumber; String url = "jdbc:JSQLConnect://" + mydatabase; String username = "username"; String password = "password"; Connection connection = DriverManager.getConnection(url, username, password); String sql = "INSERT INTO mysql_all_table(" + "col_boolean," + "col_byte," + "col_short," + "col_int," + "col_long," + "col_float," + "col_double," + "col_bigdecimal," + "col_string," + "col_date," + "col_time," + "col_timestamp," + "col_asciistream," + "col_binarystream," + "col_blob) " + "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; PreparedStatement pstmt = connection.prepareStatement(sql); pstmt.setBoolean(1, true); pstmt.setByte(2, (byte) 123); pstmt.setShort(3, (short) 123); pstmt.setInt(4, 123); pstmt.setLong(5, 123L); pstmt.setFloat(6, 1.23F); pstmt.setDouble(7, 1.23D); pstmt.setBigDecimal(8, new BigDecimal(1.23)); pstmt.setString(9, "a string"); pstmt.setDate(10, new java.sql.Date(System.currentTimeMillis())); pstmt.setTime(11, new Time(System.currentTimeMillis())); pstmt.setTimestamp(12, new Timestamp(System.currentTimeMillis())); File file = new File("infilename1"); FileInputStream is = new FileInputStream(file); pstmt.setAsciiStream(13, is, (int) file.length()); file = new File("infilename2"); is = new FileInputStream(file); pstmt.setBinaryStream(14, is, (int) file.length()); file = new File("infilename3"); is = new FileInputStream(file); pstmt.setBinaryStream(15, is, (int) file.length()); pstmt.executeUpdate(); }
From source file:SetSavepoint.java
public static void main(String args[]) { String url = "jdbc:mySubprotocol:myDataSource"; try {//from ww w. j a va 2 s . c o m Class.forName("myDriver.className"); } catch (java.lang.ClassNotFoundException e) { System.err.print("ClassNotFoundException: "); System.err.println(e.getMessage()); } try { Connection con = DriverManager.getConnection(url, "myLogin", "myPassword"); con.setAutoCommit(false); String query = "SELECT COF_NAME, PRICE FROM COFFEES " + "WHERE TOTAL > ?"; String update = "UPDATE COFFEES SET PRICE = ? " + "WHERE COF_NAME = ?"; PreparedStatement getPrice = con.prepareStatement(query); PreparedStatement updatePrice = con.prepareStatement(update); getPrice.setInt(1, 7000); ResultSet rs = getPrice.executeQuery(); Savepoint save1 = con.setSavepoint(); while (rs.next()) { String cof = rs.getString("COF_NAME"); float oldPrice = rs.getFloat("PRICE"); float newPrice = oldPrice + (oldPrice * .05f); updatePrice.setFloat(1, newPrice); updatePrice.setString(2, cof); updatePrice.executeUpdate(); System.out.println("New price of " + cof + " is " + newPrice); if (newPrice > 11.99) { con.rollback(save1); } } getPrice = con.prepareStatement(query); updatePrice = con.prepareStatement(update); getPrice.setInt(1, 8000); rs = getPrice.executeQuery(); System.out.println(); Savepoint save2 = con.setSavepoint(); while (rs.next()) { String cof = rs.getString("COF_NAME"); float oldPrice = rs.getFloat("PRICE"); float newPrice = oldPrice + (oldPrice * .05f); updatePrice.setFloat(1, newPrice); updatePrice.setString(2, cof); updatePrice.executeUpdate(); System.out.println("New price of " + cof + " is " + newPrice); if (newPrice > 11.99) { con.rollback(save2); } } con.commit(); Statement stmt = con.createStatement(); rs = stmt.executeQuery("SELECT COF_NAME, " + "PRICE FROM COFFEES"); System.out.println(); while (rs.next()) { String name = rs.getString("COF_NAME"); float price = rs.getFloat("PRICE"); System.out.println("Current price of " + name + " is " + price); } con.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:AutoGenKeys.java
public static void main(String args[]) { String url = "jdbc:mySubprotocol:myDataSource"; Connection con = null;// w w w. j a v a 2s . c o m PreparedStatement pstmt; String insert = "INSERT INTO COFFEES VALUES ('HYPER_BLEND', " + "101, 10.99, 0, 0)"; String update = "UPDATE COFFEES SET PRICE = ? WHERE KEY = ?"; try { Class.forName("myDriver.ClassName"); } catch (java.lang.ClassNotFoundException e) { System.err.print("ClassNotFoundException: "); System.err.println(e.getMessage()); } try { con = DriverManager.getConnection(url, "myLogin", "myPassword"); pstmt = con.prepareStatement(insert, Statement.RETURN_GENERATED_KEYS); pstmt.executeUpdate(); ResultSet keys = pstmt.getGeneratedKeys(); int count = 0; keys.next(); int key = keys.getInt(1); pstmt = con.prepareStatement(update); pstmt.setFloat(1, 11.99f); pstmt.setInt(2, key); pstmt.executeUpdate(); keys.close(); pstmt.close(); con.close(); } catch (SQLException e) { e.printStackTrace(); } }
From source file:org.bml.util.sql.DBUtil.java
/** * * @param ps/*from w ww . ja va2 s . c o m*/ * @param keyName * @param fieldId * @param map * @param remove * @throws SQLException */ public static void setFloat(PreparedStatement ps, String keyName, int fieldId, Map<String, String> map, Boolean remove) throws SQLException { String val = map.get(keyName); if (remove) { map.remove(keyName); } if (val == null) { ps.setString(fieldId, null); return; } if (val.isEmpty()) { ps.setFloat(fieldId, DEFAULT_FLOAT); } else { if (val.equals("null")) { ps.setFloat(fieldId, DEFAULT_FLOAT); } else { ps.setFloat(fieldId, Float.parseFloat(val)); } } }
From source file:org.wso2.extension.siddhi.store.rdbms.util.RDBMSTableUtils.java
/** * Util method which is used to populate a {@link PreparedStatement} instance with a single element. * * @param stmt the statement to which the element should be set. * @param ordinal the ordinal of the element in the statement (its place in a potential list of places). * @param type the type of the element to be set, adheres to * {@link org.wso2.siddhi.query.api.definition.Attribute.Type}. * @param value the value of the element. * @throws SQLException if there are issues when the element is being set. *//*from www .j a v a 2 s .c o m*/ public static void populateStatementWithSingleElement(PreparedStatement stmt, int ordinal, Attribute.Type type, Object value) throws SQLException { switch (type) { case BOOL: stmt.setBoolean(ordinal, (Boolean) value); break; case DOUBLE: stmt.setDouble(ordinal, (Double) value); break; case FLOAT: stmt.setFloat(ordinal, (Float) value); break; case INT: stmt.setInt(ordinal, (Integer) value); break; case LONG: stmt.setLong(ordinal, (Long) value); break; case OBJECT: stmt.setObject(ordinal, value); break; case STRING: stmt.setString(ordinal, (String) value); break; } }
From source file:dsd.dao.DAOProvider.java
/** * calls the correct method for setting the command parameter depending on * parameter type//from w ww . j a va 2 s . c o m * * @param command * @param object * @param parameterIndex * @throws SQLException */ private static void SetParameter(PreparedStatement command, Object object, int parameterIndex) throws SQLException { if (object instanceof Timestamp) { command.setTimestamp(parameterIndex, (Timestamp) object); } else if (object instanceof String) { command.setString(parameterIndex, (String) object); } else if (object instanceof Long) { command.setLong(parameterIndex, (Long) object); } else if (object instanceof Integer) { command.setInt(parameterIndex, (Integer) object); } else if (object instanceof Boolean) { command.setBoolean(parameterIndex, (Boolean) object); } else if (object instanceof Float) { command.setFloat(parameterIndex, (Float) object); } else { throw new IllegalArgumentException( "type needs to be inserted in Set parameter method of DAOProvider class"); } }
From source file:com.novartis.opensource.yada.util.YADAUtils.java
/** * One-liner execution of a sql statement, returning an SQL {@link java.sql.ResultSet}. * <strong>Note: This method opens a db connection but DOES NOT CLOSE IT. * Use the static method {@link ConnectionFactory#releaseResources(ResultSet)} to close it from * the calling method</strong>//ww w .ja v a 2 s . com * @param sql the query to execute * @param params the data values to map to query columns * @return a {@link java.sql.ResultSet} object containing the result of the query * @throws YADAConnectionException when the datasource is inaccessible * @throws YADASQLException when the JDBC configuration or execution fails */ public static ResultSet executePreparedStatement(String sql, Object[] params) throws YADAConnectionException, YADASQLException { ResultSet rs = null; try { Connection c = ConnectionFactory.getConnectionFactory().getConnection(ConnectionFactory.YADA_APP); PreparedStatement p = c.prepareStatement(sql); for (int i = 1; i <= params.length; i++) { Object param = params[i - 1]; if (param instanceof String) { p.setString(i, (String) param); } else if (param instanceof Date) { p.setDate(i, (Date) param); } else if (param instanceof Integer) { p.setInt(i, ((Integer) param).intValue()); } else if (param instanceof Float) { p.setFloat(i, ((Float) param).floatValue()); } } rs = p.executeQuery(); } catch (SQLException e) { throw new YADASQLException(e.getMessage(), e); } return rs; }
From source file:org.rhq.plugins.database.DatabasePluginUtil.java
private static void bindParameters(PreparedStatement statement, Object... parameters) throws SQLException { int i = 1;// w ww .j a v a2s .c om for (Object p : parameters) { if (p instanceof String) { statement.setString(i++, (String) p); } else if (p instanceof Byte) { statement.setByte(i++, (Byte) p); } else if (p instanceof Short) { statement.setShort(i++, (Short) p); } else if (p instanceof Integer) { statement.setInt(i++, (Integer) p); } else if (p instanceof Long) { statement.setLong(i++, (Long) p); } else if (p instanceof Float) { statement.setFloat(i++, (Float) p); } else if (p instanceof Double) { statement.setDouble(i++, (Double) p); } else { statement.setObject(i++, p); } } }