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 = 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')"); ResultSet indexInformation = null; DatabaseMetaData meta = conn.getMetaData(); // The '_' character represents any single character. // The '%' character represents any sequence of zero // or more characters. indexInformation = meta.getIndexInfo(conn.getCatalog(), null, "survey", true, true); while (indexInformation.next()) { short type = indexInformation.getShort("TYPE"); switch (type) { case DatabaseMetaData.tableIndexClustered: System.out.println("tableIndexClustered"); case DatabaseMetaData.tableIndexHashed: System.out.println("tableIndexHashed"); case DatabaseMetaData.tableIndexOther: System.out.println("tableIndexOther"); case DatabaseMetaData.tableIndexStatistic: System.out.println("tableIndexStatistic"); default:// w w w . j a va 2 s. co m System.out.println("tableIndexOther"); } } st.close(); conn.close(); }
From source file:InsertRowUpdatableResultSet_MySQL.java
public static void main(String[] args) { Connection conn = null;/* w w w . j av a 2 s. co m*/ Statement stmt = null; ResultSet rs = null; try { conn = getConnection(); String query = "select id, name from employees"; stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); rs = stmt.executeQuery(query); while (rs.next()) { String id = rs.getString(1); String name = rs.getString(2); System.out.println("id=" + id + " name=" + name); } // Move cursor to the "insert row" rs.moveToInsertRow(); // Set values for the new row. rs.updateString("id", "001"); rs.updateString("name", "newName"); // Insert the new row rs.insertRow(); // scroll from the top again rs.beforeFirst(); while (rs.next()) { String id = rs.getString(1); String name = rs.getString(2); System.out.println("id=" + id + " name=" + name); } } catch (Exception e) { e.printStackTrace(); System.exit(1); } finally { // release database resources try { rs.close(); stmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }
From source file:mx.com.pixup.portal.demo.DemoDisqueraSelect.java
public static void main(String[] args) { System.out.println("BIENVENIDO A PIXUP"); System.out.println("Mantenimiento catlogo disquera"); System.out.println("Listado de disqueras"); Connection connection = null; Statement statement = null; ResultSet resultSet = null;// w ww. j av a 2 s .c o m try { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUsername("root"); dataSource.setPassword("admin"); dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/pixup"); connection = dataSource.getConnection(); statement = connection.createStatement(); String sql = "select * from disquera order by nombre desc"; resultSet = statement.executeQuery(sql); System.out.println("ID: \t NOMBRE:"); while (resultSet.next()) { Integer id = resultSet.getInt(1); String nombre = resultSet.getString(2); System.out.println(id + " \t " + nombre); } } catch (Exception e) { System.out.println("Error en el sistema, intente ms tarde!!" + e.getMessage()); } finally { if (resultSet != null) { try { resultSet.close(); } catch (Exception e) { } } if (statement != null) { try { statement.close(); } catch (Exception e) { } } if (connection != null) { try { connection.close(); } catch (Exception e) { } } } }
From source file:InsertSuppliers.java
public static void main(String args[]) { String url = "jdbc:mySubprotocol:myDataSource"; Connection con;//from www .j av a2s.c o m Statement stmt; String query = "select SUP_NAME, SUP_ID from SUPPLIERS"; 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(); stmt.executeUpdate("insert into SUPPLIERS " + "values(49, 'Superior Coffee', '1 Party Place', " + "'Mendocino', 'CA', '95460')"); stmt.executeUpdate("insert into SUPPLIERS " + "values(101, 'Acme, Inc.', '99 Market Street', " + "'Groundsville', 'CA', '95199')"); stmt.executeUpdate("insert into SUPPLIERS " + "values(150, 'The High Ground', '100 Coffee Lane', " + "'Meadows', 'CA', '93966')"); ResultSet rs = stmt.executeQuery(query); System.out.println("Suppliers and their ID Numbers:"); while (rs.next()) { String s = rs.getString("SUP_NAME"); int n = rs.getInt("SUP_ID"); System.out.println(s + " " + n); } stmt.close(); con.close(); } catch (SQLException ex) { System.err.println("SQLException: " + ex.getMessage()); } }
From source file:com.jt.dbcp.example.BasicDataSourceExample.java
public static void main(String[] args) { // First we set up the BasicDataSource. // Normally this would be handled auto-magically by // an external configuration, but in this example we'll // do it manually. ///*from w ww .ja v a 2s . c om*/ String param1 = null; String param2 = null; if (args.length == 2) { param1 = args[0]; param2 = args[1]; } if (param1 == null) { param1 = "jdbc:mysql://localhost:3306/test"; param2 = "select * from t_user"; } System.out.println("Setting up data source."); DataSource dataSource = setupDataSource(param1); System.out.println("Done."); // // Now, we can use JDBC DataSource as we normally would. // Connection conn = null; Statement stmt = null; ResultSet rset = null; try { System.out.println("Creating connection."); conn = dataSource.getConnection(); System.out.println("Creating statement."); stmt = conn.createStatement(); System.out.println("Executing statement."); rset = stmt.executeQuery(param2); System.out.println("Results:"); int numcols = rset.getMetaData().getColumnCount(); int count = 0; while (rset.next()) { for (int i = 1; i <= numcols; i++) { System.out.print("\t" + rset.getString(i)); } System.out.println(""); count++; if (count == 10) { break; } } printDataSourceStats(dataSource); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (rset != null) rset.close(); } catch (Exception e) { } try { if (stmt != null) stmt.close(); } catch (Exception e) { } try { if (conn != null) conn.close(); } catch (Exception e) { } try { shutdownDataSource(dataSource); } catch (SQLException e) { e.printStackTrace(); } } }
From source file:InsertCoffees.java
public static void main(String args[]) { String url = "jdbc:mySubprotocol:myDataSource"; Connection con;/*from w ww . ja v a 2 s .c o m*/ Statement stmt; String query = "select COF_NAME, PRICE from COFFEES"; 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(); stmt.executeUpdate("insert into COFFEES " + "values('Colombian', 00101, 7.99, 0, 0)"); stmt.executeUpdate("insert into COFFEES " + "values('French_Roast', 00049, 8.99, 0, 0)"); stmt.executeUpdate("insert into COFFEES " + "values('Espresso', 00150, 9.99, 0, 0)"); stmt.executeUpdate("insert into COFFEES " + "values('Colombian_Decaf', 00101, 8.99, 0, 0)"); stmt.executeUpdate("insert into COFFEES " + "values('French_Roast_Decaf', 00049, 9.99, 0, 0)"); ResultSet rs = stmt.executeQuery(query); System.out.println("Coffee Break Coffees and Prices:"); while (rs.next()) { String s = rs.getString("COF_NAME"); float f = rs.getFloat("PRICE"); System.out.println(s + " " + f); } stmt.close(); con.close(); } catch (SQLException ex) { System.err.println("SQLException: " + ex.getMessage()); } }
From source file:InsertRowBug.java
public static void main(String args[]) { String url;/* ww w. ja v a 2 s .c o m*/ // url = "jdbc:odbc:SQL Anywhere 5.0 Sample"; // url = "jdbc:oracle:thin:@server:1521:db570"; url = "jdbc:odbc:RainForestDSN"; String driver; //driver = "oracle.jdbc.driver.OracleDriver"; driver = "sun.jdbc.odbc.JdbcOdbcDriver"; String user, pass; user = "student"; pass = "student"; Connection con; Statement stmt; ResultSet uprs; try { Class.forName(driver); } catch (java.lang.ClassNotFoundException e) { System.err.println(e); return; } try { con = DriverManager.getConnection(url, user, pass); stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); uprs = stmt.executeQuery("SELECT * FROM Music_Recordings"); // Check the column count ResultSetMetaData md = uprs.getMetaData(); System.out.println("Resultset has " + md.getColumnCount() + " cols."); int rowNum = uprs.getRow(); System.out.println("row1 " + rowNum); uprs.absolute(1); rowNum = uprs.getRow(); System.out.println("row2 " + rowNum); uprs.next(); uprs.moveToInsertRow(); uprs.updateInt(1, 150); uprs.updateString(2, "Madonna"); uprs.updateString(3, "Dummy"); uprs.updateString(4, "Jazz"); uprs.updateString(5, "Image"); uprs.updateInt(6, 5); uprs.updateDouble(7, 5); uprs.updateInt(8, 15); uprs.insertRow(); uprs.close(); stmt.close(); con.close(); } catch (SQLException ex) { System.err.println("SQLException: " + ex.getMessage()); } }
From source file:com.jt.dbcp.example.ManualPoolingDataSourceExample.java
public static void main(String[] args) throws SQLException { ///*from w w w . j ava2 s .c om*/ // First we load the underlying JDBC driver. // You need this if you don't use the jdbc.drivers // system property. // System.out.println("Loading underlying JDBC driver."); try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } System.out.println("Done."); // // Then, we set up the PoolingDataSource. // Normally this would be handled auto-magically by // an external configuration, but in this example we'll // do it manually. // System.out.println("Setting up data source."); DataSource dataSource = setupDataSource(args[0]); System.out.println("Done."); // // Now, we can use JDBC DataSource as we normally would. // Connection conn = null; Statement stmt = null; ResultSet rset = null; try { System.out.println("Creating connection."); conn = dataSource.getConnection(); System.out.println("Creating statement."); stmt = conn.createStatement(); System.out.println("Executing statement."); rset = stmt.executeQuery(args[1]); System.out.println("Results:"); int numcols = rset.getMetaData().getColumnCount(); int count = 0; while (rset.next()) { count++; if (count == 10) { break; } for (int i = 1; i <= numcols; i++) { System.out.print("\t" + rset.getString(i)); } System.out.println(""); } printDataSourceStats(dataSource); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (rset != null) rset.close(); } catch (Exception e) { } try { if (stmt != null) stmt.close(); } catch (Exception e) { } try { if (conn != null) conn.close(); } catch (Exception e) { } // shutdownDataSource(dataSource); } }
From source file:PrintColumns.java
public static void main(String args[]) { String url = "jdbc:mySubprotocol:myDataSource"; Connection con;//w ww . ja v a 2s . co m String query = "select * from COFFEES"; 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); ResultSetMetaData rsmd = rs.getMetaData(); PrintColumnTypes.printColTypes(rsmd); System.out.println(""); int numberOfColumns = rsmd.getColumnCount(); for (int i = 1; i <= numberOfColumns; i++) { if (i > 1) System.out.print(", "); String columnName = rsmd.getColumnName(i); System.out.print(columnName); } System.out.println(""); while (rs.next()) { for (int i = 1; i <= numberOfColumns; i++) { if (i > 1) System.out.print(", "); String columnValue = rs.getString(i); System.out.print(columnValue); } System.out.println(""); } stmt.close(); con.close(); } catch (SQLException ex) { System.err.print("SQLException: "); System.err.println(ex.getMessage()); } }
From source file:com.jt.dbcp.example.ManualPoolingDriverExample.java
public static void main(String[] args) { ///*w ww . ja v a 2s .com*/ // First we load the underlying JDBC driver. // You need this if you don't use the jdbc.drivers // system property. // System.out.println("Loading underlying JDBC driver."); try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } System.out.println("Done."); // // Then we set up and register the PoolingDriver. // Normally this would be handled auto-magically by // an external configuration, but in this example we'll // do it manually. // System.out.println("Setting up driver."); try { setupDriver(args[0]); } catch (Exception e) { e.printStackTrace(); } System.out.println("Done."); // // Now, we can use JDBC as we normally would. // Using the connect string // jdbc:apache:commons:dbcp:example // The general form being: // jdbc:apache:commons:dbcp:<name-of-pool> // Connection conn = null; Statement stmt = null; ResultSet rset = null; try { System.out.println("Creating connection."); //pool conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:example"); System.out.println("Creating statement."); stmt = conn.createStatement(); System.out.println("Executing statement."); rset = stmt.executeQuery(args[1]); System.out.println("Results:"); int numcols = rset.getMetaData().getColumnCount(); int count = 0; while (rset.next()) { count++; if (count == 10) { break; } for (int i = 1; i <= numcols; i++) { System.out.print("\t" + rset.getString(i)); } System.out.println(""); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (rset != null) rset.close(); } catch (Exception e) { } try { if (stmt != null) stmt.close(); } catch (Exception e) { } try { if (conn != null) conn.close(); } catch (Exception e) { } } // Display some pool statistics try { printDriverStats(); } catch (Exception e) { e.printStackTrace(); } // closes the pool try { shutdownDriver(); } catch (Exception e) { e.printStackTrace(); } }