List of usage examples for java.sql ResultSet close
void close() throws SQLException;
ResultSet
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 = getHSQLConnection(); conn.setAutoCommit(true);/*w w w. j a v a 2 s . co m*/ 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 = conn.createStatement(); ResultSet rs = st.executeQuery("SELECT * FROM survey"); ResultSetMetaData rsMetaData = rs.getMetaData(); int numberOfColumns = rsMetaData.getColumnCount(); System.out.println("resultSet MetaData column Count=" + numberOfColumns); rs.close(); st.close(); conn.close(); }
From source file:JavaDBDemo.java
public static void main(String[] args) { String driver = "org.apache.derby.jdbc.EmbeddedDriver"; String connectionURL = "jdbc:derby:myDatabase;create=true"; String createString = "CREATE TABLE Employee (NAME VARCHAR(32) NOT NULL, ADDRESS VARCHAR(50) NOT NULL)"; try {// w ww .j a v a2s . c o m Class.forName(driver); } catch (java.lang.ClassNotFoundException e) { e.printStackTrace(); } try { conn = DriverManager.getConnection(connectionURL); Statement stmt = conn.createStatement(); stmt.executeUpdate(createString); PreparedStatement psInsert = conn.prepareStatement("insert into Employee values (?,?)"); psInsert.setString(1, args[0]); psInsert.setString(2, args[1]); psInsert.executeUpdate(); Statement stmt2 = conn.createStatement(); ResultSet rs = stmt2.executeQuery("select * from Employee"); int num = 0; while (rs.next()) { System.out.println(++num + ": Name: " + rs.getString(1) + "\n Address" + rs.getString(2)); } rs.close(); } catch (Exception e) { e.printStackTrace(); } }
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 ww w .j a v a 2 s . co m*/ 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(); 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 ww . j av a 2 s . c o 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: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.executeUpdate("insert into survey (id,name ) values (3,'Tom')"); st = conn.createStatement();/*from www .j av a2 s.c om*/ 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: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 w w . java2s.c om*/ stmt.executeUpdate("update survey set name='newName' where id = 111"); 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 { 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 www .j a v a 2 s. c o m*/ 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 con = DriverManager.getConnection("jdbc:h2:mem:"); Statement s = con.createStatement(); s.execute("CREATE TABLE Table1 (Column1 CLOB)"); InputStream is = new FileInputStream("data.txt"); Reader rdr = new InputStreamReader(is, StandardCharsets.ISO_8859_1); PreparedStatement ps = con.prepareStatement("INSERT INTO Table1 (Column1) VALUES (?)"); ps.setCharacterStream(1, rdr);//from w ww . j a va2 s . co m ps.executeUpdate(); ResultSet rs = s.executeQuery("SELECT Column1 FROM Table1"); int rowNumber = 0; while (rs.next()) { String str = rs.getString("Column1"); System.out.println(String.format("Row %d: CLOB is %d character(s) long.", ++rowNumber, str.length())); } rs.close(); con.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Statement stmt = DriverManager.getConnection("jdbc:odbc:employee").createStatement(); ResultSet rs = stmt.executeQuery("select lastname, firstname, id from [Sheet1$]"); while (rs.next()) { String lname = rs.getString(1); String fname = rs.getString(2); int id = rs.getInt(3); System.out.println(fname + " " + lname + " id : " + id); }//ww w. j ava2 s. c o m rs.close(); stmt.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection dbConnection = null; String myConnectionString = ""; myConnectionString = "jdbc:mysql://192.168.1.3:3306/mytestdb"; dbConnection = DriverManager.getConnection(myConnectionString, "root", "whatever"); PreparedStatement stmt = dbConnection.prepareStatement("SELECT * FROM jdbctest"); ResultSet rs = stmt.executeQuery(); int i = 0;//from w ww. j a va2 s .c om int j = 0; String s = ""; while (rs.next()) { i++; j = rs.getInt("id"); s = rs.getString("textcol"); } System.out.println(String.format("Finished reading %d rows.", i)); rs.close(); stmt.close(); dbConnection.close(); }