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 { String url = "jdbc:mysql://localhost/sampledb"; String username = "root"; String password = ""; Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection(url, username, password); String sql = "DELETE FROM users WHERE user_id = ?"; int userId = 2; PreparedStatement statement = connection.prepareStatement(sql); statement.setInt(1, userId);/*from www. j a va2s. c o m*/ int rows = statement.executeUpdate(); System.out.println(rows + " record(s) deleted."); connection.close(); }
From source file:TestOCIApp.java
public static void main(String args[]) throws ClassNotFoundException, SQLException { Class.forName("oracle.jdbc.driver.OracleDriver"); // or you can use: // DriverManager.registerDriver( // new oracle.jdbc.driver.OracleDriver()); Connection conn = DriverManager.getConnection("jdbc:oracle:oci8:@dssnt01", "scott", "tiger"); Statement stmt = conn.createStatement(); ResultSet rset = stmt.executeQuery("select 'Hello OCI driver tester '||USER||'!' result from dual"); while (rset.next()) System.out.println(rset.getString(1)); rset.close();//from w ww . j a v a2 s .co m stmt.close(); conn.close(); }
From source file:TestThinApp.java
public static void main(String args[]) throws ClassNotFoundException, SQLException { Class.forName("oracle.jdbc.driver.OracleDriver"); // DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@dssw2k01:1521:orcl", "scott", "tiger"); Statement stmt = conn.createStatement(); ResultSet rset = stmt.executeQuery("select 'Hello Thin driver tester '||USER||'!' result from dual"); while (rset.next()) System.out.println(rset.getString(1)); rset.close();// w w w. j a va 2s. c o m stmt.close(); conn.close(); }
From source file:TestDSLookUp.java
public static void main(String[] args) throws SQLException, NamingException { Context ctx = null;//from ww w. j a va 2 s .com try { Properties prop = new Properties(); prop.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); prop.setProperty(Context.PROVIDER_URL, "file:/JNDI/JDBC"); ctx = new InitialContext(prop); } catch (NamingException ne) { System.err.println(ne.getMessage()); } DataSource ds = (DataSource) ctx.lookup("joe"); Connection conn = ds.getConnection(); Statement stmt = conn.createStatement(); ResultSet rset = stmt.executeQuery( "select 'Hello Thin driver data source tester '||" + "initcap(USER)||'!' result from dual"); if (rset.next()) System.out.println(rset.getString(1)); rset.close(); stmt.close(); conn.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { int records = 0; Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root"); String sql = "SELECT COUNT(*) FROM mytable"; PreparedStatement prest = con.prepareStatement(sql); ResultSet rs = prest.executeQuery(); while (rs.next()) { records = rs.getInt(1);/* w ww. j ava 2s . c o m*/ } System.out.println("Number of records: " + records); con.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.isBeforeFirst()) { System.out.println("beginning"); }/*from w w w .j a va 2s . c o m*/ connection.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection conn = null; conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password"); byte[] bkey = "This is some binary stuff".getBytes(); String query = "INSERT INTO keytable (name, `key`) VALUES (?,?)"; PreparedStatement pstmt = conn.prepareStatement(query); pstmt.setString(1, "test"); pstmt.setBytes(2, bkey);/*from w ww. j ava2 s.c o m*/ pstmt.execute(); conn.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.isBeforeFirst()) { System.out.println("isBeforeFirst."); }/*www .j a va2 s. c o m*/ connection.close(); }
From source file:Main.java
public static void main(String args[]) throws Exception { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String URL = "jdbc:odbc:dbName"; Connection conn = DriverManager.getConnection(URL, "user", "passw"); DatabaseMetaData dmd = conn.getMetaData(); ResultSet rs1 = dmd.getSchemas(); while (rs1.next()) { String ss = rs1.getString(1); ResultSet rs2 = dmd.getTables(null, ss, "%", null); while (rs2.next()) System.out.println(rs2.getString(3) + " " + rs2.getString(4)); }//from w w w . j av a 2s .c o m conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); conn.setAutoCommit(false);/* w ww. j a va2 s . c om*/ Statement st = conn.createStatement(); PreparedStatement pstmt = conn.prepareStatement("create table survey (id int, name VARCHAR(30) );"); pstmt.executeUpdate(); ResultSet rs = st.executeQuery("SELECT * FROM survey"); outputResultSet(rs); rs.close(); st.close(); conn.close(); }