Example usage for java.sql ResultSet next

List of usage examples for java.sql ResultSet next

Introduction

In this page you can find the example usage for java.sql ResultSet next.

Prototype

boolean next() throws SQLException;

Source Link

Document

Moves the cursor forward one row from its current position.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    String db_file_name_prefix = "c:\\mydbdir\\mydb";

    Connection con = null;// w  w w  .j  a  va 2  s .  co  m
    Class.forName("org.hsqldb.jdbcDriver");

    con = DriverManager.getConnection("jdbc:hsqldb:file:" + db_file_name_prefix, // filenames
            "sa", // username
            ""); // password

    Statement statement = con.createStatement();
    ResultSet rs = statement.executeQuery("SELECT * FROM \"User\"");
    while (rs.next()) {
        System.out.print("ID: " + rs.getString("ID"));
        System.out.print(" first name: " + rs.getString("firstname"));
        System.out.println(" last name: " + rs.getString("lastname"));
    }
    statement.close();
    con.close();
}

From source file:Test.java

public static void main(String[] args) throws Exception {
    Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/contact", "userName", "password");
    DatabaseMetaData metaData = con.getMetaData();
    ResultSet resultSet = metaData.getPseudoColumns("", "schemaName", "tableName", "");

    while (resultSet.next()) {
        System.out.println(resultSet.getString("TABLE_SCHEM ") + " - " + resultSet.getString("COLUMN_NAME "));
    }//from w  w  w .  ja  va 2s.c o m

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    // DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
    final Connection c = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system",
            "manager");
    String plsql = " declare " + "    p_id varchar2(20) := null; " + "    l_rc sys_refcursor;" + " begin "
            + "    p_id := ?; " + "    ? := 'input parameter was = ' || p_id;" + "    open l_rc for "
            + "        select 1 id, 'abc' name from dual; " + "    ? := l_rc;" + " end;";

    CallableStatement cs = c.prepareCall(plsql);
    cs.setString(1, "12345");
    cs.registerOutParameter(2, Types.VARCHAR);
    //  cs.registerOutParameter(3, OracleTypes.CURSOR);

    cs.execute();/*ww  w . jav a2 s.co  m*/

    System.out.println("Result = " + cs.getObject(2));

    ResultSet cursorResultSet = (ResultSet) cs.getObject(3);
    while (cursorResultSet.next()) {
        System.out.println(cursorResultSet.getInt(1) + " " + cursorResultSet.getString(2));
    }
    cs.close();
    c.close();
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection c = DriverManager
            .getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:/access.mdb");

    ResultSet rs = c.createStatement().executeQuery("select objet from Employee");

    while (rs.next()) {
        System.out.println(rs.getString("objet"));
    }/*from   w w  w. j a v  a 2  s . co  m*/
    rs.close();
    c.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   w w w  .j  av  a2 s.co  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();

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);

    String insert = "INSERT INTO orders (username, order_date) VALUES ('foobar', '2007-12-13')";
    Statement stmt = conn.createStatement();

    stmt.executeUpdate(insert, Statement.RETURN_GENERATED_KEYS);

    ResultSet keys = stmt.getGeneratedKeys();
    int lastKey = 1;
    while (keys.next()) {
        lastKey = keys.getInt(1);/*from   w w w . j a va 2 s. co  m*/
    }
    System.out.println("Last Key: " + lastKey);
    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  av a  2  s .c  om*/

    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);
    DatabaseMetaData dbmd = connection.getMetaData();
    ResultSet resultSet = dbmd.getTypeInfo();

    while (resultSet.next()) {
        String typeName = resultSet.getString("TYPE_NAME");

        short dataType = resultSet.getShort("DATA_TYPE");
        getJdbcTypeName(dataType);
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Connection dbConnection = null;
    PreparedStatement preparedStatement = null;
    Class.forName(DB_DRIVER);/*w w  w . j a va  2 s.c o m*/
    dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);

    String selectSQL = "SELECT USER_ID, USERNAME FROM Person WHERE USER_ID = ?";
    preparedStatement = dbConnection.prepareStatement(selectSQL);
    preparedStatement.setInt(1, 1001);

    ResultSet rs = preparedStatement.executeQuery();

    while (rs.next()) {

        String userid = rs.getString("USER_ID");
        String username = rs.getString("USERNAME");

        System.out.println("userid : " + userid);
        System.out.println("username : " + username);

    }

    preparedStatement.close();
    dbConnection.close();

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = null;// ww  w  . j a  va2  s. c o  m
    Statement stmt = null;

    Class.forName(JDBC_DRIVER);
    conn = DriverManager.getConnection(DB_URL, USER, PASS);
    System.out.println("Deleting database...");
    stmt = conn.createStatement();

    conn = DriverManager.getConnection(DB_URL, USER, PASS);

    stmt = conn.createStatement();
    String sql = "SELECT id, first, last, age FROM Person";
    ResultSet rs = stmt.executeQuery(sql);
    while (rs.next()) {
        // Retrieve by column name
        int id = rs.getInt("id");
        int age = rs.getInt("age");
        String first = rs.getString("firstName");
        String last = rs.getString("lastName");

        // Display values
        System.out.print("ID: " + id);
        System.out.print(", Age: " + age);
        System.out.print(", First: " + first);
        System.out.println(", Last: " + last);
    }
    rs.close();
    stmt.close();
    conn.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);
    }/*from  www  .  j  av  a  2 s.  c  o  m*/
    rs.close();
    stmt.close();
}