List of usage examples for java.sql Statement executeQuery
ResultSet executeQuery(String sql) throws SQLException;
ResultSet
object. From source file:PoolingDriverExample.java
public static void main(String[] args) { ////from w ww . j a va 2 s . c o m // First we load the underlying JDBC driver. // You need this if you don't use the jdbc.drivers // system property. // System.out.println("Loading underlying JDBC driver."); try { Class.forName("org.h2.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } System.out.println("Done."); // // Then we set up and register the PoolingDriver. // Normally this would be handled auto-magically by // an external configuration, but in this example we'll // do it manually. // System.out.println("Setting up driver."); try { setupDriver(args[0]); } catch (Exception e) { e.printStackTrace(); } System.out.println("Done."); // // Now, we can use JDBC as we normally would. // Using the connect string // jdbc:apache:commons:dbcp:example // The general form being: // jdbc:apache:commons:dbcp:<name-of-pool> // Connection conn = null; Statement stmt = null; ResultSet rset = null; try { System.out.println("Creating connection."); conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:example"); System.out.println("Creating statement."); stmt = conn.createStatement(); System.out.println("Executing statement."); rset = stmt.executeQuery(args[1]); System.out.println("Results:"); int numcols = rset.getMetaData().getColumnCount(); while (rset.next()) { for (int i = 1; i <= numcols; i++) { System.out.print("\t" + rset.getString(i)); } System.out.println(""); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (rset != null) rset.close(); } catch (Exception e) { } try { if (stmt != null) stmt.close(); } catch (Exception e) { } try { if (conn != null) conn.close(); } catch (Exception e) { } } // Display some pool statistics try { printDriverStats(); } catch (Exception e) { e.printStackTrace(); } // closes the pool try { shutdownDriver(); } catch (Exception e) { e.printStackTrace(); } }
From source file:ManualPoolingDriverExample.java
public static void main(String[] args) { ////from w w w .j a v a2s.com // First we load the underlying JDBC driver. // You need this if you don't use the jdbc.drivers // system property. // System.out.println("Loading underlying JDBC driver."); try { Class.forName("oracle.jdbc.driver.OracleDriver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } System.out.println("Done."); // // Then we set up and register the PoolingDriver. // Normally this would be handled auto-magically by // an external configuration, but in this example we'll // do it manually. // System.out.println("Setting up driver."); try { setupDriver(args[0]); } catch (Exception e) { e.printStackTrace(); } System.out.println("Done."); // // Now, we can use JDBC as we normally would. // Using the connect string // jdbc:apache:commons:dbcp:example // The general form being: // jdbc:apache:commons:dbcp:<name-of-pool> // Connection conn = null; Statement stmt = null; ResultSet rset = null; try { System.out.println("Creating connection."); conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:example"); System.out.println("Creating statement."); stmt = conn.createStatement(); System.out.println("Executing statement."); rset = stmt.executeQuery(args[1]); System.out.println("Results:"); int numcols = rset.getMetaData().getColumnCount(); while (rset.next()) { for (int i = 1; i <= numcols; i++) { System.out.print("\t" + rset.getString(i)); } System.out.println(""); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (rset != null) rset.close(); } catch (Exception e) { } try { if (stmt != null) stmt.close(); } catch (Exception e) { } try { if (conn != null) conn.close(); } catch (Exception e) { } } // Display some pool statistics try { printDriverStats(); } catch (Exception e) { e.printStackTrace(); } // closes the pool try { shutdownDriver(); } catch (Exception e) { e.printStackTrace(); } }
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); // prepare small binary stream File smallFile = new File("yourFileName.txt"); int smallFileLength = (int) smallFile.length(); InputStream smallStream = (InputStream) new FileInputStream(smallFile); pstmt.setBinaryStream(2, smallStream, smallFileLength); // insert the data pstmt.executeUpdate();//from w w w. j a v a 2 s.c om ResultSet rs = stmt.executeQuery("SELECT * FROM survey"); while (rs.next()) { System.out.print(rs.getString(1)); } rs.close(); stmt.close(); conn.close(); }
From source file:InsertRow.java
public static void main(String args[]) { String url = "jdbc:mySubprotocol:myDataSource"; Connection con;/*from w w w. j ava 2 s .c om*/ Statement stmt; String query = "select COF_NAME, PRICE from COFFEES"; try { Class.forName("myDriver.ClassName"); } catch (java.lang.ClassNotFoundException e) { System.err.print("ClassNotFoundException: "); System.err.println(e.getMessage()); } try { con = DriverManager.getConnection(url, "myLogin", "myPassword"); stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet uprs = stmt.executeQuery("SELECT * FROM COFFEES"); uprs.moveToInsertRow(); uprs.updateString("COF_NAME", "Kona"); uprs.updateInt("SUP_ID", 150); uprs.updateFloat("PRICE", 10.99f); uprs.updateInt("SALES", 0); uprs.updateInt("TOTAL", 0); uprs.insertRow(); uprs.beforeFirst(); System.out.println("Table COFFEES after insertion:"); while (uprs.next()) { String s = uprs.getString("COF_NAME"); int sup = uprs.getInt("SUP_ID"); float f = uprs.getFloat("PRICE"); int sales = uprs.getInt("SALES"); int t = uprs.getInt("TOTAL"); System.out.print(s + " " + sup + " " + f + " "); System.out.println(sales + " " + t); } uprs.close(); stmt.close(); con.close(); } catch (SQLException ex) { System.err.println("SQLException: " + ex.getMessage()); } }
From source file:mx.com.pixup.portal.demo.DemoDisqueraSelect.java
public static void main(String[] args) { System.out.println("BIENVENIDO A PIXUP"); System.out.println("Mantenimiento catlogo disquera"); System.out.println("Listado de disqueras"); Connection connection = null; Statement statement = null; ResultSet resultSet = null;//w ww .ja v a 2 s . c o m try { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUsername("root"); dataSource.setPassword("admin"); dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/pixup"); connection = dataSource.getConnection(); statement = connection.createStatement(); String sql = "select * from disquera order by nombre desc"; resultSet = statement.executeQuery(sql); System.out.println("ID: \t NOMBRE:"); while (resultSet.next()) { Integer id = resultSet.getInt(1); String nombre = resultSet.getString(2); System.out.println(id + " \t " + nombre); } } catch (Exception e) { System.out.println("Error en el sistema, intente ms tarde!!" + e.getMessage()); } finally { if (resultSet != null) { try { resultSet.close(); } catch (Exception e) { } } if (statement != null) { try { statement.close(); } catch (Exception e) { } } if (connection != null) { try { connection.close(); } catch (Exception e) { } } } }
From source file:TestBatchUpdate.java
public static void main(String args[]) { Connection conn = null;//ww w .j a va 2s. c o m Statement stmt = null; ResultSet rs = null; try { conn = getConnection(); stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); conn.setAutoCommit(false); stmt.addBatch("INSERT INTO batch_table(id, name) VALUES('11', 'A')"); stmt.addBatch("INSERT INTO batch_table(id, name) VALUES('22', 'B')"); stmt.addBatch("INSERT INTO batch_table(id, name) VALUES('33', 'C')"); int[] updateCounts = stmt.executeBatch(); conn.commit(); rs = stmt.executeQuery("SELECT * FROM batch_table"); while (rs.next()) { String id = rs.getString("id"); String name = rs.getString("name"); System.out.println("id=" + id + " name=" + name); } } catch (BatchUpdateException b) { System.err.println("SQLException: " + b.getMessage()); System.err.println("SQLState: " + b.getSQLState()); System.err.println("Message: " + b.getMessage()); System.err.println("Vendor error code: " + b.getErrorCode()); System.err.print("Update counts: "); int[] updateCounts = b.getUpdateCounts(); for (int i = 0; i < updateCounts.length; i++) { System.err.print(updateCounts[i] + " "); } } catch (SQLException ex) { System.err.println("SQLException: " + ex.getMessage()); System.err.println("SQLState: " + ex.getSQLState()); System.err.println("Message: " + ex.getMessage()); System.err.println("Vendor error code: " + ex.getErrorCode()); } catch (Exception e) { System.err.println("Exception: " + e.getMessage()); } finally { try { rs.close(); stmt.close(); conn.close(); } catch (Exception ignore) { } } }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getHSQLConnection(); conn.setAutoCommit(false);//www . j a va2 s . c om Statement st = conn.createStatement(); try { 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')"); // commits all the transactions conn.commit(); } catch (Exception e) { //cancel (roll back) all the transactions conn.rollback(); // to see what went wrong e.printStackTrace(); } st = conn.createStatement(); 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 = getHSQLConnection(); conn.setAutoCommit(false);// w w w . java2 s .co m Statement st = conn.createStatement(); try { 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')"); // commits all the transactions conn.commit(); } catch (Exception e) { // cancel (roll back) all the transactions conn.rollback(); // to see what went wrong e.printStackTrace(); } st = conn.createStatement(); ResultSet rs = st.executeQuery("SELECT * FROM survey"); outputResultSet(rs); rs.close(); st.close(); conn.close(); }
From source file:com.jt.dbcp.example.ManualPoolingDriverExample.java
public static void main(String[] args) { ///*from w w w . j av a 2 s . c o m*/ // First we load the underlying JDBC driver. // You need this if you don't use the jdbc.drivers // system property. // System.out.println("Loading underlying JDBC driver."); try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } System.out.println("Done."); // // Then we set up and register the PoolingDriver. // Normally this would be handled auto-magically by // an external configuration, but in this example we'll // do it manually. // System.out.println("Setting up driver."); try { setupDriver(args[0]); } catch (Exception e) { e.printStackTrace(); } System.out.println("Done."); // // Now, we can use JDBC as we normally would. // Using the connect string // jdbc:apache:commons:dbcp:example // The general form being: // jdbc:apache:commons:dbcp:<name-of-pool> // Connection conn = null; Statement stmt = null; ResultSet rset = null; try { System.out.println("Creating connection."); //pool conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:example"); System.out.println("Creating statement."); stmt = conn.createStatement(); System.out.println("Executing statement."); rset = stmt.executeQuery(args[1]); System.out.println("Results:"); int numcols = rset.getMetaData().getColumnCount(); int count = 0; while (rset.next()) { count++; if (count == 10) { break; } for (int i = 1; i <= numcols; i++) { System.out.print("\t" + rset.getString(i)); } System.out.println(""); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (rset != null) rset.close(); } catch (Exception e) { } try { if (stmt != null) stmt.close(); } catch (Exception e) { } try { if (conn != null) conn.close(); } catch (Exception e) { } } // Display some pool statistics try { printDriverStats(); } catch (Exception e) { e.printStackTrace(); } // closes the pool try { shutdownDriver(); } catch (Exception e) { e.printStackTrace(); } }
From source file:mx.com.pixup.portal.demo.DemoDisqueraUpdate.java
public static void main(String[] args) { System.out.println("BIENVENIDO A PIXUP"); System.out.println("Mantenimiento catlogo disquera"); System.out.println("Actualizacin de Disquera"); InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); Connection connection = null; Statement statement = null; ResultSet resultSet = null;/* w ww .jav a 2 s.com*/ try { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUsername("root"); dataSource.setPassword("admin"); dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/pixup"); connection = dataSource.getConnection(); statement = connection.createStatement(); String sql = "select id, nombre from disquera order by nombre"; resultSet = statement.executeQuery(sql); System.out.println("Id Disquera: \t Nombre Disquera"); while (resultSet.next()) { System.out.println(resultSet.getInt("id") + " \t " + resultSet.getString("nombre")); } System.out.println("Proporcione el id de la disquera a actualizar: "); String idDisquera = br.readLine(); System.out.println("Proporcione el nuevo nombre de la disquera: "); String nombreDisquera = br.readLine(); sql = "update disquera set nombre = '" + nombreDisquera + "' where id = " + idDisquera; statement.execute(sql); System.out.println("Disqueras Actualizadas:"); sql = "select id, nombre from disquera order by nombre desc"; resultSet = statement.executeQuery(sql); System.out.println("Id Disquera: \t Nombre Disquera"); while (resultSet.next()) { System.out.println(resultSet.getInt("id") + " \t " + resultSet.getString("nombre")); } } catch (Exception e) { System.out.println("Error en el sistema, intente ms tarde!!"); } finally { if (resultSet != null) { try { resultSet.close(); } catch (Exception e) { } } if (statement != null) { try { statement.close(); } catch (Exception e) { } } if (connection != null) { try { connection.close(); } catch (Exception e) { } } } }