?
Solved

JDBC INSERT with autonumbers

Posted on 2003-11-24
10
Medium Priority
?
348 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

770 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