List of usage examples for java.sql Connection createStatement
Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException;
Statement
object that will generate ResultSet
objects with the given type and concurrency. From source file:ScrollableRs.java
public static void main(String[] args) throws Exception { Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORCL"; Connection conn = DriverManager.getConnection(jdbcUrl, "yourName", "mypwd"); Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet rs = stmt.executeQuery("SELECT ssn, name, salary FROM EMPLOYEES"); while (rs.next()) { printRow(rs);//from w ww . j av a2 s. c o m } rs.afterLast(); System.out.println("\"After-last-row\" = " + rs.isAfterLast()); rs.beforeFirst(); System.out.println("\"Before-first-row\" = " + rs.isBeforeFirst()); rs.first(); printRow(rs); rs.last(); printRow(rs); rs.previous(); printRow(rs); rs.next(); printRow(rs); rs.absolute(3); printRow(rs); rs.relative(-2); printRow(rs); if (conn != null) conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/testdb", "root", ""); Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet resultSet = statement.executeQuery("SELECT * FROM products"); // Move to the second row resultSet.absolute(2);// www.ja va 2 s. c om System.out.println("You are now in: " + resultSet.getRow()); // Move 2 records forward from the current position (fourth row) resultSet.relative(2); System.out.println("You are now in: " + resultSet.getRow()); // Move to the last row in the result set resultSet.absolute(-1); System.out.println("You are now in: " + resultSet.getRow()); // Move 3 records backward from the current position (second row) resultSet.relative(-3); System.out.println("You are now in: " + resultSet.getRow()); connection.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/testdb", "root", ""); Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet resultSet = statement.executeQuery("SELECT * FROM products"); while (resultSet.next()) { String productCode = resultSet.getString("product_code"); String productName = resultSet.getString("product_name"); int quantity = resultSet.getInt("quantity"); double price = resultSet.getDouble("price"); System.out.println(productCode + "\t" + productName + "\t" + quantity + "\t" + price); }/* w ww . j av a 2 s. c om*/ while (resultSet.previous()) { String productCode = resultSet.getString("product_code"); String productName = resultSet.getString("product_name"); int quantity = resultSet.getInt("quantity"); double price = resultSet.getDouble("price"); System.out.println(productCode + "\t" + productName + "\t" + quantity + "\t" + price); } connection.close(); }
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_READ_ONLY); 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 * FROM survey"); // Move cursor forward while (rs.next()) { // Get data at cursor String id = rs.getString(1); String name = rs.getString(2); }/*from ww w . jav a 2s . c om*/ // Move cursor backward while (rs.previous()) { // Get data at cursor String id = rs.getString(1); String name = rs.getString(2); } // Move cursor to the first row rs.first(); // Move cursor to the last row rs.last(); // Move cursor to the end, after the last row rs.afterLast(); // Move cursor to the beginning, before the first row. // cursor position is 0. rs.beforeFirst(); // Move cursor to the second row rs.absolute(2); // Move cursor to the last row rs.absolute(-1); // Move cursor to the second-to-last row rs.absolute(-2); // Move cursor down 5 rows from the current row. If this moves // cursor beyond the last row, cursor is put after the last row rs.relative(5); // Move cursor up 3 rows from the current row. If this moves // cursor beyond the first row, cursor is put before the first row rs.relative(-3); rs.close(); st.close(); conn.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { String driverName = "com.jnetdirect.jsql.JSQLDriver"; Class.forName(driverName);/*from w ww . j av a2s . co m*/ String serverName = "127.0.0.1"; String portNumber = "1433"; String mydatabase = serverName + ":" + portNumber; String url = "jdbc:JSQLConnect://" + mydatabase; String username = "username"; String password = "password"; Connection connection = DriverManager.getConnection(url, username, password); // Create an updatable result set Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table"); // Delete the first row resultSet.first(); resultSet.deleteRow(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { String driverName = "com.jnetdirect.jsql.JSQLDriver"; Class.forName(driverName);/* ww w . j a va2 s . c o m*/ String serverName = "127.0.0.1"; String portNumber = "1433"; String mydatabase = serverName + ":" + portNumber; String url = "jdbc:JSQLConnect://" + mydatabase; String username = "username"; String password = "password"; Connection connection = DriverManager.getConnection(url, username, password); Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table"); // Get cursor position int pos = resultSet.getRow(); boolean b = resultSet.isBeforeFirst(); // Move cursor to the first row resultSet.next(); // Get cursor position pos = resultSet.getRow(); b = resultSet.isFirst(); // Move cursor to the last row resultSet.last(); // Get cursor position pos = resultSet.getRow(); b = resultSet.isLast(); // Move cursor past last row resultSet.afterLast(); // Get cursor position pos = resultSet.getRow(); b = resultSet.isAfterLast(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { String driverName = "com.jnetdirect.jsql.JSQLDriver"; Class.forName(driverName);// ww w. j a va2 s .c o m String serverName = "127.0.0.1"; String portNumber = "1433"; String mydatabase = serverName + ":" + portNumber; String url = "jdbc:JSQLConnect://" + mydatabase; String username = "username"; String password = "password"; Connection connection = DriverManager.getConnection(url, username, password); // Create a scrollable result set Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table"); // Move to the end of the result set resultSet.last(); // Get the row number of the last row which is also the row count int rowCount = resultSet.getRow(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/testdb", "root", ""); Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); String query = "SELECT id, code, name, quantity, price FROM products"; ResultSet uprs = statement.executeQuery(query); while (uprs.next()) { System.out.println(uprs.getString("id") + ":" + uprs.getString("code") + ":" + uprs.getString("name") + ":" + uprs.getInt("quantity") + ":" + uprs.getDouble("price")); }/*from w ww. java 2 s . co m*/ uprs.first(); uprs.updateString("name", "Java"); uprs.updateRow(); uprs.next(); uprs.deleteRow(); uprs.moveToInsertRow(); uprs.updateString("code", "1"); uprs.updateString("name", "Data Structures"); uprs.updateInt("quantity", 1); uprs.updateDouble("price", 5.99); uprs.insertRow(); uprs.beforeFirst(); while (uprs.next()) { System.out.println(uprs.getString("id") + "\t" + uprs.getString("code") + "\t" + uprs.getString("name") + "\t" + uprs.getInt("quantity") + "\t" + uprs.getDouble("price")); } connection.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { String driverName = "com.jnetdirect.jsql.JSQLDriver"; Class.forName(driverName);//from w w w .j a v a2s . c o m String serverName = "127.0.0.1"; String portNumber = "1433"; String mydatabase = serverName + ":" + portNumber; String url = "jdbc:JSQLConnect://" + mydatabase; String username = "username"; String password = "password"; Connection connection = DriverManager.getConnection(url, username, password); // Create a scrollable result set Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table"); // Move cursor forward while (resultSet.next()) { // Get data at cursor String s = resultSet.getString(1); } // Move cursor backward while (resultSet.previous()) { // Get data at cursor String s = resultSet.getString(1); } }
From source file:Main.java
public static void main(String[] args) throws Exception { try {/*from w ww . j a va 2s. co 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); String sqlQuery = "SELECT EMPNO, EName, Job, MGR, HIREDATE FROM EMP"; ResultSet rs = stmt.executeQuery(sqlQuery); int rowSize = 0; while (rs.next()) { rowSize++; } System.out.println("Number of Rows in ResultSet is: " + rowSize); if (rowSize == 0) { System.out.println("Since there are no rows, exiting..."); System.exit(0); } int cursorPosition = Math.round(rowSize / 2); System.out.println("Moving to position: " + cursorPosition); rs.absolute(cursorPosition); System.out.println("Name: " + rs.getString(2)); rs.relative(-1); cursorPosition = rs.getRow(); System.out.println("Moving to position: " + cursorPosition); System.out.println("Name: " + rs.getString(2)); System.out.println("Moving to the first row"); while (!rs.isFirst()) { rs.previous(); } System.out.println("Name: " + rs.getString(2)); connection.close(); } catch (Exception e) { System.err.println(e); } }