List of usage examples for java.sql Statement executeQuery
ResultSet executeQuery(String sql) throws SQLException;
ResultSet
object. 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')"); String tableName = "survey"; String query = "select * from " + tableName + " where 1=0"; Statement stmt = null; ResultSet rs = null;//from w w w. ja v a 2s .com try { stmt = conn.createStatement(); rs = stmt.executeQuery(query); System.out.println("Exist"); ; } catch (Exception e) { // table does not exist or some other problem //e.printStackTrace(); System.out.println("Not Exist"); } 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')"); String tableName = "survey"; String query = "select count(*) from " + tableName; Statement stmt = null; ResultSet rs = null;/*from ww w . ja va 2s .com*/ try { stmt = conn.createStatement(); rs = stmt.executeQuery(query); System.out.println("Exist"); ; } catch (Exception e) { // table does not exist or some other problem //e.printStackTrace(); System.out.println("Not Exist"); } 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 ww w . j ava 2s.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 a scrollable result set Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table"); while (resultSet.next()) { // Get data at cursor String s = resultSet.getString(1); } while (resultSet.previous()) { // Get data at cursor String s = resultSet.getString(1); } // Move cursor to the first row resultSet.first(); // Move cursor to the last row resultSet.last(); // Move cursor to the end, after the last row resultSet.afterLast(); }
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); }/* www . j a v a 2 s .com*/ 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 { try {//from w w w.ja va 2s . 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:Main.java
public static void main(String[] argv) throws Exception { String driverName = "com.jnetdirect.jsql.JSQLDriver"; Class.forName(driverName);//from ww w .ja va 2s.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 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); } // Move cursor to the second row resultSet.absolute(2); // Move cursor to the last row resultSet.absolute(-1); // Move cursor to the second last row resultSet.absolute(-2); }
From source file:GetDateFromMySql.java
public static void main(String args[]) { ResultSet rs = null;/* www .j a v a2s. c o m*/ Connection conn = null; Statement stmt = null; try { conn = getMySQLConnection(); stmt = conn.createStatement(); rs = stmt.executeQuery("select timeCol, dateCol, dateTimeCol from dateTimeTable"); while (rs.next()) { java.sql.Time dbSqlTime = rs.getTime(1); java.sql.Date dbSqlDate = rs.getDate(2); java.sql.Timestamp dbSqlTimestamp = rs.getTimestamp(3); System.out.println("dbSqlTime=" + dbSqlTime); System.out.println("dbSqlDate=" + dbSqlDate); System.out.println("dbSqlTimestamp=" + dbSqlTimestamp); java.util.Date dbSqlTimeConverted = new java.util.Date(dbSqlTime.getTime()); java.util.Date dbSqlDateConverted = new java.util.Date(dbSqlDate.getTime()); System.out.println("in standard date"); System.out.println(dbSqlTimeConverted); System.out.println(dbSqlDateConverted); } } catch (Exception e) { e.printStackTrace(); } finally { try { rs.close(); stmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }
From source file:UpdateableRs.java
public static void main(String[] args) throws Exception { Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "yourName", "mypwd"); Statement stmt = conn.createStatement(); stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet rs = stmt.executeQuery("SELECT ssn, name, salary FROM EMPLOYEES"); printRs(rs);/*from w ww . j a v a 2 s. c o m*/ rs.beforeFirst(); while (rs.next()) { double newSalary = rs.getDouble("salary") * 1.053; rs.updateDouble("salary", newSalary); rs.updateRow(); } printRs(rs); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "yourName", "mypwd"); Statement stmt = conn.createStatement(); stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet rs = stmt.executeQuery("SELECT ssn, name, salary FROM EMPLOYEES"); printRs(rs);//from ww w . j a va 2s . c o m rs.beforeFirst(); while (rs.next()) { double newSalary = rs.getDouble(3) * 1.053; rs.updateDouble("salary", newSalary); rs.updateRow(); } printRs(rs); conn.close(); }
From source file:SqlWarning.java
public static void main(String[] args) { try {// w w w .j av a 2 s .c om 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_SENSITIVE, ResultSet.CONCUR_UPDATABLE); SQLWarning sw = null; ResultSet rs = stmt.executeQuery("Select * from employees"); sw = stmt.getWarnings(); System.out.println(sw.getMessage()); while (rs.next()) { System.out.println("Employee name: " + rs.getString(2)); } rs.previous(); rs.updateString("name", "Jon"); } catch (SQLException se) { System.out.println("SQLException occurred: " + se.getMessage()); } catch (Exception e) { e.printStackTrace(); } }