File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Insert into statement using JDBC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Insert into statement using JDBC" Watch "Insert into statement using JDBC" New topic
Author

Insert into statement using JDBC

Kassi Hill
Greenhorn

Joined: Sep 29, 2003
Posts: 24
I am trying to write an SQL INSERT INTO statement to insert data into a table in MS Access. The method I am writing is accepting an object and then getting the specific information needed to insert into the database out of the object. I have having trouble with the wording of the SQL statement itself and I would appreciate any help.
int qID
String qText
boolean qType
int qCorrect
sql = "INSERT INTO Question(qID, qText, qType, qCorrect)
VALUES(+qID, +"qText", + qType, + qCorrect)");

I also need to write an UPDATE statment based on the same idea, where the method will be sent an object. Could someone give me an example of how the sql statement will look?
Thanx again!!!
Adrian Yan
Ranch Hand

Joined: Oct 02, 2000
Posts: 688

If you run this command alot, I recommend you to use PreparedStatement, which is alot cleaner.
[ October 15, 2003: Message edited by: Adrian Yan ]
Kassi Hill
Greenhorn

Joined: Sep 29, 2003
Posts: 24
Thank you VERY much, this appears to have solved my problem. Would the
update statement be the same excet using UPDATE instead of INSERT INTO??
Prasma Kankut
Greenhorn

Joined: Oct 15, 2003
Posts: 21
Originally posted by Kassi Hill:
Would the
update statement be the same excet using UPDATE instead of INSERT INTO??

No. The UPDATE query will look like this
UPDATE table_name SET column_name = value
Be sure to add a WHERE clause, so that only the corresponding row(s) will be updated. Else you will end up updating all the rows in the table.
The syntax will be
UPDATE table_name SET column_name = value WHERE condition
[ October 15, 2003: Message edited by: Prasma Kankut ]

<i>The fact that today we know more than yesterday is good news about today, not bad news about yesterday - Ilja Preuss</i>
Kassi Hill
Greenhorn

Joined: Sep 29, 2003
Posts: 24
Thanx again for your help. If I understand correctly, my Upate statement should look something like:

sql = "UPDATE Question SET qID, qText, qType, qCorrect = VALUES(" + qID + "," + qText + "," + qType + "," + qCorrect + ") WHERE qID" = qID;
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Kassi Hill:
Thanx again for your help. If I understand correctly, my Upate statement should look something like:

sql = "UPDATE Question SET qID, qText, qType, qCorrect = VALUES(" + qID + "," + qText + "," + qType + "," + qCorrect + ") WHERE qID" = qID;

"UPDATE Question SET qID = " + qID + ", qText = " + qText + "........
[ October 15, 2003: Message edited by: Gregg Bolinger ]

GenRocket - Experts at Building Test Data
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Insert into statement using JDBC