List of usage examples for java.sql PreparedStatement setDouble
void setDouble(int parameterIndex, double x) throws SQLException;
double
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 ww w.j a v a 2 s.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 url = "jdbc:mysql://localhost/testdb"; String username = "root"; String password = ""; Class.forName("com.mysql.jdbc.Driver"); Connection conn = null;//from ww w. j a va 2 s . c om try { conn = DriverManager.getConnection(url, username, password); conn.setAutoCommit(false); Statement st = conn.createStatement(); st.execute("INSERT INTO orders (username, order_date) VALUES ('java', '2007-12-13')", Statement.RETURN_GENERATED_KEYS); ResultSet keys = st.getGeneratedKeys(); int id = 1; while (keys.next()) { id = keys.getInt(1); } PreparedStatement pst = conn.prepareStatement( "INSERT INTO order_details (order_id, product_id, quantity, price) VALUES (?, ?, ?, ?)"); pst.setInt(1, id); pst.setString(2, "1"); pst.setInt(3, 10); pst.setDouble(4, 100); pst.execute(); conn.commit(); System.out.println("Transaction commit..."); } catch (SQLException e) { if (conn != null) { conn.rollback(); System.out.println("Connection rollback..."); } e.printStackTrace(); } finally { if (conn != null && !conn.isClosed()) { conn.close(); } } }
From source file:Main.java
public static void main(String[] argv) throws Exception { String driverName = "com.jnetdirect.jsql.JSQLDriver"; Class.forName(driverName);// ww w .j a v a 2s .c o 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:Main.java
public static void main(String[] args) throws Exception { String id = "0001"; float floatValue = 0001f; double doubleValue = 1.0001d; Connection conn = null;//from w w w. j av a2 s. co 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:writetodbfromfile.WriteToDBFromFile.java
/** * @param args the command line arguments *//*w w w . j a va 2 s. c o m*/ public static void main(String[] args) throws SQLException { BufferedReader br = null; Connection con = getConnection(); PreparedStatement stmt = null; int lines = 0; int nonzero = 0; int linesProcessed = 0; try { String sCurrentLine; stmt = con.prepareStatement("INSERT INTO SIMRANK (DOC1, DOC2, SIMSCORE) VALUES (?,?,?)"); br = new BufferedReader(new FileReader("D:\\CS570Project\\Test\\1500 File SimRank\\SimRank1.txt")); while ((sCurrentLine = br.readLine()) != null) { lines++; linesProcessed++; if (lines < 30000) { String[] arr = sCurrentLine.split(" "); double value = Double.parseDouble(arr[2]); if (value > 0) { nonzero++; stmt.setInt(1, Integer.parseInt(arr[0])); stmt.setInt(2, Integer.parseInt(arr[1])); stmt.setDouble(3, value); stmt.addBatch(); } } else { lines = 0; System.out.println("Lines Processed ... " + linesProcessed); System.out.println("Nonzero recors ... " + nonzero); stmt.executeBatch(); con.close(); con = getConnection(); stmt = con.prepareStatement("INSERT INTO SIMRANK (DOC1, DOC2, SIMSCORE) VALUES (?,?,?)"); } } } catch (Exception e) { e.printStackTrace(); } finally { try { stmt.close(); con.close(); if (br != null) br.close(); } catch (IOException ex) { ex.printStackTrace(); } } }
From source file:com.l2jfree.sql.L2DatabaseInstaller.java
private static void insertRevision(double revision) { System.out.println("Saving revision '" + revision + "'."); Connection con = null;// w w w .j a v a 2 s. c o m try { con = L2Database.getConnection(); PreparedStatement ps = con.prepareStatement("INSERT INTO _revision VALUES (?,?)"); ps.setDouble(1, revision); ps.setLong(2, System.currentTimeMillis()); ps.executeUpdate(); ps.close(); } catch (SQLException e) { e.printStackTrace(); } finally { L2Database.close(con); } System.out.println("Done."); }
From source file:org.rhq.plugins.database.DatabaseQueryUtility.java
private static void bindParameters(PreparedStatement statement, Object... parameters) throws SQLException { int i = 1;/*from w ww . j a v a 2s . c om*/ for (Object p : parameters) { if (p instanceof String) { statement.setString(i++, (String) p); } else if (p instanceof Number) { statement.setDouble(i++, ((Number) p).doubleValue()); } else { statement.setObject(i++, p); } } }
From source file:Main.java
public static PreparedStatement createLayersInsertForContextual(Connection conn, int layerId, String description, String path, String name, String displayPath, double minLatitude, double minLongitude, double maxLatitude, double maxLongitude, String path_orig) throws SQLException { PreparedStatement stLayersInsert = conn.prepareStatement( "INSERT INTO layers (id, name, description, type, path, displayPath, minlatitude, minlongitude, maxlatitude, maxlongitude, enabled, displayname, uid, path_orig) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"); stLayersInsert.setInt(1, layerId);/* w w w. ja v a 2 s. c o m*/ stLayersInsert.setString(2, name); stLayersInsert.setString(3, description); stLayersInsert.setString(4, CONTEXTUAL_LAYER_TYPE); stLayersInsert.setString(5, path); stLayersInsert.setString(6, displayPath); stLayersInsert.setDouble(7, minLatitude); stLayersInsert.setDouble(8, minLongitude); stLayersInsert.setDouble(9, maxLatitude); stLayersInsert.setDouble(10, maxLongitude); stLayersInsert.setBoolean(11, true); stLayersInsert.setString(12, description); stLayersInsert.setString(13, Integer.toString(layerId)); stLayersInsert.setString(14, path_orig); return stLayersInsert; }
From source file:ca.qc.adinfo.rouge.achievement.db.AchievementDb.java
public static boolean createAchievement(DBManager dbManager, String key, String name, int pointValue, double total) { Connection connection = null; PreparedStatement stmt = null; ResultSet rs = null;//from w w w. j av a 2 s. co m String sql = null; sql = "INSERT INTO rouge_achievements (`key`, `name`, `point_value`, `total`) " + " VALUES (?, ?, ?, ?);"; try { connection = dbManager.getConnection(); stmt = connection.prepareStatement(sql); stmt.setString(1, key); stmt.setString(2, name); stmt.setInt(3, pointValue); stmt.setDouble(4, total); int ret = stmt.executeUpdate(); return (ret > 0); } catch (SQLException e) { log.error(stmt); log.error(e); return false; } finally { DbUtils.closeQuietly(rs); DbUtils.closeQuietly(stmt); DbUtils.closeQuietly(connection); } }
From source file:ca.qc.adinfo.rouge.achievement.db.AchievementDb.java
public static boolean updateAchievement(DBManager dbManager, String key, long userId, double progress) { Connection connection = null; PreparedStatement stmt = null; ResultSet rs = null;//www .j a va 2s . c o m String sql = null; sql = "INSERT INTO rouge_achievement_progress (`achievement_key`, `user_id`, `progress`) " + "VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE progress = GREATEST(progress, ?)"; try { connection = dbManager.getConnection(); stmt = connection.prepareStatement(sql); stmt.setString(1, key); stmt.setLong(2, userId); stmt.setDouble(3, progress); stmt.setDouble(4, progress); int ret = stmt.executeUpdate(); return (ret > 0); } catch (SQLException e) { log.error(stmt); log.error(e); return false; } finally { DbUtils.closeQuietly(rs); DbUtils.closeQuietly(stmt); DbUtils.closeQuietly(connection); } }