JDBC Get Int

In this tutorial we want to describe you a code that help in understanding JDBC Get Int.

JDBC Get Int

JDBC Get Int

     

The  get Byte, get Short, get Long, get Float, get Double, getBigDecimal, get Boolean, get String, get Object is readable by Get Int in Java.

Understand with Example

The Tutorial illustrates an example from JDBC Get Int.In this Tutorial, the program explain you a JDBC code that connect the front end application to the backend database. The code include the sql queries that return you the integer and retrieve the element from the database. For this we have a class Jdbc Get Int, Inside the main method driver loading is done by calling a class.forname(  ) ,that accept driver class as argument.

 

 

 

 

  Method    Description

 DriverManager.getConnection() 

Return you an object of connection, built  a connection between url and database.

con.createStatement( )

Return you a sql object, The return object is used to send and execute the query in database.

executeQuery( )

Return you a result set, This result set contain a set of retrieved element from a table

getInt( )

Return you the retrieve the value of specified column in the current row of the result set object as int.

The rs.next( ) return you the next element in the series. Finally the println print the respective id on the output. The con.close ( ) closed connection with database. 

Finally the println print the respective id in the output. In case an exception occurred in try block, the subsequent catch block check and caught the exception.

JdbcGetInt.java

import java.sql.*;

public class JdbcGetInt {

    public static void main(String args[]) {

        Connection con = null;
        Statement st = null;
        ResultSet rs = null;

        String url = "jdbc:mysql://localhost:3306/";
        String db = "komal";
        String driver = "com.mysql.jdbc.Driver";
        String user = "root";
        String pass = "root";

        try {
            Class.forName(driver);
            con = DriverManager.getConnection(url + db, user, pass);
            st = con.createStatement();

            String sql = "select * from person";
            rs = st.executeQuery(sql);
            
            System.out.println("Id\tName");
            while (rs.next()) {
                System.out.print(rs.getInt(1));
                System.out.println("\t"+rs.getString(2));
            }
            rs.close();
            st.close();
            con.close();

        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

Output

Id	Name
1	Girish
2	komal

Download code