Solved

JDBC INSERT problems

Posted on 2003-11-22
5
279 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
Comment Utility
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
Comment Utility
The sql would be better with the column names too:

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

Author Comment

by:RedRichard
Comment Utility
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
Comment Utility
:-)
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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 first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

744 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

12 Experts available now in Live!

Get 1:1 Help Now