Docstoc

Insert import java io BufferedReader import java io InputStreamReader import java

Document Sample
Insert import java io BufferedReader import java io InputStreamReader import java Powered By Docstoc
					import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.*;

public class Insert {


     public static void main(String[] args)
     {
           Connection con=null;
           PreparedStatement pstatement=null;

           String driver="com.mysql.jdbc.Driver";
           String url="jdbc:mysql://localhost:3306/";

           String userName="root";
           String password="root";
           String database="employee";

           String insertquery="insert into data values(?,?,?,?,?)";

           String   name;
           String   address;
           String   id;
           String   post;
           double   salary;

           String choice=null;

           BufferedReader reader=null;

           do
           {
                 try
                 {
                       reader=new BufferedReader(new
InputStreamReader(System.in));

                        Class.forName(driver);

     con=DriverManager.getConnection(url+database,userName,password);

                        System.out.println("Enter name: ");
                        name=reader.readLine();

                        System.out.println("Enter address: ");
                        address=reader.readLine();

                        System.out.println("Enter id: ");
                        id=reader.readLine();

                        System.out.println("Enter post: ");
                        post=reader.readLine();
                         System.out.println("Enter salary: ");
                         salary=Double.parseDouble(reader.readLine());

                         pstatement=con.prepareStatement(insertquery);

                         pstatement.setString(1,name);
                         pstatement.setString(2,address);
                         pstatement.setString(3,id);
                         pstatement.setString(4,post);
                         pstatement.setDouble(5,salary);



                         int result=pstatement.executeUpdate();

                         if(result==1)
                         {
                               System.out.println("Record save in
database");
                         }
                         else
                         {
                                System.out.println("Error");
                         }

                         System.out.println("Want to enter more data?
[Y][N]");
                         choice=reader.readLine();
                   }
                   catch(Exception exception)
                   {
                         System.out.println("Exception is: " + exception);
                   }


                   finally
                   {
                         try
                         {
                         con.close();
                         }
                         catch(Exception e)
                         {
                               System.out.println("Exception is: " + e);
                         }
                   }
              }
              while(choice.equalsIgnoreCase("y"));

     }

}

				
DOCUMENT INFO
Shared By:
Stats:
views:65
posted:3/31/2012
language:English
pages:2
Description: This document is the complete java program showing JDBC to insert data into the database mysql.