Database is required for saving the record so that I can use saved data further.This is simple jdbc program to teach you jdbc in very simple easy step by step approach.

In this tutorial you will learn how to use jdbc to save or get the records.

1.First step is to Load the driver.

Class.forName(“oracle.jdbc.driver.OracleDriver”);

How to load the driver and why we should load this driver class?

Because it knows how to communicate with any database.

It helps to register driver.

Who cares if driver is registered or not i just want to insert data in database ?

 The answer is to communicate with database we need connection of the database.This connection is obtained by using static method getConnection() of  “DriverManager” class.

2.Get the connection now by using getConnection method.

Connection con=DriverManager.getConnection(“jdbc:oracle:thin:@localhost:1521:xe”,”system”,”admin”);

Now what is the syntax for the getConnection method.

public static Connection getConnection(String url,
String user, String password
) throws SQLException

Congrats you have got connected with the database.You are all set to perform actual operations like getting the records from the database.


Steps needed to get records from the table

3.Create the statement.

Statement stmt=con.createStatement();

4.Fire query and catch results in result set.

//Don’t you worry resultset is just another bucket which has all employee records

ResultSet rs=stmt.executeQuery(“select * from emp”);

5.Iterate result set and print results.

while(rs.next())

 {

    //now we got the
one record of employee.

    //”name” and “empId” are the column names in oracle table

      System.out.println(rs.getString(“name”)+”  “+rs.getString(“empId”)); 

 }

Wow you have got results from the table and you are showing it to user.

Please once again go through the complete program.

import
java.sql.DriverManager;

import java.sql.Connection;

import java.sql.SQLException;

import java.sql.*; 

class GetRecordsFromDb

       public static void main(String args[]){ 

              try

                     //This class
communicates with java and the oracle database

                     Class.forName(“oracle.jdbc.driver.OracleDriver”); 

    //public static
Connection getConnection(String url,
String user,
String password) throws SQLException

 Connection
con=DriverManager.getConnection(“jdbc:oracle:thin:@localhost:1521:xe”,“system”,“admin”);  

                   

               Statement
stmt=con.createStatement(); 

                     //Don’t you worry resultset
is just another bucket which has all employee records

                     ResultSet
rs=stmt.executeQuery(“select * from
emp”
);                

                     while(rs.next())

                     {                      

                       //now we got the
one record of employee.

                           System.out.println(rs.getString(“name”)+”  “+rs.getString(“empId”)); 

                     }          

                    //we have got all the records now close the connection                     

                    con.close(); 

              }catch(Exception exceptionObj){

                     System.out.println(“sorry error
in connection”
+exceptionObj.getMessage());

              } 

       } 

I know for sure you have got the idea how to get records from the database.

My question is what if you need to create a small software where you can store all your employees phone numbers and name?



Steps needed to insert records to the table


First 3 steps are same.


  1. you have got statement object just use executeUpdate(String sql) mehod and pass your sql query in it.


int result=stmt.executeUpdate(“insert into emp
values(‘userName’,’passWord’)”);


     5.Check whether record successfully inserted or not.by checking result value.

     6.close the connection.

 

Steps needed to delete records to the table.


int result=stmt.executeUpdate(“Delete from emp where userName=’suresh’;





Assignments


Write a program to insert a record in a database.

Write a program to delete some record in database.



Leave a Reply

Your email address will not be published. Required fields are marked *