Results 1 to 4 of 4
  1. #1
    thelinuxguy is offline Member
    Join Date
    Dec 2008
    Posts
    44
    Rep Power
    0

    Default 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

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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?

  3. #3
    thelinuxguy is offline Member
    Join Date
    Dec 2008
    Posts
    44
    Rep Power
    0

    Default 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

  4. #4
    thelinuxguy is offline Member
    Join Date
    Dec 2008
    Posts
    44
    Rep Power
    0

    Default 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

  1. Mysql/JDBC update query problem
    By thelinuxguy in forum Advanced Java
    Replies: 3
    Last Post: 02-11-2009, 09:56 PM
  2. Prblem in Prepared Statement
    By haneeshrawther in forum JDBC
    Replies: 2
    Last Post: 04-25-2008, 09:49 AM
  3. Using Prepared Statement
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-06-2008, 09:22 AM
  4. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 04:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •