Write a JAVA program to accept the details of student (Rno , SName , Per) from the user and insert it into the table. (use PreparedStatement Class).

import java.sql.*;
import java.io.*;

class slip4
{
public static void main(String args[])
{
 if(args.length!=3)
{
 System.out.println("wrong no of argument");
 System.exit(1);
}
int Rno,per;
String sname,query;
Connection conn=null;
ResultSet rs=null;
try
{
Rno=Integer.parseInt(args[0]);
}
catch(NumberFormatException e)
{
Rno=5;
System.out.println("Error");
}
 sname=args[1];
 try
{
 per=Integer.parseInt(args[2]);
}
catch(NumberFormatException e)
{
per=44;
System.out.println("Error");
}
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:my");
PreparedStatement ps=conn.prepareStatement("insert into stud values(?,?,?)");
ps.setInt(1,Rno);
ps.setString(2,sname);
ps.setInt(3,per);
ps.executeUpdate();
System.out.println("record has been inserted");

rs.close();
conn.close();
conn.commit();
}
catch(Exception e)
{
System.out.println("exception occure="+e);
}
}
}

No comments:

Post a Comment