Solved

JDBC INSERT with autonumbers

Posted on 2003-11-24
10
341 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
 
LVL 35

Assisted Solution

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

Expert Comment

by:CEHJ
ID: 9809847
No problem ;-)
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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 80 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Updating Java 9 87
json example 39 115
wordcount challenge 11 76
Java passing arguments (type error) 2 35
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now