Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

MySQL's AUTO_INCREMENT and JDBC

843859Dec 7 2005 — edited Dec 9 2005
I have the following that doesnt seem to work. The custID field i haven't specified in the prepare statement (because it's an AUTO_INCREMENT value , but for some reason it won't work without it. Nothing is added to the table when the method is completely run.
int rows = 0;
Connection conn = getConnection();
PreparedStatement pstmt = null;

try{


      pstmt = conn.prepareStatement(

     "insert into Customers values(" +
    "?," + //customername

);

       pstmt.setString(1, customerBean.getCustomerName());

}

catch (SQLException se)
{
    rollback(conn);

    System.out.println(msg);
     System.out.println(se);
     throw new Exception(se);

} 
| Field | Type | Null | Key | Default | Extra |
-------------------------------------------------------------+
| CUST_ID | int(11) | NO | PRI | NULL | auto_increment |
| CUST_NAME | varchar(100) | YES | | NULL | |

Any ideas? I want a proper way of forumlating a query using a prepared statement and using the AUTO_INCREMENT value so that when a new row is added the cust_Id field is incremented.

Another question, why is the SQLException not being caught? I have another method which gets all the rows from a table, if i change the prepared statement to an invalid table then it gives me an exception. I was previously using Oracle, SQLException was being caught when a duplicate entry was added.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 6 2006
Added on Dec 7 2005
6 comments
175 views