Example usage for java.sql ResultSet getInt

List of usage examples for java.sql ResultSet getInt

Introduction

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

Prototype

int getInt(String columnLabel) throws SQLException;

Source Link

Document

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

Usage

From source file:Main.java

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

    int records = 0;

    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root");

    String sql = "SELECT COUNT(*) FROM mytable";
    PreparedStatement prest = con.prepareStatement(sql);
    ResultSet rs = prest.executeQuery();
    while (rs.next()) {
        records = rs.getInt(1);
    }//from  w ww.  ja  va  2 s  .  c o  m
    System.out.println("Number of records: " + records);
    con.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

    Connection con = DriverManager.getConnection("jdbc:sqlserver://MYSERVER;databaseName=MYDATABASE", "USERID",
            "PASSWORD");

    CallableStatement proc_stmt = con.prepareCall("{ call generateID(?) }");

    proc_stmt.setString(1, "employee");
    ResultSet rs = proc_stmt.executeQuery();

    if (rs.next()) {
        int employeeId = rs.getInt(1);
        System.out.println("Generated employeeId: " + employeeId);
    } else {//from   w  ww  .j a  v  a  2 s  .  c om
        System.out.println("Stored procedure couldn't generate new Id");
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    int sum = 0;/*from   w ww .j a va  2  s  .  c  om*/
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root");
    Statement st = con.createStatement();
    ResultSet res = st.executeQuery("SELECT SUM(col) FROM mytable");
    while (res.next()) {
        int c = res.getInt(1);
        sum = sum + c;
    }
    System.out.println("Sum of column = " + sum);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driver = "com.mysql.jdbc.Driver";
    String user = "root";
    String pass = "root";

    Class.forName(driver).newInstance();
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", user, pass);

    Statement st = con.createStatement();
    ResultSet res = st.executeQuery("SELECT * FROM  emp");
    while (res.next()) {
        int i = res.getInt("ID");
        String s = res.getString("name");
        System.out.println(i + "\t\t" + s);
    }/*  www.j  av a  2  s. co m*/
    con.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    System.out.println("Count number of rows in a specific table!");
    Connection con = null;//  w w  w  . ja va 2 s  .  c  om
    int count = 0;
    Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root");

    Statement st = con.createStatement();
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    ResultSet res = st.executeQuery("SELECT COUNT(*) FROM EMP");
    while (res.next()) {
        count = res.getInt(1);
    }
    System.out.println("Number of row:" + count);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement st = conn.createStatement();

    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (10,'nameValue')");
    st.executeUpdate("insert into survey (id,name ) values (null,null)");
    st = conn.createStatement();/*from  w  w w . j  a  va  2 s.c o m*/
    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    while (rs.next()) {
        int id = rs.getInt(1);
        System.out.println(id);
        if (rs.wasNull()) {
            id = -1;
        }
        System.out.println(id);
    }
    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  ww.j  ava2 s . c o m
    System.out.println("Last Key: " + lastKey);
    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.executeUpdate("insert into survey (id,name ) values (3,'Tom')");
    st = conn.createStatement();/*w w  w. ja v  a  2  s  .c om*/
    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[] argv) throws Exception {

    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root");

    Statement st = con.createStatement();
    ResultSet res = st.executeQuery("SELECT col_name  FROM mytable  ORDER BY col_name ASC");
    while (res.next()) {
        int col = res.getInt(1);
        System.out.println(col);//from www.j  av  a 2s. c  om
    }
}

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();//  w w  w. j  a  v  a2 s. c om
    ResultSet rs = s.getResultSet();
    if (rs != null) {
        while (rs.next()) {
            System.out.println(rs.getInt("All"));
        }
    }
    s.close();

    conn.close();
}