List of usage examples for java.sql Statement close
void close() throws SQLException;
Statement
object's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed. 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 www .j a v a 2 s . co m*/ 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(); }
From source file:MainClass.java
public static void main(String[] args) { Connection connection = null; Statement statement = null; try {/*from w ww. jav a 2 s. co m*/ Class.forName("org.gjt.mm.mysql.Driver").newInstance(); String url = "jdbc:mysql://localhost/mysql"; connection = DriverManager.getConnection(url, "username", "password"); statement = connection.createStatement(); String hrappSQL = "CREATE DATABASE hrapp"; statement.executeUpdate(hrappSQL); } catch (Exception e) { e.printStackTrace(); } finally { if (statement != null) { try { statement.close(); } catch (SQLException e) { } } if (connection != null) { try { connection.close(); } catch (SQLException e) { } } } }
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);// www .j a v a 2 s . c o m pstmt.executeUpdate(); ResultSet rs = stmt.executeQuery("SELECT * FROM survey"); while (rs.next()) { System.out.print(rs.getByte(1)); } 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);/* ww w . ja v a 2 s . c om*/ 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:CreateDatabase.java
public static void main(String[] args) { Connection connection = null; Statement statement = null; try {/* ww w .j av a2 s.c o m*/ Class.forName("org.gjt.mm.mysql.Driver").newInstance(); String url = "jdbc:mysql://localhost/mysql"; connection = DriverManager.getConnection(url, "username", "password"); statement = connection.createStatement(); String hrappSQL = "CREATE DATABASE hrapp"; statement.executeUpdate(hrappSQL); } catch (Exception e) { e.printStackTrace(); } finally { if (statement != null) { try { statement.close(); } catch (SQLException e) { } // nothing we can do } if (connection != null) { try { connection.close(); } catch (SQLException e) { } // nothing we can do } } }
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 (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.executeUpdate("insert into survey (id,name ) values (3,'Tom')"); ResultSet rs = st.executeQuery("SELECT id,name FROM survey"); // Delete the first row rs.first();/*from w ww . j av a 2 s . com*/ rs.deleteRow(); 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_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();//from w w w . j a v a 2 s . c o m 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);//from www .j a v a 2 s .co 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 = getConnection(); conn.setAutoCommit(false);/*from w w w . ja v a 2 s . 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(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();/*from w w w . j a va2 s. c om*/ 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(); }