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:TestCreateConnectionWithProperties_MySQL.java
public static void main(String[] args) { Connection conn = null; try {//from w ww.j ava2s. c o m // get connection to an Oracle database conn = getConnection(); System.out.println("conn=" + conn); } catch (Exception e) { // handle the exception e.printStackTrace(); System.exit(1); } finally { // release database resources try { conn.close(); } catch (Exception ignore) { } } }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection connection = null; Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost/testdb"; String user = "root"; String password = ""; connection = DriverManager.getConnection(url, user, password); Statement stmt = connection.createStatement(); String sql = "INSERT INTO users (name, address) VALUES ('Foo', 'Street')"; stmt.execute(sql);//from w w w .ja v a 2 s .co m connection.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); conn.setAutoCommit(false);/*w w w.j a v a 2 s .co m*/ Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int,name varchar(30));"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); st = conn.createStatement(); ResultSet rs = st.executeQuery("SELECT * FROM survey"); rs.close(); st.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); DatabaseMetaData dbMeta = conn.getMetaData(); if (dbMeta.supportsResultSetHoldability(ResultSet.HOLD_CURSORS_OVER_COMMIT)) { System.out.println("this database hold cursors over commit"); } else if (dbMeta.supportsResultSetHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT)) { System.out.println("this database close cursors at commit"); }/*from w w w . j ava 2 s . c o m*/ conn.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { String driver = "com.mysql.jdbc.Driver"; String user = "root"; String pass = "root"; Class.forName(driver);//from w ww . j a va2 s . co m Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", user, pass); DatabaseMetaData dbm = con.getMetaData(); String[] types = { "TABLE" }; ResultSet rs = dbm.getTables(null, null, "%", types); System.out.println("Table name:"); while (rs.next()) { String table = rs.getString("TABLE_NAME"); System.out.println(table); con.close(); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root"); con.setAutoCommit(false);// www.j a va2 s . c o m String table1 = "INSERT emp_sal VALUES('v',1200)"; String table2 = "DELETE FROM movies WHERE title = 'r'"; Statement st = con.createStatement(); st.addBatch(table1); st.addBatch(table2); int count[] = st.executeBatch(); con.commit(); con.close(); System.out.println("Successfully!"); }
From source file:TestDataEncryptionIntegrity.java
public static void main(String[] argv) throws Exception { DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); Properties prop = new Properties(); prop.setProperty("user", "scott"); prop.setProperty("password", "tiger"); prop.setProperty("oracle.net.encryption_client", "REQUIRED"); prop.setProperty("oracle.net.encryption_types_client", "( RC4_40 )"); prop.setProperty("oracle.net.crypto_checksum_client", "REQUIRED"); prop.setProperty("oracle.net.crypto_checksum_types_client", "( MD5 )"); Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@dssw2k01:1521:orcl", prop); Statement stmt = conn.createStatement(); ResultSet rset = stmt.executeQuery( "select 'Hello Thin driver Encryption & Integrity " + "tester '||USER||'!' result from dual"); while (rset.next()) System.out.println(rset.getString(1)); rset.close();//from w ww.j a v a 2s . c o m stmt.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection connection = getConnection(); // Do work with connection Statement statement = connection.createStatement(); String selectEmployeesSQL = "SELECT * FROM employees"; ResultSet resultSet = statement.executeQuery(selectEmployeesSQL); while (resultSet.next()) { printEmployee(resultSet);//from w w w . ja v a2 s . c o m } resultSet.close(); statement.close(); connection.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/testdb", "root", ""); Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet resultSet = statement.executeQuery("SELECT * FROM products"); if (resultSet.isAfterLast()) { System.out.println("You are at the beginning of the result set."); }//from w ww. j av a2 s .c o m connection.close(); }
From source file:Main.java
public static void main(String args[]) throws Exception { Connection conn = null; Statement stmt = null;/* ww w .j ava 2s. co m*/ ResultSet rs = null; conn = getConnection(); stmt = conn.createStatement(); String excelQuery = "select * from [Sheet1$]"; rs = stmt.executeQuery(excelQuery); while (rs.next()) { System.out.println(rs.getString("FirstName") + " " + rs.getString("LastName")); } rs.close(); stmt.close(); conn.close(); }