Solved

JDBC INSERT problems

Posted on 2003-11-22
5
282 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse shortcuts 9 56
factorial example 4 41
even odd program using while loop 3 43
Java string replace 11 48
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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 …

823 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