Write a JAVA program to accept the details of Doctor (DNo, DName, Address, Salary) from the user. Insert it into the table and display it on the screen. (use Command Line arguments).

import java.sql.*;
class Slip9
{
   public static void main(String args[])
   {
        Connection con;
        PreparedStatement pstmt;
        ResultSet rs;
        try
        {
            if(args.length==0)
            {
               System.out.println("Pass the argument");
               System.exit(0);
            }
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            System.out.println("Driver loaded");
            con=DriverManager.getConnection("jdbc:odbc:Bookdsn");
            System.out.println("Connection Established");
            if(args[0].equals("|")|| args[0].equals("i"))
            {
                 pstmt=con.prepareStatement("Insert into Doctor values(?,?,?,?)");
                 pstmt.setInt(1,Integer.parseInt(args[1]));
                 pstmt.setString(2,args[2]);
                 pstmt.setString(3,args[3]);
                 pstmt.setFloat(4,Float.parseFloat(args[4]));
                 pstmt.executeUpdate();
                 System.out.println("Inserted record successfully");
            }
            pstmt=con.prepareStatement("Select * from Doctor");
            rs=pstmt.executeQuery();
            while(rs.next());
            {
                System.out.println("" +rs.getInt(1)+""+rs.getString(2)+""+rs.getString(3)+""+rs.getFloat(4));
            }
            rs.close();
            con.close();
       }
       catch(ClassNotFoundException e)
       {
           e.printStackTrace();
       }
       catch(SQLException e)
       {
           e.printStackTrace();
       }
       catch(Exception e)
       {
           e.printStackTrace();
       }
    }
}

No comments:

Post a Comment