Results 1 to 4 of 4
- 02-11-2009, 10:23 PM #1
Member
- Join Date
- Dec 2008
- Posts
- 44
- Rep Power
- 0
MySQL/JDBC prepared statement problem
Hi
I am working with a prepared statement in order to make an insert query. I am trying to insert todays date into a mysql table in order to keep a record of the day of a transaction. The query is as follows:-
PreparedStatement pstatement = connection.prepareStatement(
"insert statement set dateOut = ?, amountOut = ? where account_number = ?");
pstatement.setDate(1, getDate());
pstatement.setInt(2, amountToWithdraw);
pstatement.setString(3, accountNumber);
I keep getting the error message:-
Account.java:153: cannot find symbol
symbol : method setDate(int,java.util.Date)
location: interface java.sql.PreparedStatement
pstatement.setDate(1, getDate());
^
1 error
where the getDate() method returns todays date as shown:
//method for todays date
public Date getDate()
{
Date d = new Date();
return d;
}
I would appreciate any help as to what is going wrong. I checked and there is a setDate() method for prepared statements.
Sincerely
theLinuxGuy
- 02-11-2009, 10:46 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Read the API docs for PreparedStatement and it's setDate method.
It takes a java.sql.Date and you're feeding it a java.util.Date. Do you see a problem there?
- 02-11-2009, 10:52 PM #3
Member
- Join Date
- Dec 2008
- Posts
- 44
- Rep Power
- 0
Problem solved
Hi masijade
Yeah I sorted the problem, I found an example at java2s
This showed me the method example:-
public static java.sql.Date getCurrentJavaSqlDate() {
java.util.Date today = new java.util.Date();
return new java.sql.Date(today.getTime());
then:-
java.sql.Date date = getCurrentJavaSqlDate();
pstmt.setDate(2, date);
So I now have that part working.
Thanks for your help.
Kind Regards
theLinuxGuy
- 02-11-2009, 11:21 PM #4
Member
- Join Date
- Dec 2008
- Posts
- 44
- Rep Power
- 0
Revised Problem Solved MySQL JDBC
Hi
Just to add. The date problem was solved but there was an error with regard to the prepared statement query.
The previous query was not working. The one below does:-
try
{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/ddd";
Connection connection = DriverManager.getConnection(
url, "root", "password");
String query = "insert into statement(dateOut, amountOut, account_number) values(?, ?, ?)";
java.sql.Date date = getCurrentJavaSqlDate();
PreparedStatement preparedStatement = connection.prepareStatement(query); // create a statement
preparedStatement.setDate(1, date); // set input parameter 1
preparedStatement.setInt(2, amountToWithdraw); // set input parameter 2
preparedStatement.setString(3, accountNumber); // set input parameter 3
preparedStatement.executeUpdate(); // execute insert statement
connection.close();
}
catch(Exception e)
{
e.printStackTrace();
}
In case this ever helps
regards
theLinuxGuy
Similar Threads
-
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 -
Statement or Prepared Statement ?
By paty in forum JDBCReplies: 3Last Post: 08-01-2007, 04:45 PM


LinkBack URL
About LinkBacks

Bookmarks