Solved

JDBC INSERT problems

Posted on 2003-11-22
5
285 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
[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
  • 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

Industry Leaders: 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
Facing this issue for maven proxy setting 2 43
Java: anonymous class 4 52
junit as external jar or library 7 56
Strange router problem - can't access hotmail.com 14 107
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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 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 …

739 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