Example usage for java.sql ResultSet getString

List of usage examples for java.sql ResultSet getString

Introduction

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

Prototype

String getString(String columnLabel) throws SQLException;

Source Link

Document

Retrieves the value of the designated column in the current row of this ResultSet object as a String in the Java programming language.

Usage

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();/*  w  w  w .  java 2s. c om*/

    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 {
    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 to the last row
    rs.last();/*w ww. j a  va2  s . c om*/
    // Get data at cursor
    String id = rs.getString("id");
    System.out.println(id);

    rs.close();
    st.close();
    conn.close();
}

From source file:JndiDataSource.java

public static void main(String args[]) throws Exception {
    String sp = "com.sun.jndi.fscontext.RefFSContextFactory";
    String file = "file:/e:/JNDI";
    String dataSourceName = "jdbc/myDatabase";

    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, sp);
    env.put(Context.PROVIDER_URL, file);
    ctx = new InitialContext(env);

    bindDataSource(ctx, dataSourceName);

    DataSource ds = null;/*from ww  w . ja  v  a2s .  c om*/
    ds = (DataSource) ctx.lookup(dataSourceName);

    Connection conn = ds.getConnection();

    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT Name FROM Employees");

    while (rs.next())
        System.out.println(rs.getString("name"));

    ctx.close();
    rs.close();
    stmt.close();
    conn.close();

    ctx.close();
    conn.close();
}

From source file:Main.java

public static final void main(String[] argv) throws Exception {
    Class.forName("oracle.jdbc.OracleDriver");
    Connection conn = DriverManager.getConnection("your_connection_string", "your_user_name", "your_password");
    Date nowDate = new Date();
    Timestamp nowTimestamp = new Timestamp(nowDate.getTime());
    PreparedStatement insertStmt = conn.prepareStatement(
            "INSERT INTO MyTable" + " (os_name, ts, ts_with_tz, ts_with_local_tz)" + " VALUES (?, ?, ?, ?)");
    insertStmt.setString(1, System.getProperty("os.name"));
    insertStmt.setTimestamp(2, nowTimestamp);
    insertStmt.setTimestamp(3, nowTimestamp);
    insertStmt.setTimestamp(4, nowTimestamp);
    insertStmt.executeUpdate();/*from ww w.jav  a  2 s  . c  o  m*/
    insertStmt.close();
    System.out.println("os_name, ts, ts_with_tz, ts_with_local_tz");
    PreparedStatement selectStmt = conn
            .prepareStatement("SELECT os_name, ts, ts_with_tz, ts_with_local_tz" + " FROM MyTable");
    ResultSet result = null;
    result = selectStmt.executeQuery();
    while (result.next()) {
        System.out.println(String.format("%s,%s,%s,%s", result.getString(1), result.getTimestamp(2).toString(),
                result.getTimestamp(3).toString(), result.getTimestamp(4).toString()));
    }
    result.close();
    selectStmt.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_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 to the last row
    rs.absolute(-1);/*from w w w .ja v a 2s . com*/
    // Get data at cursor
    String id = rs.getString("id");
    System.out.println(id);

    // Move cursor to the second-to-last row
    //rs.absolute(-2);

    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);//  w w  w . j a v a 2 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);
    DatabaseMetaData dbmd = connection.getMetaData();
    ResultSet resultSet = dbmd.getTypeInfo();

    while (resultSet.next()) {
        // Get the database-specific type name
        String typeName = resultSet.getString("TYPE_NAME");

        // Get the java.sql.Types type to which this database-specific type is
        // mapped
        short dataType = resultSet.getShort("DATA_TYPE");
        getJdbcTypeName(dataType);
    }
}

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();//  ww  w.j  a  va  2s.c o m
    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    while (rs.next()) {
        int id = rs.getInt(1); // index 1 is the "id" column
        String name = rs.getString(2); // index 2 is the "name" column
        System.out.println(id);
        System.out.println(name);
    }

    rs.close();
    st.close();
    conn.close();

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection dbConnection = null;
    String myConnectionString = "";
    myConnectionString = "jdbc:mysql://192.168.1.3:3306/mytestdb";
    dbConnection = DriverManager.getConnection(myConnectionString, "root", "whatever");
    PreparedStatement stmt = dbConnection.prepareStatement("SELECT * FROM jdbctest");
    ResultSet rs = stmt.executeQuery();
    int i = 0;/*from  w  ww  .  j a  v  a 2 s.com*/
    int j = 0;
    String s = "";
    while (rs.next()) {
        i++;
        j = rs.getInt("id");
        s = rs.getString("textcol");
    }
    System.out.println(String.format("Finished reading %d rows.", i));
    rs.close();
    stmt.close();
    dbConnection.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);//w ww . ja  v a  2  s. 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);

    DatabaseMetaData dbmd = connection.getMetaData();

    // Get all stored procedures in any schema and catalog
    ResultSet resultSet = dbmd.getProcedures(null, null, "%");

    // Get stored procedure names from the result set
    while (resultSet.next()) {
        String procName = resultSet.getString(3);
        System.out.println(procName);
    }
}

From source file:TypeInfo.java

public static void main(String args[]) {

    String url = "jdbc:mySubprotocol:myDataSource";
    Connection con;//w ww .  j  a v a2  s. c  om
    DatabaseMetaData dbmd;

    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");

        dbmd = con.getMetaData();

        ResultSet rs = dbmd.getTypeInfo();
        while (rs.next()) {
            String typeName = rs.getString("TYPE_NAME");
            short dataType = rs.getShort("DATA_TYPE");
            String createParams = rs.getString("CREATE_PARAMS");
            int nullable = rs.getInt("NULLABLE");
            boolean caseSensitive = rs.getBoolean("CASE_SENSITIVE");
            System.out.println("DBMS type " + typeName + ":");
            System.out.println("     java.sql.Types:  " + dataType);
            System.out.print("     parameters used to create: ");
            System.out.println(createParams);
            System.out.println("     nullable?:  " + nullable);
            System.out.print("     case sensitive?:  ");
            System.out.println(caseSensitive);
            System.out.println("");

        }

        con.close();

    } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
    }
}