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

    Default Java database/method display problem

    Hi

    I am having some trouble with a program I am trying to run. Well not really a problem it runs as intended but it is not working as would like, any help appreciated.

    I have a MySQL/JDBC connection with a query that is supposed to return the statement details for a customer of an atm machine.

    public int checkStatement()
    {
    Date dateWithdrawn;
    int available;
    try
    {
    Class.forName("com.mysql.jdbc.Driver");
    String url = "jdbc:mysql://localhost:3306/atm";
    Connection connection = DriverManager.getConnection( url, "user", "password");
    String iQuery = "select balance + overdraft as funds from account" +
    " where account_number = ?";
    PreparedStatement preparedStatement = connection.prepareStatement(iQuery);
    preparedStatement.setString(1, accountNumber);
    ResultSet rs = preparedStatement.executeQuery();
    while(rs.next())
    {
    int funds = rs.getInt("funds");
    System.out.println("Available funds" + "£ "+funds);
    }
    connection.close();
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    try
    {
    Class.forName("com.mysql.jdbc.Driver");
    String url = "jdbc:mysql://localhost:3306/atm";
    Connection connection = DriverManager.getConnection(
    url, "user", "password");


    String query = "select m.statement_id, m.dateOut, m.amountOut from " +
    " (select statement.statement_id, statement.dateOut, statement.amountOut from " +
    " statement, account where account.account_number = statement.account_number and "
    + "account.account_number = ? and dateOut between curdate()-5 and curdate() order by " +
    " statement.statement_id desc limit 8) as m order by m.statement_id asc";
    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 +
    "\t-" + checkStatement);
    }

    connection.close();

    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    return checkStatement;
    }

    when the program is run I get the following displayed in the compiler window:-

    Available funds£ 88

    Date2009-02-20 -10

    Date2009-02-20 -10

    Date2009-02-20 -20

    Date2009-02-20 -30

    Date2009-02-20 -30

    Date2009-02-20 -10

    Date2009-02-20 -10

    Date2009-02-20 -40

    the problem is that I want this information to display in a JtextArea called displayArea. And when I attempt to create a variable to hold this particular information, I only get a single figure - 40 - the last value of the variable checkStatement after the while loop has finished in the query string and the value returned by the method. I am just wondering how I could get the entire string as displayed, placed wholely in the JTextArea.

    Any help would be much appreciated.

    theLinuxGuy

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

    Default Problem continued

    I have tried to use a string:-

    checkStatement = "\nDate" + dateWithdrawn +
    "\t£-" + makeStatement;
    and this has worked in sending the very last line:-

    Date2009-02-20 £-40

    to the JTextArea.

    Would anyone have a suggestion as to how I might get the preceding lines displayed.

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

    Default Problem Continued

    I have also tried to use an array:-
    while(result.next())
    {
    dateWithdrawn = result.getDate("dateOut");
    makeStatement = result.getInt("amountOut");
    for(int i = 0; i <= 7; i++)
    {
    checkStatement [i] = "\nDate" + dateWithdrawn +
    "\t£-" + makeStatement;
    }
    }
    I placed this in the account class

    and then called the array using another string array from the JTextArea class. The following code activates when a key(four) is clicked.
    if(source == four)
    {
    String[] m = new String[8];
    displayArea.setText("");

    for(int y = 0; y <= 7; y++)
    {

    m [y] = a.checkStatement();
    displayArea.append(m[y] + "");
    }
    }

    but all I got was
    Date2009-02-20 £-40
    Date2009-02-20 £-40
    Date2009-02-20 £-40
    Date2009-02-20 £-40
    Date2009-02-20 £-40
    Date2009-02-20 £-40
    Date2009-02-20 £-40
    Date2009-02-20 £-40

    which is last row from the database repeated eight times over.

    theLinuxGuy

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

    Default Problem solved

    This works:-
    while(result.next() && i != 8)
    {
    dateWithdrawn = result.getDate("dateOut");
    makeStatement = result.getInt("amountOut");
    checkStatement [i] = "\nDate" + dateWithdrawn +
    "\t£-" + makeStatement;
    i++;

    }

    theLinuxGuy

Similar Threads

  1. Replies: 3
    Last Post: 01-29-2009, 11:20 AM
  2. Replies: 0
    Last Post: 12-04-2008, 06:39 PM
  3. Replies: 29
    Last Post: 09-25-2008, 08:55 PM
  4. Problem connecting to my Database using Java
    By javaneed in forum New To Java
    Replies: 3
    Last Post: 08-13-2007, 10:35 AM
  5. How to display a database-image in a page?
    By simon in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 12:56 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
  •