Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

JDBC INSERT with autonumbers

Posted on 2003-11-24
10
Medium Priority
?
349 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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:
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

636 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