List of usage examples for java.sql ResultSet next
boolean next() throws SQLException;
From source file:Main.java
public static void main(String[] args) throws Exception { Connection con = DriverManager.getConnection("jdbc:mysql://192.168.1.3/zzzTest?" + // "useUnicode=yes&characterEncoding=UTF-8" + // "&user=root&password=whatever"); String newName = "G"; String newEmail = "g@example.com"; String newMobile = "444-555-2222"; String sql = "SELECT " + // "id, " + // "name, " + // "email, " + // "mobile " + // "FROM registerSmsUsers " + // "WHERE mobile = ? " + // "FOR UPDATE"; PreparedStatement pst = con.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE); pst.setString(1, newMobile);/*from w w w.jav a 2 s. c o m*/ ResultSet rs = pst.executeQuery(); if (rs.next()) { rs.moveToCurrentRow(); rs.updateString("name", newName); rs.updateString("email", newEmail); rs.updateRow(); System.out.println("Existing row updated."); } else { rs.moveToInsertRow(); rs.updateString("name", newName); rs.updateString("email", newEmail); rs.updateString("mobile", newMobile); rs.insertRow(); System.out.println("New row inserted."); } }
From source file:Main.java
public static void main(String[] args) throws Exception { System.setProperty("oracle.net.tns_admin", "C:/app/product/11.2.0/client_1/NETWORK/ADMIN"); String dbURL = "jdbc:oracle:thin:@ENTRY_FROM_TNSNAMES"; Class.forName("oracle.jdbc.OracleDriver"); Connection conn = DriverManager.getConnection(dbURL, "your_user_name", "your_password"); System.out.println("Connection established"); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT dummy FROM dual"); if (rs.next()) { System.out.println(rs.getString(1)); }/*w w w . ja v a 2s. co m*/ stmt.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection conn = DriverManager .getConnection("jdbc:odbc:Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};" + "Dbq=C://Book1.xlsx;"); PreparedStatement s = conn.prepareStatement("SELECT * FROM [Sheet1$] WHERE [MetricMonth] = ?"); s.setString(1, "Jul-2013"); s.execute();/*from w ww . ja v a 2 s. co m*/ ResultSet rs = s.getResultSet(); if (rs != null) { while (rs.next()) { System.out.println(rs.getInt("All")); } } s.close(); 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"); while (resultSet.next()) { String productCode = resultSet.getString("product_code"); int row = resultSet.getRow(); System.out.println(row + ". " + productCode); }//from www . java2s . co m connection.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 BINARY );"); String sql = "INSERT INTO survey (name) VALUES(?)"; PreparedStatement pstmt = conn.prepareStatement(sql); String myData = "some string data ..."; byte[] binaryData = myData.getBytes(); pstmt.setBytes(1, binaryData);//from ww w . ja v a 2 s . co m pstmt.executeUpdate(); ResultSet rs = stmt.executeQuery("SELECT * FROM survey"); while (rs.next()) { System.out.print(rs.getBytes("name").length + " "); } rs.close(); stmt.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 BINARY );"); String sql = "INSERT INTO survey (name) VALUES(?)"; PreparedStatement pstmt = conn.prepareStatement(sql); String myData = "some string data ..."; byte[] binaryData = myData.getBytes(); pstmt.setBytes(1, binaryData);/* w w w . j a va 2s . co m*/ pstmt.executeUpdate(); ResultSet rs = stmt.executeQuery("SELECT * FROM survey"); while (rs.next()) { System.out.println(rs.getByte("id")); } 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(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 = conn.createStatement();/*from ww w. j a v a 2 s . com*/ ResultSet rs = st.executeQuery("SELECT * FROM survey"); while (rs.next()) { String name = rs.getString(2); System.out.println(name); } 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 BINARY );"); String sql = "INSERT INTO survey (name) VALUES(?)"; PreparedStatement pstmt = conn.prepareStatement(sql); String myData = "some string data ..."; byte[] binaryData = myData.getBytes(); pstmt.setBytes(1, binaryData);/* w ww .jav a2 s.c om*/ pstmt.executeUpdate(); ResultSet rs = stmt.executeQuery("SELECT * FROM survey"); while (rs.next()) { System.out.print(rs.getByte(1)); } rs.close(); stmt.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 BINARY );"); String sql = "INSERT INTO survey (name) VALUES(?)"; PreparedStatement pstmt = conn.prepareStatement(sql); String myData = "some string data ..."; byte[] binaryData = myData.getBytes(); pstmt.setBytes(1, binaryData);//ww w . j av a 2 s . c o m pstmt.executeUpdate(); ResultSet rs = stmt.executeQuery("SELECT * FROM survey"); while (rs.next()) { System.out.print(rs.getBytes(2).length + " "); } 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(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 = conn.createStatement();/*from ww w . j a v a 2 s . c o m*/ ResultSet rs = st.executeQuery("SELECT * FROM survey"); while (rs.next()) { String name = rs.getString("name"); System.out.println(name); } rs.close(); st.close(); conn.close(); }