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 = null;//from ww w . j av a 2s . c om 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 = "CREATE TABLE Person" + "(id INTEGER not NULL, " + " firstName VARCHAR(50), " + " lastName VARCHAR(50), " + " age INTEGER, " + " PRIMARY KEY ( id ))"; stmt.executeUpdate(sql); System.out.println("Created table in given database..."); stmt.close(); conn.close(); }
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("insert into survey (id,name ) values (1,'nameValue')"); st.executeUpdate("insert into survey (id,name ) values (2,'anotherValue')"); Statement stmt = conn.createStatement(); String sqlQuery = "SELECT * FROM survey WHERE id='1'"; WebRowSet webRS = new WebRowSetImpl(); webRS.setCommand(sqlQuery);/*from ww w . j av a 2 s . c o m*/ webRS.execute(conn); File file = new File("1.xml"); FileWriter fw = new FileWriter(file); System.out.println("Writing db data to file " + file.getAbsolutePath()); webRS.writeXml(fw); StringWriter sw = new StringWriter(); webRS.writeXml(sw); System.out.println(sw.toString()); fw.flush(); fw.close(); stmt.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { try {/*from w ww . ja va2 s . c o m*/ 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); pstmt.executeUpdate(); ResultSet rs = st.executeQuery("SELECT * FROM survey"); rs.close(); st.close(); conn.close(); } catch (SQLException e) { while (e != null) { String errorMessage = e.getMessage(); System.err.println("sql error message:" + errorMessage); // This vendor-independent string contains a code. String sqlState = e.getSQLState(); System.err.println("sql state:" + sqlState); int errorCode = e.getErrorCode(); System.err.println("error code:" + errorCode); // String driverName = conn.getMetaData().getDriverName(); // System.err.println("driver name:"+driverName); // processDetailError(drivername, errorCode); e = e.getNextException(); } } }
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 TIMESTAMP );"); String INSERT_RECORD = "insert into survey(id) values(?)"; PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD); pstmt.setString(1, "1"); pstmt.executeUpdate();/*w w w .j a v a2 s . c om*/ ResultSet rs = st.executeQuery("SELECT * FROM survey"); outputResultSet(rs); pstmt.setString(1, "2"); pstmt.executeUpdate(); 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')"); DatabaseMetaData meta = conn.getMetaData(); ResultSet schemas = meta.getSchemas(); while (schemas.next()) { String tableSchema = schemas.getString(1); // "TABLE_SCHEM" String tableCatalog = schemas.getString(2); //"TABLE_CATALOG" System.out.println("tableSchema" + tableSchema); }//from w ww . j a v a 2s .c o m 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 (pk_column int,name varchar(30));"); st.executeUpdate("insert into survey (pk_column,name ) values (1,'nameValue')"); st.executeUpdate("insert into survey (pk_column,name ) values (2,null)"); st.executeUpdate("insert into survey (pk_column,name ) values (3,'Tom')"); // Primary key pk_column must be specified // so that the result set is updatable ResultSet rs = st.executeQuery("SELECT pk_column FROM survey"); rs.close();/*from www . j a v a 2 s. c o m*/ st.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,name varchar(30));"); st.executeUpdate("insert into survey (id,name ) values (10,'nameValue')"); st.executeUpdate("insert into survey (id,name ) values (null,null)"); st = conn.createStatement();//w w w . j a v a 2s . c o m ResultSet rs = st.executeQuery("SELECT * FROM survey"); while (rs.next()) { int id = rs.getInt(1); System.out.println(id); if (rs.wasNull()) { id = -1; } System.out.println(id); } 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')"); ResultSet rs = null;/*from w w w. java 2s . c o m*/ DatabaseMetaData meta = conn.getMetaData(); rs = meta.getTables(null, null, null, new String[] { "TABLE", "VIEW" }); while (rs.next()) { String tableOrViewName = rs.getString("TABLE_NAME"); System.out.println("getTableNames(): tableOrViewName=" + tableOrViewName); } 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 id,name FROM survey"); // Use the result set // Retrieve the current values of the row from the database rs.refreshRow();/*w w w . java2 s. c o m*/ rs.close(); st.close(); conn.close(); }
From source file:TestAccessExcel.java
public static void main(String args[]) { Connection conn = null;/*from w ww .jav a 2s .c om*/ Statement stmt = null; ResultSet rs = null; try { conn = getConnection(); stmt = conn.createStatement(); String excelQuery = "select * from [Sheet1$]"; rs = stmt.executeQuery(excelQuery); while (rs.next()) { System.out.println(rs.getString("BadgeNumber") + " " + rs.getString("FirstName") + " " + rs.getString("LastName")); } } catch (Exception e) { System.err.println(e.getMessage()); } finally { try { rs.close(); stmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }