Solved

JDBC INSERT with autonumbers

Posted on 2003-11-24
10
342 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
network + 7 80
groupNoAdj 7 86
Java Message handling in Service Layer 3 57
why cannot we forward request once the response is committed 2 33
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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

920 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

13 Experts available now in Live!

Get 1:1 Help Now