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:CreateUDTs.java
public static void main(String args[]) { String url = "jdbc:mySubprotocol:myDataSource"; Connection con;// ww w. j a v a2 s.c o m Statement stmt; String createAddress = "CREATE TYPE ADDRESS (NUM INTEGER, " + "STREET VARCHAR(40), CITY VARCHAR(40), " + "STATE CHAR(2), ZIP CHAR(5))"; String createManager = "CREATE TYPE MANAGER (MGR_ID INTEGER, " + "LAST_NAME VARCHAR(40), FIRST_NAME VARCHAR(40), " + "PHONE char(10))"; 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(createAddress); stmt.executeUpdate("CREATE TYPE PHONE_NO AS CHAR(10)"); stmt.executeUpdate(createManager); stmt.close(); con.close(); } catch (SQLException ex) { System.err.println("-----SQLException-----"); System.err.println("SQLState: " + ex.getSQLState()); System.err.println("Message: " + ex.getMessage()); System.err.println("Vendor: " + ex.getErrorCode()); } }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = null;/*from ww w . j a v a2 s. c o m*/ Statement stmt = null; Class.forName(JDBC_DRIVER); conn = DriverManager.getConnection(DB_URL, USER, PASS); System.out.println("Deleting database..."); stmt = conn.createStatement(); conn = DriverManager.getConnection(DB_URL, USER, PASS); stmt = conn.createStatement(); String sql = "SELECT id, first, last, age FROM Person"; ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { // Retrieve by column name int id = rs.getInt("id"); int age = rs.getInt("age"); String first = rs.getString("firstName"); String last = rs.getString("lastName"); // Display values System.out.print("ID: " + id); System.out.print(", Age: " + age); System.out.print(", First: " + first); System.out.println(", Last: " + last); } rs.close(); stmt.close(); conn.close(); }
From source file:mx.com.pixup.portal.demo.DemoDisqueraDelete.java
public static void main(String[] args) { System.out.println("BIENVENIDO A PIXUP"); System.out.println("Mantenimiento catlogo disquera"); System.out.println("Inserte el nombre de la disquera a eliminar: "); InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); Connection connection = null; Statement statement = null; try {// w ww . j av a 2s. c om String nombreDisquera = br.readLine(); 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 = "delete from disquera where nombre = '" + nombreDisquera + "'"; statement.execute(sql); System.out.println("Disquera: " + nombreDisquera + " eliminada con xito"); } catch (Exception e) { System.out.println("Error en el sistema, intente ms tarde!!"); } finally { if (statement != null) { try { statement.close(); } catch (Exception e) { } } if (connection != null) { try { connection.close(); } catch (Exception e) { } } } }
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 . ja va2 s. c om ResultSet rs = st.executeQuery("SELECT * FROM survey"); ResultSetMetaData rsMetaData = rs.getMetaData(); int numberOfColumns = rsMetaData.getColumnCount(); // get the column names; column indexes start from 1 for (int i = 1; i < numberOfColumns + 1; i++) { String columnName = rsMetaData.getColumnName(i); String tableName = rsMetaData.getTableName(i); System.out.println(columnName); System.out.println(tableName); } rs.close(); 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_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 = conn.createStatement();//from w ww . j a v a2s . co m ResultSet rs = st.executeQuery("SELECT * FROM survey"); int rsType = rs.getType(); if (rsType == java.sql.ResultSet.TYPE_FORWARD_ONLY) { System.out.println("java.sql.ResultSet.TYPE_FORWARD_ONLY"); } else if (rsType == java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE) { System.out.println("java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE"); } else if (rsType == java.sql.ResultSet.TYPE_SCROLL_SENSITIVE) { System.out.println("java.sql.ResultSet.TYPE_SCROLL_SENSITIVE"); } else { // it is an error } rs.close(); st.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) { Connection conn = null;/*from w ww . j ava 2 s . c o 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); } rs.first(); rs.deleteRow(); 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 { try { rs.close(); stmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }
From source file:Main.java
public static void main(String args[]) throws Exception { Connection conn = null;/*from w ww . java 2 s .c o m*/ Statement s = null; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); conn = DriverManager.getConnection("jdbc:odbc:Driver={SQL Server};" + "Server=.\\SQLEXPRESS;" + "Trusted_Connection=yes;" + "Database=myDb"); s = conn.createStatement(); s.executeQuery("SELECT * FROM dbo.SalesSummary WHERE 0 = 1"); ResultSet rs = s.getResultSet(); ResultSetMetaData rsmd = rs.getMetaData(); for (int i = 1; i <= rsmd.getColumnCount(); i++) { System.out.println(String.format("-- Column %d --", i)); System.out.println(String.format("Column name: %s", rsmd.getColumnName(i))); System.out.println(String.format("Database-specific type name: %s", rsmd.getColumnTypeName(i))); System.out.println(String.format("Column size (DisplaySize): %d", rsmd.getColumnDisplaySize(i))); System.out.println(String.format("java.sql.Type of column: %d", rsmd.getColumnType(i))); System.out.println(); } s.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')"); st = conn.createStatement();/* w w w . ja va 2s . c o m*/ ResultSet rs = st.executeQuery("SELECT * FROM survey"); ResultSetMetaData rsMetaData = rs.getMetaData(); int numberOfColumns = rsMetaData.getColumnCount(); System.out.println("resultSet MetaData column Count=" + numberOfColumns); for (int i = 1; i <= numberOfColumns; i++) { System.out.println("column MetaData "); System.out.println("column number " + i); // indicates the designated column's normal maximum width in // characters System.out.println(rsMetaData.getColumnDisplaySize(i)); // gets the designated column's suggested title // for use in printouts and displays. } st.close(); conn.close(); }
From source file:SQLStatement.java
public static void main(String args[]) { String url = "jdbc:mySubprotocol:myDataSource"; Connection con;/* w w w . ja va2 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); ResultSetMetaData rsmd = rs.getMetaData(); int numberOfColumns = rsmd.getColumnCount(); int rowCount = 1; while (rs.next()) { System.out.println("Row " + rowCount + ": "); for (int i = 1; i <= numberOfColumns; i++) { System.out.print(" Column " + i + ": "); System.out.println(rs.getString(i)); } System.out.println(""); rowCount++; } 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_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"); // extract data from the ResultSet scroll from top while (rs.next()) { String id = rs.getString(1); String name = rs.getString(2); System.out.println("id=" + id + " name=" + name); }// ww w. j a v a2 s .c om System.out.println("---------"); // scroll from the bottom rs.afterLast(); while (rs.previous()) { String id = rs.getString(1); String name = rs.getString(2); System.out.println("id=" + id + " name=" + name); } rs.close(); st.close(); conn.close(); }