List of usage examples for java.sql Statement close
void close() throws SQLException;
Statement
object's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed. From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getHSQLConnection(); conn.setAutoCommit(false);/* w w w . j a v a2 s . co m*/ Statement st = conn.createStatement(); try { 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,'nameValue')"); // commits all the transactions conn.commit(); } catch (Exception e) { //cancel (roll back) all the transactions conn.rollback(); // to see what went wrong e.printStackTrace(); } st = conn.createStatement(); ResultSet rs = st.executeQuery("SELECT * FROM survey"); outputResultSet(rs); rs.close(); st.close(); conn.close(); }
From source file:JDBCQuery.java
public static void main(String[] av) { try {/* www.j a v a2 s . c o m*/ System.out.println("Loading Driver (with Class.forName)"); // Load the jdbc-odbc bridge driver Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // Enable logging // DriverManager.setLogStream(System.err); System.out.println("Getting Connection"); Connection conn = DriverManager.getConnection("jdbc:odbc:Companies", "ian", ""); // user, passwd // Any warnings generated by the connect? checkForWarning(conn.getWarnings()); System.out.println("Creating Statement"); Statement stmt = conn.createStatement(); System.out.println("Executing Query"); ResultSet rs = stmt.executeQuery("SELECT * FROM Companies"); System.out.println("Retrieving Results"); int i = 0; while (rs.next()) { System.out.println("Retrieving Company ID"); int x = rs.getInt("CustNO"); System.out.println("Retrieving Name"); String s = rs.getString("Company"); System.out.println("ROW " + ++i + ": " + x + "; " + s + "; " + "."); } rs.close(); // All done with that resultset stmt.close(); // All done with that statement conn.close(); // All done with that DB connection } catch (ClassNotFoundException e) { System.out.println("Can't load driver " + e); } catch (SQLException e) { System.out.println("Database access failed " + e); } }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getHSQLConnection(); conn.setAutoCommit(false);/*from w ww . j a va 2 s . co m*/ Statement st = conn.createStatement(); try { 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,'nameValue')"); // commits all the transactions conn.commit(); } catch (Exception e) { // cancel (roll back) all the transactions conn.rollback(); // to see what went wrong e.printStackTrace(); } st = conn.createStatement(); 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 = getMySqlConnection(); System.out.println("Got Connection."); Statement st = conn.createStatement(); st.executeUpdate("drop table survey;"); st.executeUpdate("create table survey (id int,name varchar(30));"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); String tableName = "survey"; String query = "select count(*) from " + tableName; Statement stmt = null;//from w w w .j a v a 2 s . c om ResultSet rs = null; try { stmt = conn.createStatement(); rs = stmt.executeQuery(query); System.out.println("Exist"); ; } catch (Exception e) { // table does not exist or some other problem //e.printStackTrace(); System.out.println("Not Exist"); } st.close(); conn.close(); }
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_UPDATABLE); 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"); // Move cursor to the row to update rs.first();//from ww w. j a va2s .c om // Update the value of column column_1 on that row rs.updateString(2, "new data"); // Update the row; if autocommit is enabled, // update is committed rs.updateRow(); rs.close(); st.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getMySqlConnection(); System.out.println("Got Connection."); Statement st = conn.createStatement(); st.executeUpdate("drop table survey;"); st.executeUpdate("create table survey (id int,name varchar(30));"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); DatabaseMetaData meta = conn.getMetaData(); if (meta.supportsResultSetType(ResultSet.TYPE_FORWARD_ONLY)) { System.out.println("type name=TYPE_FORWARD_ONLY"); }//from ww w. jav a2s . c om if (meta.supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE)) { System.out.println("type name=TYPE_SCROLL_INSENSITIVE"); } if (meta.supportsResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE)) { System.out.println("type name=TYPE_SCROLL_SENSITIVE"); } st.close(); conn.close(); }
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_UPDATABLE); 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"); // Move cursor to the row to update rs.first();/*from w ww .ja v a 2 s . c o m*/ // Update the value of column column_1 on that row rs.updateString("name", "new data"); // Update the row; if autocommit is enabled, // update is committed rs.updateRow(); rs.close(); st.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getMySqlConnection(); System.out.println("Got Connection."); Statement st = conn.createStatement(); st.executeUpdate("drop table survey;"); st.executeUpdate("create table survey (id int,name varchar(30));"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); String tableName = "survey"; String query = "select * from " + tableName + " where 1=0"; Statement stmt = null;// w w w . ja v a2 s . c o m ResultSet rs = null; try { stmt = conn.createStatement(); rs = stmt.executeQuery(query); System.out.println("Exist"); ; } catch (Exception e) { // table does not exist or some other problem //e.printStackTrace(); System.out.println("Not Exist"); } st.close(); conn.close(); }
From source file:Join.java
public static void main(String args[]) { String url = "jdbc:mySubprotocol:myDataSource"; Connection con;/* w ww .j av a 2 s .co m*/ String query = "select SUPPLIERS.SUP_NAME, COFFEES.COF_NAME " + "from COFFEES, SUPPLIERS " + "where SUPPLIERS.SUP_NAME like 'Acme, Inc.' and " + "SUPPLIERS.SUP_ID = COFFEES.SUP_ID"; Statement stmt; 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"); stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(query); System.out.println("Supplier, Coffee:"); while (rs.next()) { String supName = rs.getString(1); String cofName = rs.getString(2); System.out.println(" " + supName + ", " + cofName); } stmt.close(); con.close(); } catch (SQLException ex) { System.err.print("SQLException: "); System.err.println(ex.getMessage()); } }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); Statement st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); 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')"); st = conn.createStatement();/*from w w w .j a v a 2s . co m*/ ResultSet rs = st.executeQuery("SELECT * FROM survey"); while (rs.next()) { String name = rs.getString(2); if (rs.wasNull()) { System.out.println("was NULL"); } else { System.out.println("not NULL"); } } rs.close(); st.close(); conn.close(); }