Adv Java - [JDBC: Database Operations]

♠ Posted by Unknown in at 01:24

JDBC Database Operations


Write an application program to perform all the database driven operations such as
  Insertion, Deletion, Update and Selection using the concept of Statement.

import java.sql.*;
import java.io.*;
class EmployeeExample
{
          String dbUrl = "jdbc:odbc:Employee";
          String user = "";
          String password = "";
          Connection c;
          Statement s;
          ResultSet r;
          EmployeeExample() throws SQLException, ClassNotFoundException
          {
                   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                   c = DriverManager.getConnection(dbUrl, user,password);
                   s = c.createStatement();
          }
          public void InsertEmployee() throws SQLException, IOException
          {
                   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                   System.out.print("Enter Employee No. : ");
                   int eno = Integer.parseInt(br.readLine());
                   System.out.print("Enter Employee Name : ");
                   String ename = br.readLine();
                   System.out.print("Enter Employee Designation : ");
                   String edesig = br.readLine();
                   System.out.print("Enter Employee Salary : ");
                   int esal = Integer.parseInt(br.readLine());
                   System.out.print("Enter Employee Join Date : ");;
                   String edate = br.readLine();
                   String InsQuery = "Insert Into Emp_Mst Values(" + eno + ",'" + ename + "','" + edesig + "'," + esal + ",'" + edate + "')";
                   s.executeUpdate(InsQuery);
          }
          public void DeleteEmployee() throws SQLException, IOException
          {
                   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                   System.out.print("Enter Employee No. TO DELETE Record : ");
                   int eno = Integer.parseInt(br.readLine());
                   String DelQuery = "Delete From Emp_Mst Where EmpId = " + eno;
                   s.executeUpdate(DelQuery);
          }
          public void DisplayEmployee() throws SQLException, ClassNotFoundException
          {
                   r = s.executeQuery("Select * From Emp_Mst");
                   while(r.next())
                   {
                             System.out.println(r.getString("EmpId") + " " + r.getString("EmpName"));
                   }
          }
}
public class DBExample
{
          public static void main(String [] args) throws SQLException, ClassNotFoundException, IOException
          {
                   EmployeeExample EObj = new EmployeeExample();
                   EObj.DisplayEmployee();
                   EObj.InsertEmployee();
                   EObj.DisplayEmployee();
                   EObj.DeleteEmployee();
                   EObj.DisplayEmployee();
          }

}

0 comments:

Post a Comment