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[] argv) throws Exception { Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root"); con.setAutoCommit(false);/*from www . j a v a 2 s . c o m*/ String sql = "INSERT product VALUES(?,?)"; PreparedStatement prest = con.prepareStatement(sql); prest.setString(1, "A"); prest.setInt(2, 2002); prest.addBatch(); prest.setString(1, "B"); prest.setInt(2, 1998); prest.addBatch(); prest.setString(1, "C"); prest.setInt(2, 1980); prest.addBatch(); prest.setString(1, "D"); prest.setInt(2, 1975); prest.addBatch(); int count[] = prest.executeBatch(); con.commit(); con.close(); }
From source file:DemoPreparedStatementSetBigDecimal.java
public static void main(String[] args) throws Exception { Connection conn = null; PreparedStatement pstmt = null; String query = null;/*from ww w .j av a2 s . com*/ try { conn = getConnection(); query = "insert into BIG_DECIMAL_TABLE(id, big_decimal) values(?, ?)"; pstmt = conn.prepareStatement(query); pstmt.setString(1, "001"); pstmt.setBigDecimal(2, new java.math.BigDecimal("123456789")); // execute query, and return number of rows created int rowCount = pstmt.executeUpdate(); System.out.println("rowCount=" + rowCount); } catch (Exception e) { e.printStackTrace(); } finally { pstmt.close(); conn.close(); } }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); conn.setAutoCommit(false);//ww w .jav a 2s .com Statement st = conn.createStatement(); st.setFetchSize(1); st.executeUpdate("create table survey (id int,name varchar(30));"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); st.executeUpdate("insert into survey (id,name ) values (2,'nameValue')"); st = conn.createStatement(); ResultSet rs = st.executeQuery("SELECT * FROM survey"); rs.setFetchSize(1); outputResultSet(rs); checkForWarning(rs.getWarnings()); 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) values(?)"; PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD); pstmt.setString(1, "1"); pstmt.executeUpdate();/*from w w w.j a v a 2s.c om*/ ResultSet rs = st.executeQuery("SELECT * FROM survey"); outputResultSet(rs); pstmt.setString(1, "2"); pstmt.executeUpdate(); 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(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); st.executeUpdate("create table survey (pk_column int,name varchar(30));"); st.executeUpdate("insert into survey (pk_column,name ) values (1,'nameValue')"); st.executeUpdate("insert into survey (pk_column,name ) values (2,null)"); st.executeUpdate("insert into survey (pk_column,name ) values (3,'Tom')"); // Primary key pk_column must be specified // so that the result set is updatable ResultSet rs = st.executeQuery("SELECT pk_column FROM survey"); rs.close();/*from w w w . j a v a 2 s .com*/ 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,name varchar(30));"); st.executeUpdate("insert into survey (id,name ) values (10,'nameValue')"); st.executeUpdate("insert into survey (id,name ) values (null,null)"); st = conn.createStatement();//from w w w . j a va 2 s . c om ResultSet rs = st.executeQuery("SELECT * FROM survey"); while (rs.next()) { int id = rs.getInt(1); System.out.println(id); if (rs.wasNull()) { id = -1; } System.out.println(id); } rs.close(); st.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); conn.setAutoCommit(false);//from ww w . j av a 2 s . c om Statement stmt = conn.createStatement(); stmt.executeUpdate("create table survey (id int, name CHAR(5) );"); stmt.executeUpdate("INSERT INTO survey(id, name)VALUES(111, '123456789')"); displayError(stmt.getWarnings()); // try to write more data for the name column. displayError(stmt.getWarnings()); // since there were no errors, commit conn.commit(); ResultSet rs = stmt.executeQuery("SELECT * FROM survey"); outputResultSet(rs); rs.close(); stmt.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { try {/* w w w . ja v a 2 s . c om*/ Connection conn = getConnection(); Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int,myDate DATE );"); String INSERT_RECORD = "insert into survey(id, myDate) values(?, ?)"; PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD); pstmt.setString(1, "1"); java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime()); pstmt.setDate(2, sqlDate); pstmt.executeUpdate(); ResultSet rs = st.executeQuery("SELECT * FROM survey"); rs.close(); st.close(); conn.close(); } catch (SQLException e) { while (e != null) { String errorMessage = e.getMessage(); System.err.println("sql error message:" + errorMessage); // This vendor-independent string contains a code. String sqlState = e.getSQLState(); System.err.println("sql state:" + sqlState); int errorCode = e.getErrorCode(); System.err.println("error code:" + errorCode); // String driverName = conn.getMetaData().getDriverName(); // System.err.println("driver name:"+driverName); // processDetailError(drivername, errorCode); e = e.getNextException(); } } }
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')"); ResultSet catalogs = null;/*from w w w .j a va 2 s.c o m*/ DatabaseMetaData meta = conn.getMetaData(); catalogs = meta.getCatalogs(); while (catalogs.next()) { String catalog = catalogs.getString(1); //"TABLE_CATALOG" System.out.println("catalog: " + catalog); } st.close(); conn.close(); }
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 v a 2 s. 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); st.close(); conn.close(); }