List of usage examples for java.sql PreparedStatement setString
void setString(int parameterIndex, String x) throws SQLException;
String
value. From source file:MainClass.java
public static void main(String[] args) throws Exception { if (args.length != 2) { System.out.println("Usage: java JavaDBDemo <Name> <Address>"); System.exit(1);// w ww. j ava 2s. c o m } String driver = "org.apache.derby.jdbc.EmbeddedDriver"; String dbName = "AddressBookDB"; String connectionURL = "jdbc:derby:" + dbName + ";create=true"; String createString = "CREATE TABLE ADDRESSBOOKTbl (NAME VARCHAR(32) NOT NULL, ADDRESS VARCHAR(50) NOT NULL)"; Class.forName(driver); conn = DriverManager.getConnection(connectionURL); Statement stmt = conn.createStatement(); stmt.executeUpdate(createString); PreparedStatement psInsert = conn.prepareStatement("insert into ADDRESSBOOKTbl values (?,?)"); psInsert.setString(1, args[0]); psInsert.setString(2, args[1]); psInsert.executeUpdate(); Statement stmt2 = conn.createStatement(); ResultSet rs = stmt2.executeQuery("select * from ADDRESSBOOKTbl"); System.out.println("Addressed present in your Address Book\n\n"); int num = 0; while (rs.next()) { System.out.println(++num + ": Name: " + rs.getString(1) + "\n Address" + rs.getString(2)); } rs.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); conn.setAutoCommit(false);/*from w w w.j a v a 2 s . c o m*/ Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int, name VARCHAR(30) );"); String INSERT_RECORD = "insert into survey(id, name) values(?,?)"; PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD); pstmt.setString(1, "1"); pstmt.setString(2, "name1"); pstmt.addBatch(); pstmt.setString(1, "2"); pstmt.setString(2, "name2"); pstmt.addBatch(); // execute the batch int[] updateCounts = pstmt.executeBatch(); checkUpdateCounts(updateCounts); // since there were no errors, commit conn.commit(); ResultSet rs = st.executeQuery("SELECT * FROM survey"); outputResultSet(rs); rs.close(); st.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection con = DriverManager.getConnection("jdbc:mysql://192.168.1.3/zzzTest?" + // "useUnicode=yes&characterEncoding=UTF-8" + // "&user=root&password=whatever"); String newName = "G"; String newEmail = "g@example.com"; String newMobile = "444-555-2222"; String sql = "SELECT " + // "id, " + // "name, " + // "email, " + // "mobile " + // "FROM registerSmsUsers " + // "WHERE mobile = ? " + // "FOR UPDATE"; PreparedStatement pst = con.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE); pst.setString(1, newMobile); ResultSet rs = pst.executeQuery(); if (rs.next()) { rs.moveToCurrentRow();// ww w .j av a2 s . c o m rs.updateString("name", newName); rs.updateString("email", newEmail); rs.updateRow(); System.out.println("Existing row updated."); } else { rs.moveToInsertRow(); rs.updateString("name", newName); rs.updateString("email", newEmail); rs.updateString("mobile", newMobile); rs.insertRow(); System.out.println("New row inserted."); } }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); conn.setAutoCommit(false);/*from w w w . jav a 2 s .c o m*/ Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int, name VARCHAR(30) );"); String INSERT_RECORD = "insert into survey(id, name) values(?,?)"; PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD); pstmt.setInt(1, 1); pstmt.setString(2, "name"); pstmt.executeUpdate(); // Get warnings on PreparedStatement object SQLWarning warning = pstmt.getWarnings(); while (warning != null) { // Process statement warnings... String message = warning.getMessage(); String sqlState = warning.getSQLState(); int errorCode = warning.getErrorCode(); warning = warning.getNextWarning(); } ResultSet rs = st.executeQuery("SELECT * FROM survey"); outputResultSet(rs); rs.close(); st.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); conn.setAutoCommit(false);/* w w w. j a v a2 s . c o m*/ Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int, name VARCHAR(30) );"); String INSERT_RECORD = "insert into survey(id, name) values(?,?)"; PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD); pstmt.setString(1, "1"); pstmt.setString(2, "name1"); pstmt.addBatch(); pstmt.setString(1, "2"); pstmt.setString(2, "name2"); pstmt.addBatch(); try { // execute the batch int[] updateCounts = pstmt.executeBatch(); } catch (BatchUpdateException e) { int[] updateCounts = e.getUpdateCounts(); checkUpdateCounts(updateCounts); try { conn.rollback(); } catch (Exception e2) { e.printStackTrace(); System.exit(1); } } // since there were no errors, commit conn.commit(); ResultSet rs = st.executeQuery("SELECT * FROM survey"); outputResultSet(rs); rs.close(); st.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 . ja va2 s . c om 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 my_table (col_string) VALUES(?)"; PreparedStatement pstmt = connection.prepareStatement(sql); // Insert 10 rows for (int i = 0; i < 10; i++) { // Set the value pstmt.setString(1, "row " + i); // Insert the row pstmt.executeUpdate(); } }
From source file:GetDateFromOracle.java
public static void main(String args[]) { String GET_RECORD = "select date_column, time_column, " + "timestamp_column from TestDates where id = ?"; ResultSet rs = null;//from w w w. j a v a 2 s .c o m Connection conn = null; PreparedStatement pstmt = null; try { conn = getConnection(); pstmt = conn.prepareStatement(GET_RECORD); pstmt.setString(1, "0001"); rs = pstmt.executeQuery(); while (rs.next()) { java.sql.Date dbSqlDate = rs.getDate(1); java.sql.Time dbSqlTime = rs.getTime(2); java.sql.Timestamp dbSqlTimestamp = rs.getTimestamp(3); System.out.println("dbSqlDate=" + dbSqlDate); System.out.println("dbSqlTime=" + dbSqlTime); System.out.println("dbSqlTimestamp=" + dbSqlTimestamp); } } catch (Exception e) { e.printStackTrace(); } finally { try { rs.close(); pstmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }
From source file:InsertDateToOracle.java
public static void main(String args[]) throws Exception { String INSERT_RECORD = "insert into TestDates(id, date_column, " + "time_column, timestamp_column) values(?, ?, ?, ?)"; Connection conn = null;// w w w . j av a 2 s . c o m PreparedStatement pstmt = null; try { conn = getConnection(); pstmt = conn.prepareStatement(INSERT_RECORD); pstmt.setString(1, "001"); java.util.Date date = new java.util.Date(); long t = date.getTime(); java.sql.Date sqlDate = new java.sql.Date(t); java.sql.Time sqlTime = new java.sql.Time(t); java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(t); System.out.println("sqlDate=" + sqlDate); System.out.println("sqlTime=" + sqlTime); System.out.println("sqlTimestamp=" + sqlTimestamp); pstmt.setDate(2, sqlDate); pstmt.setTime(3, sqlTime); pstmt.setTimestamp(4, sqlTimestamp); pstmt.executeUpdate(); } catch (Exception e) { e.printStackTrace(); System.out.println("Failed to insert the record."); } finally { pstmt.close(); conn.close(); } }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int,myDate DATE);"); String INSERT_RECORD = "insert into survey(id, myDate) values(?, ?)"; PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD); pstmt.setString(1, "1"); java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime()); pstmt.setDate(2, sqlDate);//from w ww . j a va2 s .c om pstmt.executeUpdate(); ResultSet rs = st.executeQuery("SELECT * FROM survey"); ResultSetMetaData rsmd = rs.getMetaData(); int numCols = rsmd.getColumnCount(); System.out.print("\t"); for (int i = 1; i <= numCols; i++) { System.out.println(rsmd.getColumnLabel(i)); } System.out.print("\nAuto Increment\t"); for (int i = 1; i <= numCols; i++) { System.out.println(rsmd.isAutoIncrement(i)); } for (int i = 1; i <= numCols; i++) { System.out.println(rsmd.isCaseSensitive(i)); } System.out.print("\nSearchable\t"); for (int i = 1; i <= numCols; i++) { System.out.println(rsmd.isSearchable(i)); } System.out.print("\nCurrency\t"); for (int i = 1; i <= numCols; i++) { System.out.println(rsmd.isCurrency(i)); } System.out.print("\nAllows nulls\t"); for (int i = 1; i <= numCols; i++) { System.out.println(rsmd.isNullable(i)); } System.out.print("\nSigned\t"); for (int i = 1; i <= numCols; i++) { System.out.println(rsmd.isSigned(i)); } System.out.print("\nRead only\t"); for (int i = 1; i <= numCols; i++) { System.out.println(rsmd.isReadOnly(i)); } System.out.print("\nWritable\t"); for (int i = 1; i <= numCols; i++) { System.out.print(rsmd.isWritable(i)); } System.out.print("\nDefinitely Writable\t"); for (int i = 1; i <= numCols; i++) { System.out.println(rsmd.isDefinitelyWritable(i)); } conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); conn.setAutoCommit(false);//from w w w .j av a 2 s.c o m Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int, name VARCHAR(30) );"); String INSERT_RECORD = "insert into survey(id, name) values(?,?)"; PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD); for (int i = 0; i < 10; i++) { pstmt.setInt(1, i); pstmt.setString(2, "name" + i); pstmt.executeUpdate(); } ResultSet rs = st.executeQuery("SELECT * FROM survey"); outputResultSet(rs); rs.close(); st.close(); conn.close(); }