List of usage examples for java.sql Statement executeUpdate
int executeUpdate(String sql) throws SQLException;
INSERT
, UPDATE
, or DELETE
statement or an SQL statement that returns nothing, such as an SQL DDL statement. From source file:Main.java
public static void main(String[] argv) throws Exception { String driverName = "com.jnetdirect.jsql.JSQLDriver"; Class.forName(driverName);/*from w ww.j a v a 2s . 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); Statement stmt = connection.createStatement(); String sql = "DELETE FROM my_table WHERE col_string='a string'"; int deleteCount = stmt.executeUpdate(sql); sql = "DELETE FROM my_table WHERE col_string=?"; PreparedStatement pstmt = connection.prepareStatement(sql); pstmt.setString(1, "a string"); deleteCount = pstmt.executeUpdate(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getHSQLConnection(); System.out.println("Got Connection."); Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int,name varchar);"); st.executeUpdate("create view surveyView as (select * from survey);"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); st.executeUpdate("insert into survey (id,name ) values (2,'anotherValue')"); CachedRowSet crs = null;//from w w w . j a va 2 s . c o m RowSetMetaData rsMD = new RowSetMetaDataImpl(); rsMD.setColumnCount(2); rsMD.setColumnName(1, "id"); rsMD.setColumnType(1, Types.VARCHAR); rsMD.setColumnName(2, "name"); rsMD.setColumnType(2, Types.VARCHAR); // sets the designated column's table name, if any, to the given String. rsMD.setTableName(1, "survey"); rsMD.setTableName(2, "survey"); // use a custom made RowSetMetaData object for CachedRowSet object crs = new CachedRowSetImpl(); crs.setMetaData(rsMD); crs.moveToInsertRow(); crs.updateString(1, "1111"); crs.updateString(2, "alex"); crs.insertRow(); crs.moveToInsertRow(); crs.updateString(1, "2222"); crs.updateString(2, "jane"); crs.insertRow(); // if you want to commit changes from a CachedRowSet // object to your desired datasource, then you must // create a Connection object. // //conn = getHSQLConnection(); // moves the cursor to the remembered cursor position, usually // the current row. This method has no effect if the cursor is // not on the insert row. crs.moveToCurrentRow(); // when the method acceptChanges() is executed, the CachedRowSet // object's writer, a RowSetWriterImpl object, is called behind the // scenes to write the changes made to the rowset to the underlying // data source. The writer is implemented to make a connection to // the data source and write updates to it. crs.acceptChanges(conn); conn.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { Statement stmt = connection.createStatement(); // Load the data String filename = "c:/infile.txt"; String tablename = "mysql_2_table"; stmt.executeUpdate("LOAD DATA INFILE \"" + filename + "\" INTO TABLE " + tablename); // file is comma-separated stmt.executeUpdate(//from w ww .j a v a 2 s. c o m "LOAD DATA INFILE \"" + filename + "\" INTO TABLE " + tablename + " FIELDS TERMINATED BY ','"); }
From source file:Main.java
public static void main(String[] argv) throws Exception { String driverName = "com.jnetdirect.jsql.JSQLDriver"; Class.forName(driverName);/* w w w . ja v a 2 s . 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); Statement stmt = connection.createStatement(); // Use TRUNCATE String sql = "TRUNCATE my_table"; // Execute deletion stmt.executeUpdate(sql); // Use DELETE sql = "DELETE FROM my_table"; // Execute deletion stmt.executeUpdate(sql); }
From source file:Main.java
public static void main(String[] argv) throws Exception { Connection connection = DriverManager.getConnection(url, username, password); Statement stmt = connection.createStatement(); // Load the data String filename = "c:/infile.txt"; String tablename = "mysql_2_table"; stmt.executeUpdate("LOAD DATA INFILE \"" + filename + "\" INTO TABLE " + tablename); // file is terminated by \r\n, use this statement stmt.executeUpdate(/*from ww w .j a v a 2 s.co m*/ "LOAD DATA INFILE \"" + filename + "\" INTO TABLE " + tablename + " LINES TERMINATED BY '\\r\\n'"); }
From source file:MainClass.java
public static void main(String args[]) { Connection conn = null;/* w ww .j a v a2 s . c o m*/ Statement stmt = null; try { conn = getConnection(); stmt = conn.createStatement(); stmt.executeUpdate(EMPLOYEE_TABLE); stmt.executeUpdate("insert into MyEmployees3(id, firstName) values(100, 'A')"); stmt.executeUpdate("insert into MyEmployees3(id, firstName) values(200, 'B')"); System.out.println("CreateEmployeeTableOracle: main(): table created."); } catch (ClassNotFoundException e) { System.out.println("error: failed to load Oracle driver."); e.printStackTrace(); } catch (SQLException e) { System.out.println("error: failed to create a connection object."); e.printStackTrace(); } catch (Exception e) { System.out.println("other error:"); e.printStackTrace(); } finally { try { stmt.close(); conn.close(); } catch (Exception e) { } } }
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 av a2 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:MainClass.java
public static void main(String args[]) { Connection conn = null;//from www. ja v a 2 s. c om Statement stmt = null; try { conn = getConnection(); stmt = conn.createStatement(); stmt.executeUpdate(EMPLOYEE_TABLE); stmt.executeUpdate("insert into MyEmployees3(id, firstName) values(100, 'A')"); stmt.executeUpdate("insert into MyEmployees3(id, firstName) values(200, 'B')"); System.out.println("CreateEmployeeTableMySQL: main(): table created."); } catch (ClassNotFoundException e) { System.out.println("error: failed to load MySQL driver."); e.printStackTrace(); } catch (SQLException e) { System.out.println("error: failed to create a connection object."); e.printStackTrace(); } catch (Exception e) { System.out.println("other error:"); e.printStackTrace(); } finally { try { stmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); Statement st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); st.executeUpdate("create table survey (id int,name varchar(30));"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); st.executeUpdate("insert into survey (id,name ) values (2,null)"); st.executeUpdate("insert into survey (id,name ) values (3,'Tom')"); ResultSet rs = st.executeQuery("SELECT * FROM survey"); // Get cursor position int pos = rs.getRow(); // 0 System.out.println(pos);// www.j av a 2s .co m boolean b = rs.isBeforeFirst(); // true System.out.println(b); // Move cursor to the first row rs.next(); // Get cursor position pos = rs.getRow(); // 1 b = rs.isFirst(); // true System.out.println(pos); System.out.println(b); // Move cursor to the last row rs.last(); // Get cursor position pos = rs.getRow(); System.out.println(pos); b = rs.isLast(); // true // Move cursor past last row rs.afterLast(); // Get cursor position pos = rs.getRow(); b = rs.isAfterLast(); // true 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 w w w . j a va2 s . 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); Statement stmt = connection.createStatement(); String sql = "UPDATE my_table SET col_string='a new string' WHERE col_string = 'a string'"; int updateCount = stmt.executeUpdate(sql); }