[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

JDBC INSERT with autonumbers

Posted on 2003-11-24
10
Medium Priority
?
350 Views
Last Modified: 2008-03-03
Here's the problem query:

int update = statement1.executeUpdate(
            "INSERT INTO diary(name,date,time,)
   VALUES ( " 
   + z.getName() + "','" + z.getDate() + "','" + z.geTime()")");

The problem is, in the database table diary preceeding 'name' there is an autonumber acting as the 'primary key', called diarykey.

What's the best way to include this in the query, because when i leave it out i get 'INSERT SYNTAX ERRORS'!

0
Comment
Question by:RedRichard
  • 5
  • 3
  • 2
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9809823
You shouldn't do. The problem is more likely to be with the way you're putting in the other params.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9809836
Which is not too surprising - you've reverted to your earliest code in http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20805871.html. Why?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9809839
you are missing a ' before z.getName() ;

you have an extra "," after time

and you should use a preparedstatement;

Assiming all 3 fields are strings;

PreparedStatement ps = connection.prepareStatement( "INSERT INTO diary( name,date,time ) VALUES ( ?, ?, ? )" ) ;
ps.setString( 1, z.getName() ) ;
ps.setString( 2, z.getTime() ) ;
ps.setString( 3, z.getDate() ) ;
int update = ps.executeUpdate() ;

Tim
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Assisted Solution

by:TimYates
TimYates earned 80 total points
ID: 9809841
whoops...sorry CEHJ :-/
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9809847
No problem ;-)
0
 

Author Comment

by:RedRichard
ID: 9811437
Ok, so I should have used a PreparedStatement, but the original problem
still remains.

If the first column in the table is an autonumber called diarykey how does this get handled?

Do you just ignore it from the java side?


0
 
LVL 86

Accepted Solution

by:
CEHJ earned 320 total points
ID: 9811496
>>Do you just ignore it from the java side?

Yes
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9811645
:-)
0
 

Author Comment

by:RedRichard
ID: 9811646
em, the assissted answer was actually TimYates' first post.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9811671
:-)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month18 days, 16 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question