Solved

JDBC INSERT problems

Posted on 2003-11-22
5
284 Views
Last Modified: 2010-03-31
I have tested the following:

      int update = statement1.executeUpdate(
            "INSERT INTO tablename " +
             "VALUES ('1', 10.25)");

and it works.

However if I want the values to be the result of method calls and not literally inserted, what syntax do i need?

      int update = statement1.executeUpdate(
            "INSERT INTO tablename " +
             "VALUES ('currentKey', 'z.getPrice()')");

this wont work even though currnetKey is an int and z.getName() returns a double.  I get a 'criteria mismatch problem'.  Do i need to wrap?  currentKey holds number in the db and z.getName() is for a column that holds decimal in the db.

confused.
0
Comment
Question by:RedRichard
  • 4
5 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 100 total points
ID: 9802926
The simplest way is to use a PreparedStatement

String sql = "INSERT INTO tablename VALUES (?, ?)";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setInt(1, currentKey);
pstmt.setDouble(2, z.getPrice());
pstmt.executeUpdate();


0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9802928
The sql would be better with the column names too:

String sql = "INSERT INTO tablename (col1, col2) VALUES (?, ?)";
0
 

Author Comment

by:RedRichard
ID: 9802955
Thanks CEHJ,

Prepared statements might help me cut down on the headaches my
style of sql was giving me!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9802957
:-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9802976
Bear in mind that theoretically you may take a performance hit from a PS as they are designed to optimise queries in loops and that comes at an DBMS price. However, this should not be noticeable. If, for some reason, it is. Let us know.
0

Featured Post

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error in @AspectJ Based AOP with Spring 2 23
hibernate example for saving data 19 73
Java array 21 79
How to fix  socket closed error 11 23
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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:

740 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