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 { Connection conn = null; Statement stmt = null;//from ww w . ja v a2s .c om 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[] argv) throws Exception { int count = 0; Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root"); String sql = "SELECT title,year_made FROM product WHERE year_made >= ? AND year_made <= ?"; PreparedStatement prest = con.prepareStatement(sql); prest.setInt(1, 2000);/* w w w .j av a2s. c o m*/ prest.setInt(2, 2009); ResultSet rs = prest.executeQuery(); while (rs.next()) { String mov_name = rs.getString(1); int mov_year = rs.getInt(2); count++; System.out.println(mov_name + "\t" + "- " + mov_year); } System.out.println("Number of records: " + count); prest.close(); con.close(); }
From source file:SimpleConnection.java
static public void main(String args[]) { Connection connection = null; if (args.length != 4) { System.out.println("Syntax: java SimpleConnection " + "DRIVER URL UID PASSWORD"); return;// ww w. j av a2 s. co m } try { Class.forName(args[0]).newInstance(); } catch (Exception e) { e.printStackTrace(); return; } try { connection = DriverManager.getConnection(args[1], args[2], args[3]); System.out.println("Connection successful!"); } catch (SQLException e) { e.printStackTrace(); } finally { if (connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection dbConnection = null; String myConnectionString = ""; myConnectionString = "jdbc:mysql://192.168.1.3:3306/mytestdb"; dbConnection = DriverManager.getConnection(myConnectionString, "root", "whatever"); PreparedStatement stmt = dbConnection.prepareStatement("SELECT * FROM jdbctest"); ResultSet rs = stmt.executeQuery(); int i = 0;/*from w ww . j a v a 2s . com*/ int j = 0; String s = ""; while (rs.next()) { i++; j = rs.getInt("id"); s = rs.getString("textcol"); } System.out.println(String.format("Finished reading %d rows.", i)); rs.close(); stmt.close(); dbConnection.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { Connection dbConnection = null; PreparedStatement preparedStatement = null; String createTableSQL = "CREATE TABLE Person(" + "USER_ID NUMBER(5) NOT NULL, " + "USERNAME VARCHAR(20) NOT NULL, " + "CREATED_BY VARCHAR(20) NOT NULL, " + "CREATED_DATE DATE NOT NULL, " + "PRIMARY KEY (USER_ID) " + ")"; Class.forName(DB_DRIVER);//from w w w . jav a 2 s .co m dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD); preparedStatement = dbConnection.prepareStatement(createTableSQL); System.out.println(createTableSQL); preparedStatement.executeUpdate(); preparedStatement.close(); dbConnection.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); Statement stmt = null;//ww w.jav a 2 s . c o m ResultSet rs = null; conn = getConnection(); stmt = conn.createStatement(); stmt.executeUpdate("insert into animals_table (name) values('newName')"); rs = stmt.getGeneratedKeys(); while (rs.next()) { ResultSetMetaData rsMetaData = rs.getMetaData(); int columnCount = rsMetaData.getColumnCount(); for (int i = 1; i <= columnCount; i++) { String key = rs.getString(i); System.out.println("key " + i + " is " + key); } } rs.close(); stmt.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); conn.setAutoCommit(false);/*from w ww .j a v a 2 s . c o m*/ 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); for (int i = 0; i < 10; i++) { pstmt.setInt(1, i); pstmt.setString(2, "name" + i); pstmt.executeUpdate(); } ResultSet 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 = getConnection(); Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int,myDate TIMESTAMP );"); String INSERT_RECORD = "insert into survey(id, myDate) values(?, ?)"; PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD); pstmt.setString(1, "1"); java.sql.Timestamp sqlDate = new java.sql.Timestamp(new java.util.Date().getTime()); pstmt.setTimestamp(2, sqlDate);// www . java 2 s . c o m pstmt.executeUpdate(); ResultSet 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 { try {/*w w w . j a va 2s . co m*/ String url = "jdbc:odbc:databaseName"; String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; String user = "guest"; String password = "guest"; FileInputStream fis = new FileInputStream("somefile.txt"); Class.forName(driver); Connection connection = DriverManager.getConnection(url, user, password); Statement createTable = connection.createStatement(); createTable.executeUpdate("CREATE TABLE source_code (name CHAR(20), source LONGTEXT)"); String ins = "INSERT INTO source_code VALUES(?,?)"; PreparedStatement statement = connection.prepareStatement(ins); statement.setString(1, "TryInputStream"); // Set first field statement.setAsciiStream(2, fis, fis.available()); // Stream is source int rowsUpdated = statement.executeUpdate(); System.out.println("Rows affected: " + rowsUpdated); connection.close(); } catch (Exception e) { System.err.println(e); } }
From source file:DemoDisplayBinaryDataFromDatabase.java
public static void main(String args[]) throws Exception { Connection conn = null; ResultSet rs = null;/*from www .j ava2 s . com*/ PreparedStatement pstmt = null; String query = "SELECT raw_column, long_raw_column FROM binary_table WHERE id = ?"; try { conn = getConnection(); Object[] results = new Object[2]; pstmt = conn.prepareStatement(query); pstmt.setString(1, "0001"); rs = pstmt.executeQuery(); rs.next(); // materialize binary data onto client results[0] = rs.getBytes("RAW_COLUMN"); results[1] = rs.getBytes("LONG_RAW_COLUMN"); } finally { rs.close(); pstmt.close(); conn.close(); } }