Saturday, 9 January 2016

Database using Prepared Statement Class

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.io.*;
import java.sql.*;
class Student
{
   static Connection cn;

   static PreparedStatement ps;
   static ResultSet rs;
   static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
   public static void main(String args[])
   {
            int r,p;
            String sn;
       try
       {
                        System.out.println("Enter the Rno");
                        r=Integer.parseInt(br.readLine());
            System.out.println("Enter the Student Name");
            sn=br.readLine();
            System.out.println("Enter the Per");
            p=Integer.parseInt(br.readLine());
           Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
               cn=DriverManager.getConnection("jdbc:odbc:Stud","","");
           String str="insert into stud values(?,?,?)";
                        ps=cn.prepareStatement(str);
                        ps.setInt(1,r);
            ps.setString(2,sn);
            ps.setInt(3,p);

                        int k=ps.executeUpdate();
                        if(k>0)
            {
                        System.out.println("Record Is Added");
            }
            else
            {
                        System.out.println("Error");
            }
        }
        catch(Exception er)
        {
             System.out.println(er);
        }

    }

No comments:

Ebook

Ebook
Ebook