Results 1 to 5 of 5
- 02-12-2009, 04:12 PM #1
Member
- Join Date
- Dec 2008
- Posts
- 44
- Rep Power
- 0
MySQL/JDBC Prepared Statement Select query
Hi
I am having trouble with a select query using a prepared Statement.
I have as follows:-
String query = "select statement.dateOut, statement.amountOut from statement, account where "
+ " account.account_number = statement.account_number and account.account_number = ?";
PreparedStatement ps = connection.prepareStatement(query);
ps.setString(1, accountNumber);
ResultSet rs = ps.executeQuery(query);
while(rs.next())
{
dateWithdrawn = rs.getDate("dateOut");
checkStatement = rs.getInt("amountOut");
System.out.println("\n Balance " + accountBalance +
"\nAmount Out " + checkStatement +
"\tdate" + dateWithdrawn);
}
I keep getting the message:-
com.mysql.jdbc.exceptions.MySQLSyntaxErrorExceptio n: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 1
Any help appreciated
the LinuxGuy
- 02-12-2009, 04:31 PM #2
Member
- Join Date
- Feb 2009
- Location
- Italy
- Posts
- 51
- Rep Power
- 0
try to put "" around the prepared variable
maybe works like that.... if not try to check the exact SQL syntax of your MySQL versionJava Code:String query = "select statement.dateOut, statement.amountOut " +"from statement, account " +"where account.account_number = statement.account_number " +"and account.account_number = \"?\"";
- 02-12-2009, 04:41 PM #3
Member
- Join Date
- Dec 2008
- Posts
- 44
- Rep Power
- 0
Thanks Wolfcro
It still doesnt work, I checked the MySQL syntax and I dont think select queries are supported by prepared Statements.
theLinuxGuy
- 02-12-2009, 05:02 PM #4
Use
instead ofJava Code:ResultSet rs = ps.executeQuery();
In other words do not pass query string.Java Code:ResultSet rs = ps.executeQuery(query);
- 02-12-2009, 05:29 PM #5
Member
- Join Date
- Dec 2008
- Posts
- 44
- Rep Power
- 0
Hi ProjectKaiser
Thanks for your input it works perfectly.
String query = "select dateOut, amountOut from statement, account" +
" where account.account_number = statement.account_number and " +
"account.account_number = ? ";
PreparedStatement ps = connection.prepareStatement(query);
ps.setString(1, accountNumber);
result = ps.executeQuery();
while(result.next())
{
dateWithdrawn = result.getDate("dateOut");
checkStatement = result.getInt("amountOut");
System.out.println(
"\nDate" + dateWithdrawn +
"\tAmount" + checkStatement);
}
theLinuxGuy
Similar Threads
-
MySQL/JDBC prepared statement problem
By thelinuxguy in forum Advanced JavaReplies: 3Last Post: 02-11-2009, 11:21 PM -
Mysql/JDBC update query problem
By thelinuxguy in forum Advanced JavaReplies: 3Last Post: 02-11-2009, 09:56 PM -
Prblem in Prepared Statement
By haneeshrawther in forum JDBCReplies: 2Last Post: 04-25-2008, 09:49 AM -
Using Prepared Statement
By Java Tip in forum Java TipReplies: 0Last Post: 02-06-2008, 09:22 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks