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 = null;/*w w w.java2s . c o m*/ Statement stmt = null; ResultSet rs = null; conn = getConnection(); stmt = conn.createStatement(); String excelQuery = "select * from [Sheet1$]"; rs = stmt.executeQuery(excelQuery); while (rs.next()) { System.out.println(rs.getString("FirstName") + " " + rs.getString("LastName")); } rs.close(); stmt.close(); conn.close(); }
From source file:Update.java
public static void main(String args[]) { Connection con = null;// w w w . j a va 2s. c o m if (args.length != 2) { System.out.println("Syntax: <java UpdateApp [number] [string]>"); return; } try { String driver = "com.imaginary.sql.msql.MsqlDriver"; Class.forName(driver).newInstance(); String url = "jdbc:msql://carthage.imaginary.com/ora"; con = DriverManager.getConnection(url, "borg", ""); Statement s = con.createStatement(); String test_id = args[0]; String test_val = args[1]; int update_count = s.executeUpdate( "INSERT INTO test (test_id, test_val) " + "VALUES(" + test_id + ", '" + test_val + "')"); System.out.println(update_count + " rows inserted."); s.close(); } catch (Exception e) { e.printStackTrace(); } finally { if (con != null) { try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getOracleConnection(); Statement stmt = null; ResultSet rs = null;/*from w ww. j a v a 2 s . c o m*/ stmt = conn.createStatement(); //only for Oracle rs = stmt.executeQuery("select object_name from user_objects where object_type = 'VIEW'"); while (rs.next()) { String tableName = rs.getString(1); System.out.println("viewName=" + tableName); } stmt.close(); conn.close(); }
From source file:MainClass.java
public static void main(String[] arguments) { String data = "jdbc:odbc:YourSettings"; try {//from w w w .j a v a 2s . c o m Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection conn = DriverManager.getConnection(data, "", ""); Statement st = conn.createStatement(); ResultSet rec = st .executeQuery("SELECT * FROM Coal WHERE (Country='" + arguments[0] + "') ORDER BY Year"); while (rec.next()) { System.out.println(rec.getString(1) + "\t" + rec.getString(2) + "\t\t" + rec.getString(3) + "\t" + rec.getString(4)); } st.close(); } catch (Exception e) { System.out.println("Error: " + e.toString() + e.getMessage()); } }
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 CHAR(5) );"); stmt.executeUpdate("INSERT INTO survey(id, name)VALUES(111, '123456789')"); ResultSet rs = stmt.executeQuery("SELECT * FROM survey"); outputResultSet(rs);//w ww . j av a 2 s . c o m stmt.executeUpdate("drop table survey"); rs.close(); stmt.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { try {/*from ww w . ja v a 2 s . c o m*/ String url = "jdbc:odbc:yourdatabasename"; String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; String user = "guest"; String password = "guest"; Class.forName(driver); Connection connection = DriverManager.getConnection(url, user, password); Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet rs = stmt.executeQuery("SELECT * from DUMMY"); System.out.println(rs.getType()); System.out.println(rs.getConcurrency()); rs.deleteRow(); rs.close(); rs.close(); stmt.close(); connection.close(); } catch (Exception e) { System.err.println(e); } }
From source file:UpdateLogic.java
public static void main(String args[]) { Connection con = null;//w ww. j a va2s . c om if (args.length != 2) { System.out.println("Syntax: <java UpdateLogic [number] [string]>"); return; } try { String driver = "com.imaginary.sql.msql.MsqlDriver"; Class.forName(driver).newInstance(); String url = "jdbc:msql://carthage.imaginary.com/ora"; Statement s; con = DriverManager.getConnection(url, "borg", ""); con.setAutoCommit(false); // make sure auto commit is off! s = con.createStatement();// create the first statement s.executeUpdate("INSERT INTO test (test_id, test_val) " + "VALUES(" + args[0] + ", '" + args[1] + "')"); s.close(); // close the first statement s = con.createStatement(); // create the second statement s.executeUpdate("INSERT into test_desc (test_id, test_desc) " + "VALUES(" + args[0] + ", 'This describes the test.')"); con.commit(); // commit the two statements System.out.println("Insert succeeded."); s.close(); // close the second statement } catch (Exception e) { if (con != null) { try { con.rollback(); } // rollback on error catch (SQLException e2) { } } e.printStackTrace(); } finally { if (con != null) { try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
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 rs = null;/*from w w w .j av a 2s. c om*/ DatabaseMetaData meta = conn.getMetaData(); System.out.println(meta.getDriverName()); 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')"); ResultSet rs = null;// w w w .j a v a 2 s . c o m DatabaseMetaData meta = conn.getMetaData(); System.out.println(meta.getDriverVersion()); 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, b BLOB);"); PreparedStatement pstmt = conn.prepareStatement("INSERT INTO survey VALUES(1,?)"); File file = new File("blob.txt"); FileInputStream fis = new FileInputStream(file); pstmt.setBinaryStream(1, fis, (int) file.length()); pstmt.execute();//from w w w.j a v a2 s. co m fis.close(); st.close(); conn.close(); }