LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   java code executing SQL statements problem (https://www.linuxquestions.org/questions/programming-9/java-code-executing-sql-statements-problem-252523/)

randomx 11-08-2004 02:53 PM

java code executing SQL statements problem
 
Please enlighten me. This is driving me crazy.

I don't know if this is a database or java code related problem.

JVM: 1.4.2
Database: Oracle 9i

Objective: trying to add a row into a table

Problem: 1. SQL statement never gets executed.
2. Application freezes. I have to terminate it myself.

Any Ideas?


Code:

void addCourse(){

try {
    DriverManager.registerDriver ( new oracle.jdbc.driver.OracleDriver () ) ;
  System.out.println("Driver registered");  //---> ok.
 
Connection conn = DriverManager.getConnection ( dbPath, username, password ) ;
  System.out.println("Connected");  // -----> it connects fine

  ResultSet rset = stmt.getResultSet();
  System.out.println("getting result set");  //-----> gets the resultSet fine

  Statement stmt = conn.createStatement();
  System.out.println("statement created");  //------> it creates statement

/*
7 = are the credits. Number on table. Length allowed 5.
CS 500 = are the course code. Varchar2 on table. Length allowed 20
Java = Actual course name. Varchar2 on table. Length 30
3 = credits. Number on table. Length 5.
*/
          // Prepare a statement to insert a record
          String sql = "INSERT INTO course VALUES (7 , 'CS 500', 'Java', 3 )"; //-->ok. Tested it manually 
 
          // Execute the insert statement
        stmt.executeUpdate(sql);  //----> *** java program freezes right here.***
        System.out.println("sql executed");  //--> Checked table. SQL never gets executed.
}
catch ( SQLException ex ) {  //---> no error messages thrown.
System.err.println(ex.getMessage());
  System.err.println(ex.getErrorCode());
  System.err.println(ex.getSQLState());
 }
}


csfalcon 11-09-2004 10:59 AM

I usually use prepareStatement(String) instead of createStatement()

Code:

String query = "INSERT INTO course VALUES (7 , 'CS 500', 'Java', 3 )";

PreparedStatement ps = conn.prepareStatement(query);

ps.executeUpdate();



All times are GMT -5. The time now is 02:53 AM.