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(DRIVER);//w ww. ja v a 2 s . c o m Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); DatabaseMetaData metadata = connection.getMetaData(); boolean updatable = metadata.supportsResultSetConcurrency(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE); System.out.println("Updatable ResultSet supported = " + updatable); connection.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"); String sql = "DELETE FROM product where year_made = ?"; PreparedStatement prest = con.prepareStatement(sql); prest.setInt(1, 2008);//from w w w . j av a2 s . c om int del = prest.executeUpdate(); System.out.println("Number of deleted records: " + del); con.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { String driver = "oracle.jdbc.driver.OracleDriver"; Class.forName(driver).newInstance(); System.out.println("Connecting to database..."); String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORCL"; Connection conn = DriverManager.getConnection(jdbcUrl, "yourName", "mypwd"); CallableStatement cstmt = conn.prepareCall("{call getEmpName (?,?)}"); cstmt.setInt(1, 111111111);/*w w w . j ava 2 s .c om*/ cstmt.registerOutParameter(2, java.sql.Types.VARCHAR); cstmt.execute(); String empName = cstmt.getString(2); System.out.println(empName); conn.close(); }
From source file:Main.java
public static void main(String args[]) throws Exception { Class.forName("oracle.jdbc.driver.OracleDriver"); String url = "jdbc:oracle:thin:@localhost:1521:XE"; String userName = "suru"; String password = "password"; Connection con = DriverManager.getConnection(url, userName, password); System.out.println("Connection success!"); Statement stmt = con.createStatement(); String sql = "CREATE TABLE EMP ( ID NUMBER(5) PRIMARY KEY, NAME VARCHAR2(50))"; stmt.execute(sql);/* w w w. j a va 2 s. com*/ System.out.println("Table created successfully!"); stmt.close(); con.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); DatabaseMetaData dbmd = conn.getMetaData(); if (dbmd.supportsResultSetConcurrency(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE)) { System.out.println("Updatable ResultSets are supported"); } else {/*w w w.ja v a 2s. c o m*/ System.out.println("Updatable ResultSets are not supported"); } conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = null; Statement stmt = null;/*from ww w .jav a2 s .co m*/ Class.forName(JDBC_DRIVER); conn = DriverManager.getConnection(DB_URL, USER, PASS); stmt = conn.createStatement(); String sql = "CREATE DATABASE STUDENTS"; stmt.executeUpdate(sql); System.out.println("Database created successfully..."); stmt.close(); conn.close(); }
From source file:JndiDataSource.java
public static void main(String args[]) throws Exception { String sp = "com.sun.jndi.fscontext.RefFSContextFactory"; String file = "file:/e:/JNDI"; String dataSourceName = "jdbc/myDatabase"; Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, sp); env.put(Context.PROVIDER_URL, file); ctx = new InitialContext(env); bindDataSource(ctx, dataSourceName); DataSource ds = null;//from w w w . j a va 2 s . c o m ds = (DataSource) ctx.lookup(dataSourceName); Connection conn = ds.getConnection(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT Name FROM Employees"); while (rs.next()) System.out.println(rs.getString("name")); ctx.close(); rs.close(); stmt.close(); conn.close(); ctx.close(); conn.close(); }
From source file:TestThinDSApp.java
public static void main(String args[]) throws ClassNotFoundException, SQLException { // These settings are typically configured in JNDI // so they a implementation specific OracleDataSource ds = new OracleDataSource(); ds.setDriverType("thin"); ds.setServerName("dssw2k01"); ds.setPortNumber(1521);//from w ww . ja v a2 s .c o m ds.setDatabaseName("orcl"); // sid ds.setUser("scott"); ds.setPassword("tiger"); 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:DeleteRecordsUsingPreparedStatement.java
public static void main(String[] args) throws Exception { Connection conn = null; PreparedStatement pstmt = null; try {//from ww w . java 2 s. c om conn = getConnection(); String query = "delete from tableName"; pstmt = conn.prepareStatement(query); pstmt.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { pstmt.close(); conn.close(); } }
From source file:Main.java
License:asdf
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"); String sql = "INSERT product VALUES(?,?)"; PreparedStatement prest = con.prepareStatement(sql); prest.setString(1, "asdf"); prest.setInt(2, 2009);//from w w w . j a v a 2 s . c o m int count = prest.executeUpdate(); System.out.println(count + "row(s) affected"); con.close(); }