List of usage examples for java.sql Connection close
void close() throws SQLException;
Connection
object's database and JDBC resources immediately instead of waiting for them to be automatically released. From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName("org.sqlite.JDBC"); Connection conn = DriverManager.getConnection("jdbc:sqlite:test.db"); Statement stat = conn.createStatement(); stat.executeUpdate("drop table if exists people;"); stat.executeUpdate("create table people (name, occupation);"); PreparedStatement prep = conn.prepareStatement("insert into people values (?, ?);"); prep.setString(1, "G"); prep.setString(2, "politics"); prep.addBatch();/* w w w .j a v a 2 s. c o m*/ prep.setString(1, "Turing"); prep.setString(2, "computers"); prep.addBatch(); prep.setString(1, "W"); prep.setString(2, "Tester"); prep.addBatch(); conn.setAutoCommit(false); prep.executeBatch(); conn.setAutoCommit(true); ResultSet rs = stat.executeQuery("select * from people;"); while (rs.next()) { System.out.println("name = " + rs.getString("name")); System.out.println("job = " + rs.getString("occupation")); } rs.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getOracleConnection(); // Step-2: identify the stored procedure String proc3StoredProcedure = "{ call proc3(?, ?, ?) }"; // Step-3: prepare the callable statement CallableStatement cs = conn.prepareCall(proc3StoredProcedure); // Step-4: set input parameters ... // first input argument cs.setString(1, "abcd"); // third input argument cs.setInt(3, 10);// w ww . j a v a 2 s .c o m // Step-5: register output parameters ... cs.registerOutParameter(2, java.sql.Types.VARCHAR); cs.registerOutParameter(3, java.sql.Types.INTEGER); // Step-6: execute the stored procedures: proc3 cs.execute(); // Step-7: extract the output parameters // get parameter 2 as output String param2 = cs.getString(2); // get parameter 3 as output int param3 = cs.getInt(3); System.out.println("param2=" + param2); System.out.println("param3=" + param3); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); conn.setAutoCommit(false);//from w w w.ja va 2s . c om Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int, name VARCHAR(30) );"); String INSERT_RECORD = "insert into survey(id, name) values(?,?)"; PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD); pstmt.setInt(1, 1); pstmt.setString(2, "name"); pstmt.executeUpdate(); // Get warnings on PreparedStatement object SQLWarning warning = pstmt.getWarnings(); while (warning != null) { // Process statement warnings... String message = warning.getMessage(); String sqlState = warning.getSQLState(); int errorCode = warning.getErrorCode(); warning = warning.getNextWarning(); } ResultSet rs = st.executeQuery("SELECT * FROM survey"); outputResultSet(rs); rs.close(); st.close(); conn.close(); }
From source file:TestDatabaseMetaDataToolDatabaseInformation.java
public static void main(String[] args) throws Exception { Connection conn = getMySqlConnection(); DatabaseMetaData meta = conn.getMetaData(); // Oracle (and some other vendors) do not support // some the following methods; therefore, we need // to use try-catch block. try {//from w ww .java2 s. c o m int majorVersion = meta.getDatabaseMajorVersion(); System.out.println("major Version: " + majorVersion); } catch (Exception e) { System.out.println("major Version: unsupported feature"); } try { int minorVersion = meta.getDatabaseMinorVersion(); System.out.println("minorVersion" + minorVersion); } catch (Exception e) { System.out.println("minorVersion unsupported feature"); } String productName = meta.getDatabaseProductName(); String productVersion = meta.getDatabaseProductVersion(); System.out.println("productName" + productName); System.out.println("productVersion" + productVersion); conn.close(); }
From source file:DemoPreparedStatementSetClob.java
public static void main(String[] args) throws Exception { String id = "0001"; String newID = "0002"; ResultSet rs = null;//w ww . j a v a 2 s. c o m Connection conn = null; PreparedStatement pstmt = null; try { conn = getConnection(); // begin transaction conn.setAutoCommit(false); String query1 = "select clob_column from clob_table where id = ?"; pstmt = conn.prepareStatement(query1); pstmt.setString(1, id); rs = pstmt.executeQuery(); rs.next(); java.sql.Clob clob = (java.sql.Clob) rs.getObject(1); String query = "insert into clob_table(id, clob_column) values(?, ?)"; pstmt = conn.prepareStatement(query); pstmt.setString(1, newID); pstmt.setClob(2, clob); // execute query, and return number of rows created int rowCount = pstmt.executeUpdate(); System.out.println("rowCount=" + rowCount); // end transaction conn.commit(); } finally { rs.close(); pstmt.close(); conn.close(); } }
From source file:MainClass.java
public static void main(String[] args) { Connection connection = null; Statement statement = null;//from ww w . ja v a 2 s.co m try { Class.forName("org.gjt.mm.mysql.Driver").newInstance(); String url = "jdbc:mysql://localhost/hrapp"; connection = DriverManager.getConnection(url, "username", "password"); statement = connection.createStatement(); String employees1SQL = "UPDATE employees SET " + "num_dependants = 4 " + "WHERE employee_id = 123456"; statement.executeUpdate(employees1SQL); String employees2SQL = "UPDATE employees SET " + "num_dependants = 4 " + "WHERE employee_id = 123457"; statement.executeUpdate(employees2SQL); } catch (Exception e) { e.printStackTrace(); } finally { if (statement != null) { try { statement.close(); } catch (SQLException e) { } } if (connection != null) { try { connection.close(); } catch (SQLException e) { } } } }
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();/*from w w w. j a 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 nullability of values // in the designated column. System.out.println(rsMetaData.isNullable(i)); } st.close(); conn.close(); }
From source file:Select.java
public static void main(String args[]) { String url = "jdbc:msql://carthage.imaginary.com/ora"; Connection con = null; try {/*from www. j a va2 s. com*/ String driver = "com.imaginary.sql.msql.MsqlDriver"; Class.forName(driver).newInstance(); } catch (Exception e) { System.out.println("Failed to load mSQL driver."); return; } try { con = DriverManager.getConnection(url, "borg", ""); Statement select = con.createStatement(); ResultSet result = select.executeQuery("SELECT test_id, test_val FROM test"); System.out.println("Got results:"); while (result.next()) { // process results one row at a time int key = result.getInt(1); String val = result.getString(2); System.out.println("key = " + key); System.out.println("val = " + val); } } catch (Exception e) { e.printStackTrace(); } finally { if (con != null) { try { con.close(); } catch (Exception e) { e.printStackTrace(); } } } }
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 ww. ja v a2s.c om 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 whether a write on the designated // column will succeed. System.out.println(rsMetaData.isWritable(i)); } st.close(); conn.close(); }
From source file:InsertDateToOracle.java
public static void main(String args[]) throws Exception { String INSERT_RECORD = "insert into TestDates(id, date_column, " + "time_column, timestamp_column) values(?, ?, ?, ?)"; Connection conn = null; PreparedStatement pstmt = null; try {/*from www . j ava2 s.co m*/ conn = getConnection(); pstmt = conn.prepareStatement(INSERT_RECORD); pstmt.setString(1, "001"); java.util.Date date = new java.util.Date(); long t = date.getTime(); java.sql.Date sqlDate = new java.sql.Date(t); java.sql.Time sqlTime = new java.sql.Time(t); java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(t); System.out.println("sqlDate=" + sqlDate); System.out.println("sqlTime=" + sqlTime); System.out.println("sqlTimestamp=" + sqlTimestamp); pstmt.setDate(2, sqlDate); pstmt.setTime(3, sqlTime); pstmt.setTimestamp(4, sqlTimestamp); pstmt.executeUpdate(); } catch (Exception e) { e.printStackTrace(); System.out.println("Failed to insert the record."); } finally { pstmt.close(); conn.close(); } }