Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

java jdbc batch example

Hi,

import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.SQLException;

public class MainClass2 {

  private static final String EMPLOYEE_TABLE = "create table MyEmployees6 ( "
      + "   id INT PRIMARY KEY, firstName VARCHAR(20), lastName VARCHAR(20), "
      + "   title VARCHAR(20), salary INT " + ")";

  public static Connection getConnection() throws ClassNotFoundException, SQLException {
    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@localhost:1521:XE";
    String username = "sample";
    String password = "admin";
    Class.forName(driver);
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  }

  public static void main(String args[]) throws SQLException {
    Connection conn = null;
    
    
    Statement stmt = null;
    try {
      conn = getConnection();
      stmt = conn.createStatement();
      // Set auto-commit to false
      conn.setAutoCommit(false);
      stmt.executeUpdate(EMPLOYEE_TABLE);
      
      // Create SQL statement
     // String SQL = "INSERT INTO Employees (id, first, last, age) " + 
     //              "VALUES(200,'Zia', 'Ali', 30)";
      // Add above SQL statement in the batch.
      //stmt.addBatch(SQL);
      
      stmt.addBatch("insert into MyEmployees6(id, firstName) values(100, 'A')");      
      stmt.addBatch("insert into MyEmployees6(id, firstName) values(200, 'B')");
      stmt.addBatch("insert into MyEmployees6(id, firstName) values(300, 'A')");      
      stmt.addBatch("insert into MyEmployees6(id, firstName) values(300, 'B')");
      conn.commit();
     // stmt.executeUpdate("update MyEmployees6 set FIRSTNAME='user1' where ID='100'");      
     // stmt.executeUpdate("update MyEmployees6 set FIRSTNAME='user2' where ID='200'"); 
      System.out.println("CreateEmployeeTableOracle: main(): table created.");
    } /*catch (ClassNotFoundException e) {
      System.out.println("error: failed to load Oracle driver.");
      e.printStackTrace();
    } catch (SQLException e) {
      System.out.println("error: failed to create a connection object.");
      e.printStackTrace();
    } */catch (Exception e) {
      System.out.println("other error:");
      e.printStackTrace();
      conn.rollback();
    } finally {
      try {
        stmt.close();
        conn.close();
      } catch (Exception e) {
      }
    }
  }
}
 

Open in new window


i was trying above code to see how jdbc batch works


even though same primiary key id  300 appearing two times i still see table got created without any data.

stmt.addBatch("insert into MyEmployees6(id, firstName) values(100, 'A')");      
      stmt.addBatch("insert into MyEmployees6(id, firstName) values(200, 'B')");
      stmt.addBatch("insert into MyEmployees6(id, firstName) values(300, 'A')");      
      stmt.addBatch("insert into MyEmployees6(id, firstName) values(300, 'B')");


i expected to see unique constraint exception in eclipse but never saw that.  any complete example on jdbc batch using oracle databaseplease advise
Avatar of gudii9
gudii9
Flag of United States of America image

ASKER

when i run same example again i am getting

java.sql.SQLSyntaxErrorException: ORA-00955: name is already used by an existing object

i wonder what it means ? please advise
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

When same I'd why no errors out like unique constraint
Your program is not safe to be run multiple times.
Guddi9 if our roles were reversed and I asked you a question in this format, do you think you would want me to remove the commented/redundant code before asking the question? Honestly think about it. Question asking is a technique itself. I'd like to help you but don't want to spend time separating the woods from the trees. You really need to practice clean code.
By the way, you should read up on jdbc and batches.  Doing army.addBatch(..) is not enough. You need to execute the statement too!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Guddi9 you have not understood.