List of usage examples for java.sql PreparedStatement setString
void setString(int parameterIndex, String x) throws SQLException;
String
value. From source file:Main.java
public static final void main(String[] argv) throws Exception { Class.forName("oracle.jdbc.OracleDriver"); Connection conn = DriverManager.getConnection("your_connection_string", "your_user_name", "your_password"); Date nowDate = new Date(); Timestamp nowTimestamp = new Timestamp(nowDate.getTime()); PreparedStatement insertStmt = conn.prepareStatement( "INSERT INTO MyTable" + " (os_name, ts, ts_with_tz, ts_with_local_tz)" + " VALUES (?, ?, ?, ?)"); insertStmt.setString(1, System.getProperty("os.name")); insertStmt.setTimestamp(2, nowTimestamp); insertStmt.setTimestamp(3, nowTimestamp); insertStmt.setTimestamp(4, nowTimestamp); insertStmt.executeUpdate();/* w ww . j a v a 2 s .c om*/ insertStmt.close(); System.out.println("os_name, ts, ts_with_tz, ts_with_local_tz"); PreparedStatement selectStmt = conn .prepareStatement("SELECT os_name, ts, ts_with_tz, ts_with_local_tz" + " FROM MyTable"); ResultSet result = null; result = selectStmt.executeQuery(); while (result.next()) { System.out.println(String.format("%s,%s,%s,%s", result.getString(1), result.getTimestamp(2).toString(), result.getTimestamp(3).toString(), result.getTimestamp(4).toString())); } result.close(); selectStmt.close(); conn.close(); }
From source file:DemoPreparedStatementSetURL.java
public static void main(String[] args) throws Exception { String id = "0001"; String urlValue = "http://www.java2s.com"; Connection conn = null;// w ww . j av a 2s .c o m PreparedStatement pstmt = null; try { conn = getConnection(); String query = "insert into url_table(id, url) values(?, ?)"; pstmt = conn.prepareStatement(query); pstmt.setString(1, id); pstmt.setURL(2, new java.net.URL(urlValue)); // 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:DemoPreparedStatementSetString.java
public static void main(String[] args) throws Exception { String stringValue = "stringValueToBeInserted"; Connection conn = null;//from w w w .ja v a2 s . com PreparedStatement pstmt = null; try { conn = getConnection(); String query = "insert into string_table(string_column) values(?)"; pstmt = conn.prepareStatement(query); pstmt.setString(1, stringValue); // 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:DemoDisplayBlobFromDatabase.java
public static void main(String args[]) throws Exception { Connection conn = null;/*w ww .j a va2 s .c o m*/ ResultSet rs = null; PreparedStatement pstmt = null; String query = "SELECT blob_column FROM blob_table WHERE id = ?"; try { conn = getConnection(); pstmt = conn.prepareStatement(query); pstmt.setString(1, "0001"); rs = pstmt.executeQuery(); rs.next(); // materialize binary data onto client java.sql.Blob blob = rs.getBlob(1); } finally { rs.close(); pstmt.close(); conn.close(); } }
From source file:DemoPreparedStatementSetBoolean.java
public static void main(String[] args) throws Exception { boolean booleanValue = true; Connection conn = null;//from w ww . j ava 2s .c om PreparedStatement pstmt = null; try { conn = getConnection(); String query = "insert into boolean_table(id, boolean_column) values(?, ?)"; pstmt = conn.prepareStatement(query); pstmt.setString(1, "0001"); pstmt.setBoolean(2, booleanValue); int rowCount = pstmt.executeUpdate(); System.out.println("rowCount=" + rowCount); } finally { pstmt.close(); conn.close(); } }
From source file:DemoPreparedStatementSetDate.java
public static void main(String[] args) throws Exception { Connection conn = null;/* w ww .ja va 2s. com*/ PreparedStatement pstmt = null; try { conn = getConnection(); String query = "insert into date_table(id, date_column) values(?, ?)"; pstmt = conn.prepareStatement(query); pstmt.setString(1, "0001"); java.sql.Date date = getCurrentJavaSqlDate(); pstmt.setDate(2, date); // 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[] args) throws Exception { String url = "jdbc:mysql://localhost/testdb"; String username = "root"; String password = ""; Class.forName("com.mysql.jdbc.Driver"); Connection conn = null;/*from w w w . j ava2 s.com*/ 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:UpdateRecordsUsingPreparedStatement.java
public static void main(String[] args) throws Exception { Connection conn = null;// w w w .j ava 2 s.c o m PreparedStatement pstmt = null; try { conn = getConnection(); String query = "update dept set DEPT_LOC = ? where DEPT_NUM = ? "; pstmt = conn.prepareStatement(query); // create a statement pstmt.setString(1, "deptLocation"); // set input parameter 1 pstmt.setInt(2, 1001); // set input parameter 2 pstmt.executeUpdate(); // execute update statement } catch (Exception e) { e.printStackTrace(); System.exit(1); } finally { pstmt.close(); conn.close(); } }
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 a v a2 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:InsertPictureToMySql.java
public static void main(String[] args) throws Exception, IOException, SQLException { Class.forName("org.gjt.mm.mysql.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/databaseName", "root", "root"); String INSERT_PICTURE = "insert into MyPictures(id, name, photo) values (?, ?, ?)"; FileInputStream fis = null;/*from w w w .j a va 2 s . c o m*/ PreparedStatement ps = null; try { conn.setAutoCommit(false); File file = new File("myPhoto.png"); fis = new FileInputStream(file); ps = conn.prepareStatement(INSERT_PICTURE); ps.setString(1, "001"); ps.setString(2, "name"); ps.setBinaryStream(3, fis, (int) file.length()); ps.executeUpdate(); conn.commit(); } finally { ps.close(); fis.close(); } }