Practical 18: Write a program to insert and retrieve the data from database using JDBC

Database:
Student Database describe Oracle

Program to insert data:

// Program to Insert Data into Database

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.SQLException;

public class InsertStaticOracle
{ 
 public static void main(String args[])
 {
  Statement  st = null;
  Connection connection = null; 
  try{
   oracle.jdbc.OracleDriver driverObj = new oracle.jdbc.OracleDriver();
   
   String url = "jdbc:oracle:thin:@localhost:1521:XE", username = "System" ,password = "pass";
  
   connection =DriverManager.getConnection(url,username,password);
 
   if(connection!=null)
    System.out.println("Connection established successfully");

    st = connection.createStatement();

   String qry = "Insert into Student values(104 ,'Atharva Agrawal','Dhule')";
  
   int count =  st.executeUpdate(qry);

   System.out.println(count+" Statement Created Successfully ");
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }
  finally{  
   try
   {
    if(st != null) 
     st.close();
    if(connection != null)
     connection.close();
   }
   catch(SQLException e){
    e.printStackTrace();
   }
  }
 }
}



Output:
Insert data into database



Program to retrieve data:

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;

public class SelectOracle
{
 public static void main(String args[]) throws SQLException
 {
  System.out.println("Step 1: ");
  oracle.jdbc.driver.OracleDriver obj = new oracle.jdbc.driver.OracleDriver();
  // Class.forName(oracle.jdbc.driver.OracleDriver);
  System.out.println("Driver loaded successfully");

  System.out.println("Step 2: ");
  String url="jdbc:oracle:thin:@localhost:1521:XE",uname="SYSTEM" , password="Atharva007";
  Connection connection = DriverManager.getConnection(url,uname,password);
  if(connection!=null)
   System.out.println("Connection Established Successfully");
  else
   System.out.println("Connection Not Established Successfully");

  
  System.out.println("Step 3: ");
  Statement st = connection.createStatement();
  System.out.println("Statement Referenced   ");

  System.out.println("Step 4: ");
  System.out.println("Step 5: ");
  String qry = "select * from Student";
  ResultSet rs = st.executeQuery(qry);
  System.out.println("rs: "+rs);
  
  System.out.println("Step 6: ");
  System.out.println("Id\tName\taddress");
  while(rs.next())
  {
   int x = rs.getInt(1);
   String y = rs.getString("Name");
   String s = rs.getString(3);
   System.out.println(x+"\t"+y+"\t"+s);
  }    
    
  System.out.println("Step 7: ");
  rs.close();
  st.close();
  connection.close();
 } 
}

Output:

Retrieve Data from Database

Previous
Next Post »