Saturday 22 October 2016

JDBC Insertion Operation using Java & MySQL Data Base

import java.sql.*;

public class JDBCExampleInsert
{


   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; 
   static final String DB_URL = "jdbc:mysql://localhost/employee";


   static final String USER = "username";
   static final String PASS = "password";
  
   public static void main(String[] args)
   {
   Connection conn = null;
   Statement stmt = null;

   try
   {

      Class.forName("com.mysql.jdbc.Driver");

      System.out.println("Connecting to database...");
      conn = DriverManager.getConnection(DB_URL, USER, PASS);
    
       con.setAutoCommit(false);

      stmt = conn.createStatement();
     
    

      String sql = "CREATE TABLE employee" + "(eid INTEGER not NULL, " + " ename VARCHAR(255), " + 
                   " designation VARCHAR(255), " +  " PRIMARY KEY ( eid ))"; 


     stmt.addBatch(SQL);


     String sql= "INSERT INTO employee VALUES(' "+eid+" ',' "+ename+" ',' "+designation+" ') ";

     stmt.addBatch(SQL);

    
     String sql= "UPDATE  employee " + " SET designation  = " Asst Deve"  WHERE eid = "  '+eid+" ',' "+ename+" ',' "+designation+" ') ";

     stmt.addBatch(SQL);

           int[] count = stmt.executeBatch();



    }
     catch(SQLException se)
    {
      se.printStackTrace();
     con.rollback();
   }
   
     catch(Exception e)
     {
      e.printStackTrace();
    }

    finally
      {
      try
        {
         if(stmt!=null)
            stmt.close();
      }
      catch(SQLException se2)
      {
      }

      try
      {
         if(conn!=null)
            conn.close();
      }
        catch(SQLException se)
       {
         se.printStackTrace();
      }
   }
}
}

No comments:

Post a Comment