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 = getConnection(); Statement st = conn.createStatement(); 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')"); st = conn.createStatement();/* w w w. j a va 2 s .co m*/ ResultSet rs = st.executeQuery("SELECT * FROM survey"); rs = st.executeQuery("SELECT COUNT(*) FROM survey"); // get the number of rows from the result set rs.next(); int rowCount = rs.getInt(1); System.out.println(rowCount); rs.close(); st.close(); conn.close(); }
From source file:GetColumnNamesFromResultSet_Oracle.java
public static void main(String[] args) { Connection conn = null;/*from w ww .j a va 2s .c o m*/ Statement stmt = null; ResultSet rs = null; try { conn = getConnection(); // prepare query String query = "select id, name, age from employees"; // create a statement stmt = conn.createStatement(); // execute query and return result as a ResultSet rs = stmt.executeQuery(query); // get the column names from the ResultSet getColumnNames(rs); } catch (Exception e) { e.printStackTrace(); } finally { // release database resources try { rs.close(); stmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }
From source file:Main.java
public static void main(String[] args) throws SQLException { // DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); Connection conn = DriverManager.getConnection("jdbc:oracle:oci8:@yourDB", "scott", "tiger"); Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); stmt.setFetchSize(1);//from w w w . ja va 2s.com ResultSet rset = stmt.executeQuery("select EMPNO, ENAME, SAL from EMP"); showProperty(rset); while (rset.next()) { System.out.println(rset.getInt(1) + " " + rset.getString(2) + " " + rset.getInt(3)); } doSomeChanges(conn); // Place the cursor before the first row rset.beforeFirst(); while (rset.next()) { System.out.println(rset.getInt(1) + " " + rset.getString(2) + " " + rset.getInt(3)); } rset.close(); stmt.close(); cleanup(conn); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getMySQLConnection(); Statement stmt = null; try {/*from w ww.j a va 2s. com*/ stmt = conn.createStatement(); stmt.executeUpdate("DELETE FROM Mytable"); displayError(stmt.getWarnings()); stmt.executeUpdate( "INSERT INTO Mytable(id, name)" + "VALUES(1, 'NameLongerThanColumnLengthInDatabase')"); displayError(stmt.getWarnings()); } catch (DataTruncation dt) { displayError(dt); dt.printStackTrace(); } catch (SQLException se) { System.out.println("Database error message: " + se.getMessage()); } catch (Exception e) { e.printStackTrace(); } finally { stmt.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) );"); String INSERT_RECORD = "insert into survey(id, name) values(?,?)"; PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD); pstmt.setString(1, "1"); pstmt.setString(2, "name1"); pstmt.executeUpdate();//from w ww.ja v a2 s. co m ResultSet rs = st.executeQuery("SELECT * FROM survey"); outputResultSet(rs); pstmt.setString(1, "2"); pstmt.setString(2, "name2"); 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 { String url = "jdbc:odbc:technical_library"; String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; String theStatement = "SELECT authid, lastname, firstname, email FROM authors ORDER BY authid"; try {/*from w w w . j av a2 s. c om*/ Class.forName(driver); Connection connection = DriverManager.getConnection(url, "guest", "guest"); Statement queryAuthors = connection.createStatement(); ResultSet results = queryAuthors.executeQuery(theStatement); String lastname, firstname, email; int id; while (results.next()) { id = results.getInt(1); lastname = results.getString(2); firstname = results.getString(3); email = results.getString(4); if (results.wasNull()) { email = "no email"; } System.out.println(Integer.toString(id) + ", " + lastname.trim() + ", " + firstname.trim() + ", " + email.trim()); } queryAuthors.close(); } catch (Exception e) { System.err.println(e); } }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = null;/*from w ww .ja va 2 s. c o m*/ Statement stmt = null; 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 = "INSERT INTO Person VALUES (1, 'A', 'B', 18)"; stmt.executeUpdate(sql); sql = "INSERT INTO Person VALUES (2, 'C', 'D', 25)"; stmt.executeUpdate(sql); sql = "INSERT INTO Person VALUES (3, 'E', 'F', 30)"; stmt.executeUpdate(sql); sql = "INSERT INTO Person VALUES(4, 'S', 'M', 28)"; stmt.executeUpdate(sql); stmt.close(); conn.close(); }
From source file:MainClass.java
public static void main(String args[]) { Connection conn = null;/* ww w . jav a 2 s . c o m*/ Statement stmt = null; ResultSet rs = null; try { conn = getConnection(); stmt = conn.createStatement(); String query = "select EmployeeID, LastName, FirstName from Employees"; rs = stmt.executeQuery(query); while (rs.next()) { System.out.println(rs.getString("EmployeeID") + " " + rs.getString("LastName") + " " + rs.getString("FirstName")); } } catch (Exception e) { // handle the exception e.printStackTrace(); System.err.println(e.getMessage()); } finally { try { rs.close(); stmt.close(); conn.close(); } catch (Exception ee) { ee.printStackTrace(); } } }
From source file:GetColumnNamesFromResultSet_MySQL.java
public static void main(String[] args) { Connection conn = null;/* ww w .j a va 2 s .co m*/ Statement stmt = null; ResultSet rs = null; try { conn = getConnection(); // prepare query String query = "select id, name, age from employees"; // create a statement stmt = conn.createStatement(); // execute query and return result as a ResultSet rs = stmt.executeQuery(query); // get the column names from the ResultSet getColumnNames(rs); } catch (Exception e) { e.printStackTrace(); System.exit(1); } finally { // release database resources try { rs.close(); stmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }
From source file:DemoDataTruncation.java
public static void main(String[] args) throws Exception { Connection conn = getMySQLConnection(); Statement stmt = null; try {//from w w w.ja va 2 s .c o m stmt = conn.createStatement(); stmt.executeUpdate("DELETE FROM animals_table"); displayError(stmt.getWarnings()); stmt.executeUpdate( "INSERT INTO animals_table(id, name)" + "VALUES(1, 'NameLongerThanColumnLengthInDatabase')"); displayError(stmt.getWarnings()); } catch (DataTruncation dt) { displayError(dt); dt.printStackTrace(); } catch (SQLException se) { System.out.println("Database error message: " + se.getMessage()); } catch (Exception e) { e.printStackTrace(); } finally { stmt.close(); conn.close(); } }