Saturday, March 21, 2015

How to use JDBC in Java Application



How to use JDBC in Java Application



How to use JDBC in Java Application




To use the JDBC in a Java language application or applet, the following steps are to be followed:


1.   Import the java.sql package.

2.   Load the driver.

3.   Establish a connection to the database

4.   Create a statement

5.   Execute the statement

6.   Retrieve the result

7.   close the connection and statement

Step 1:  Import sql package.


            The first step of importing java.sql is the routine procedure.
            Import java.sql.*;


Step 2: Load the driver


            Loading of the driver is done using the following method:
            Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);


Step 3: Establish connection


            Connection is established with the database using the following method defined in the DriverManager class:

            Connection  con=DriverManager.getConnection(“jdbc:odbc:dsnName”)

            Where, “jdbc:odbc:database” is the database URL Object specifying the protocol,subprotocol and the database name.



Step 4: Prepare statement


            In this step, statement object that is required to send a query to the database is prepared. This statement is prepared using the following method defined in Connection interface:
            

Statement st=con.createStatement();



Step 5: Execute query


            The SQL query that is to be sent to the database is executed by calling the following method on statement object, which returns a ResultSet object:
            ResultSet rs=st.executeQuery(“select * from database”);



Step 6: Retrieve the result.


            The result returned by the database to the query executed are extracted form ResultSet using the get method defined in ResultSet interface.


            While(rs.next())                                                                      //while(rs.next())
                        System.out.println(“Name:” + rs.getString(“name”));



Step 7: Close the statement and connection.


            Using the close method, the Statement and Connection are closed:
          
            st.close();
            con.close();           


Example:


import java.sql.*;

class DbDemo
{
      public static void main(String argv[])
     {
            try
            {
            Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
            String url = "jdbc:odbc:profiledsn";
            Connection con = DriverManager.getConnection(url,"","");
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * FROM profile");
            System.out.println("Records are:");
            while (rs.next())
            {
                        String fname = rs.getString(1);
                        String lname = rs.getString(2);
                        System.out.print (" FirstName=" + fname);
                        System.out.print (" LastName=" + lname);
                        System.out.print(" \n");
            }
            stmt.close();
            con.close();
            }
            catch (Exception e)
            {
                        e.printStackTrace();
            }
     }
}




How to use JDBC in Java Application

0 comments:

Post a Comment