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 {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String URL = "jdbc:odbc:dbName";
    Connection conn = DriverManager.getConnection(URL, "user", "passw");

    DatabaseMetaData dmd = conn.getMetaData();
    ResultSet rs1 = dmd.getSchemas();
    while (rs1.next()) {
        String ss = rs1.getString(1);
        ResultSet rs2 = dmd.getTables(null, ss, "%", null);
        while (rs2.next())
            System.out.println(rs2.getString(3) + " " + rs2.getString(4));
    }/*from   w  ww. j  a  v  a2 s. c o m*/
    conn.close();
}

From source file:PreparedStmt.java

public static void main(String args[]) throws Exception {

    String query = "SELECT * FROM Stock WHERE Item_Number = ?";
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:Inventory");
    PreparedStatement pstmt = con.prepareStatement(query);
    pstmt.setInt(1, 2);/*from w ww . java  2 s  .  com*/
    ResultSet rs = pstmt.executeQuery();
    while (rs.next()) {
        String name = rs.getString("Name");
        String desc = rs.getString("Description");
        int qty = rs.getInt("Qty");
        float cost = rs.getFloat("Cost");
        System.out.println(name + ", " + desc + "\t: " + qty + "\t@ $" + cost);
    }
}

From source file:JdbcDemo.java

public static void main(String args[]) throws Exception {
    String query = "SELECT Name,Description,Qty,Cost FROM Stock";

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:Inventory");
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery(query);
    while (rs.next()) {
        String name = rs.getString("Name");
        String desc = rs.getString("Description");
        int qty = rs.getInt("Qty");
        float cost = rs.getFloat("Cost");
        System.out.println(name + ", " + desc + "\t: " + qty + "\t@ $" + cost);
    }/*from  w w  w .ja  va 2  s.  c  o  m*/
    con.close();
}

From source file:CallableStmt.java

public static void main(String args[]) throws Exception {
    String storedProc = "create procedure SHOW_ORDERS_BY_STATE @State CHAR (2) as "
            + "select c.Last_Name+', '+c.First_Name AS Name,o.Order_Number "
            + "from CUSTOMERS c, ORDERS o where c.Customer_Number = o.Customer_Number "
            + "AND c.State = @State order by c.Last_Name;";

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:Customers");
    Statement stmt = con.createStatement();
    stmt.executeUpdate(storedProc);//w  w w .  j a v  a2 s. c o m
    CallableStatement cs = con.prepareCall("{call SHOW_ORDERS_BY_STATE(?)}");
    cs.setString(1, "NJ");
    ResultSet rs = cs.executeQuery();
    while (rs.next()) {
        String name = rs.getString("Name");
        int orderNo = rs.getInt("Order_Number");
        System.out.println(name + ": " + orderNo);
    }
}

From source file:PrintResultSet.java

public static void main(String args[]) throws Exception {
    String query = "SELECT Name,Description,Qty,Cost FROM Stock";
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:Inventory");
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery(query);

    while (rs.next()) {
        System.out.print(rs.getString("Name") + "\t");
        System.out.print(rs.getString("Description") + "\t");
        System.out.print(rs.getInt("Qty") + "\t");
        System.out.println(rs.getFloat("Cost"));
    }//from ww  w.j a va  2s .  co m
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    System.out.println("Oracle driver registered");
    Connection conn = null;//  www  .  ja va2  s .com
    conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orclh", "scott", "tiger");

    Statement stmt = conn.createStatement();
    ResultSet r = stmt.executeQuery("Select * from emp");
    while (r.next()) {
        String str = r.getString("ename");
        System.out.println(str);
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    int count = 0;
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root");

    String sql = "SELECT title,year_made FROM product WHERE year_made >= ? AND year_made <= ?";
    PreparedStatement prest = con.prepareStatement(sql);
    prest.setInt(1, 2000);//ww w.  j  av  a  2  s  .com
    prest.setInt(2, 2009);
    ResultSet rs = prest.executeQuery();
    while (rs.next()) {
        String mov_name = rs.getString(1);
        int mov_year = rs.getInt(2);
        count++;
        System.out.println(mov_name + "\t" + "- " + mov_year);
    }
    System.out.println("Number of records: " + count);
    prest.close();
    con.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root");

    String sql = "SELECT * FROM product WHERE year_made = ?";
    PreparedStatement prest = con.prepareStatement(sql);
    prest.setInt(1, 2002);//from   w  w  w  . j av a2  s  .c  o m
    ResultSet rs1 = prest.executeQuery();
    while (rs1.next()) {
        String mov_name = rs1.getString(1);
        int mad_year = rs1.getInt(2);
        System.out.println(mov_name + "\t- " + mad_year);
    }
    prest.setInt(1, 2003);
    ResultSet rs2 = prest.executeQuery();
    while (rs2.next()) {
        String mov_name = rs2.getString(1);
        int mad_year = rs2.getInt(2);
        System.out.println(mov_name + "\t- " + mad_year);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getHSQLConnection();

    DatabaseMetaData meta = conn.getMetaData();
    ResultSet catalogs = meta.getCatalogs();

    while (catalogs.next()) {
        String catalog = catalogs.getString(1); //"TABLE_CATALOG"
        System.out.println("catalog: " + catalog);
    }//from ww w . j  a  va  2  s  .  c  om

    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getHSQLConnection();

    DatabaseMetaData meta = conn.getMetaData();
    ResultSet schemas = meta.getSchemas();
    while (schemas.next()) {
        String tableSchema = schemas.getString(1); // "TABLE_SCHEM"
        System.out.println("tableSchema:" + tableSchema);
        String tableCatalog = schemas.getString(2); //"TABLE_CATALOG"
        System.out.println("tableCatalog:" + tableCatalog);
    }//from   w w w . j a va 2s  .  c  o m

    conn.close();
}