aspose file tools*
The moose likes JDBC and the fly likes How Do I Insert .... ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How Do I Insert .... ?" Watch "How Do I Insert .... ?" New topic
Author

How Do I Insert .... ?

JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
I have a table 'message' in the MySQL database. To make it simple; say, I have three fields in that table: thread_ID, thread_topic, and thread_body.
The thread_ID field is AUTO_INCREMENT; therefore, I do not insert any value into that column. The value of that field starts with 1 when the first row is inserted.
Now, I have the values for thread_topic and thread_body and want to insert those values into the 'message' table to create the first record in that table.
1. Is thread_ID in column 0 (zero)? and thread_topic in column 1, thread_body in column 2?
2. where and how do I indicate that there is a problem insert a row into the table 'message'?
3. Do I code this way:

[ December 25, 2003: Message edited by: JiaPei Jen ]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

I see a couple of things.
1. You can't use a Statement to do what you are trying to do. You need to use a PreparedStatement.
2. You are making it too complicated. Try something like this instead.

As far as how you indicate that there is a problem, you should be catching an SQLException rather mayeb than your custom DatabaseException. An SQLException will tell you if there was a problem interacting with the database. Hope that helps.


GenRocket - Experts at Building Test Data
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
Thanks for your reply.

will thread_ID generate a number automatically if we insert a 'null' value?
(The column thread_ID is AUTO_INCREMENT when the table was created.)
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by JiaPei Jen:
Thanks for your reply.

will thread_ID generate a number automatically if we insert a 'null' value?
(The column thread_ID is AUTO_INCREMENT when the table was created.)

It sure will. You can also leave thread_ID out of the equation completely and it will work.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How Do I Insert .... ?