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

    Default Mysql/JDBC update query problem

    Hi

    I am trying to make an update query for java jdbc/mysql.
    The query runs as follows:-
    result = statement.executeUpdate("update account set balance = balance - " + amountToWithdraw +
    " where account.account_number = '" + accountNumber + "'" + "and account.sortcode = " + "'"+sortCode+"'");

    I keep getting the error message:-
    Account.java:126: incompatible types
    found : int
    required: java.sql.ResultSet

    result = statement.executeUpdate("update account set balance = balance - " + amountToWithdraw +
    ^
    1 error

    I know that the syntax is correct because I have it working in mysql, I dont quite know what this means.

    Any help would be appreciated

    theLinuxGuy

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

    Default Problem Resolved

    Hi

    I have corrected the mistake - the problem was I had adjusted a select query that had used a result set with the statement connection, but when executing an update there is no such result set, nothing is returned from the query so there is no result set.

    I corrected the query by simply adding the line statement = connection.createStatement(); and removed the previous line statement = connection.createStatement(//resultSetdetails);.

    as follows if it helps anybody ever!

    public int makeWithdrawel(int amountToWithdrawIn)
    {
    amountToWithdraw = amountToWithdrawIn;
    try
    {
    Class.forName("com.mysql.jdbc.Driver");
    String url = "jdbc:mysql://localhost:3306/atm";
    Connection connection = DriverManager.getConnection(
    url, "user", "password");
    statement = connection.createStatement();
    statement.executeUpdate("update account set balance = balance - " + amountToWithdraw +
    " where account.account_number = '" + accountNumber + "'");

    connection.close();

    }

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Just an off-topic suggestion. Look into using PreparedStatement. It seems like a pain in the aft, but CreateStatement is *very*, as in noticeable, slow. You can reuse prepared statements, they are easier to write and debug, and the stop SQL injection attacks.

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

    Default Prepared Statements

    Hi Steve11235

    Thanks for the suggestion, Ill look into prepared statements.

    Kind regards

    theLinuxGuy

Similar Threads

  1. Java JDBC/MySQL appropriate Syntax
    By thelinuxguy in forum Advanced Java
    Replies: 7
    Last Post: 02-10-2009, 07:57 PM
  2. Replies: 1
    Last Post: 08-22-2008, 06:28 PM
  3. ms access jdbc query problem
    By Fleur in forum New To Java
    Replies: 2
    Last Post: 04-30-2008, 06:55 PM
  4. unable to update MYSQL with values from jsp page
    By koushika in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 03-27-2008, 01:35 AM
  5. help ... JDBC or PHP/MySQL
    By bluebarca in forum Advanced Java
    Replies: 1
    Last Post: 11-16-2007, 10:05 AM

Posting Permissions

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