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 = getConnection(); Statement st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); 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,null)"); st = conn.createStatement();/*w w w .j a va2s . c om*/ ResultSet rs = st.executeQuery("SELECT * FROM survey"); while (rs.next()) { String name = rs.getString(2); System.out.println(name); } rs.close(); st.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); Statement stmt = conn.createStatement(); stmt.executeUpdate("create table survey (id int, name BINARY );"); String sql = "INSERT INTO survey (name) VALUES(?)"; PreparedStatement pstmt = conn.prepareStatement(sql); String myData = "some string data ..."; byte[] binaryData = myData.getBytes(); pstmt.setBytes(1, binaryData);//w ww .j a v a 2 s .c o m pstmt.executeUpdate(); ResultSet rs = stmt.executeQuery("SELECT * FROM survey"); while (rs.next()) { System.out.println(rs.getByte("id")); } rs.close(); stmt.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getHSQLConnection(); System.out.println("Got Connection."); Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int,name varchar);"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); ResultSet rs = null;/*from ww w .j a v a 2 s . com*/ DatabaseMetaData meta = conn.getMetaData(); rs = meta.getTables(null, null, null, new String[] { "TABLE" }); while (rs.next()) { String tableName = rs.getString("TABLE_NAME"); System.out.println("tableName=" + tableName); } 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_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); 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,null)"); st = conn.createStatement();// w ww. j a va 2 s . co m ResultSet rs = st.executeQuery("SELECT * FROM survey"); while (rs.next()) { String name = rs.getString("name"); System.out.println(name); } rs.close(); st.close(); conn.close(); }
From source file:ResultSetExample.java
public static void main(String args[]) { try {/*w ww .j a v a 2 s . co m*/ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:Inventory", "", ""); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT SupplierName,ProductName, Price " + "FROM ProductSuppliersView WHERE CategoryName LIKE '%BEVERAGES%' "); while (rs.next()) { String supplier = rs.getString("SupplierName"); String product = rs.getString("ProductName"); int price = rs.getInt("Price"); System.out.println(supplier + " sells " + product + " for $" + price); } stmt.close(); con.close(); } catch (Exception e) { System.out.println(e); } }
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 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);/*from ww w. j a v a2s . co 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 { Connection conn = getConnection(); Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int,myDate TIME);"); String INSERT_RECORD = "insert into survey(id, myDate) values(?, ?)"; PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD); pstmt.setString(1, "1"); java.sql.Time sqlDate = new java.sql.Time(new java.util.Date().getTime()); pstmt.setTime(2, sqlDate);/*from www. j a v a 2 s. com*/ 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 stmt = conn.createStatement(); stmt.executeUpdate("create table survey (id int, name BINARY );"); String sql = "INSERT INTO survey (name) VALUES(?)"; PreparedStatement pstmt = conn.prepareStatement(sql); String myData = "some string data ..."; byte[] binaryData = myData.getBytes(); pstmt.setBytes(1, binaryData);/* w ww. j a v a2s.c o m*/ pstmt.executeUpdate(); ResultSet rs = stmt.executeQuery("SELECT * FROM survey"); while (rs.next()) { System.out.print(rs.getBytes(2).length + " "); } rs.close(); stmt.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName(DRIVER);//ww w .j a va 2 s .com Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); DatabaseMetaData metadata = connection.getMetaData(); int majorVersion = metadata.getDatabaseMajorVersion(); System.out.println("majorVersion = " + majorVersion); int minorVersion = metadata.getDatabaseMinorVersion(); System.out.println("minorVersion = " + minorVersion); String productName = metadata.getDatabaseProductName(); System.out.println("productName = " + productName); String productVersion = metadata.getDatabaseProductVersion(); System.out.println("productVersion = " + productVersion); connection.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); Statement stmt = conn.createStatement(); stmt.executeUpdate("create table survey (id int, name BINARY );"); String sql = "INSERT INTO survey (name) VALUES(?)"; PreparedStatement pstmt = conn.prepareStatement(sql); String myData = "some string data ..."; byte[] binaryData = myData.getBytes(); pstmt.setBytes(1, binaryData);//w ww .ja va 2 s . co m pstmt.executeUpdate(); ResultSet rs = stmt.executeQuery("SELECT * FROM survey"); while (rs.next()) { System.out.print(rs.getBytes("name").length + " "); } rs.close(); stmt.close(); conn.close(); }