Results 1 to 6 of 6
  1. #1
    esgol is offline Member
    Join Date
    Dec 2010
    Posts
    29
    Rep Power
    0

    Default Why changes arent seen when i re-conect to my Database through a Java API?

    Hi, I have a simple Java API, an ATM which is a window with cardlayout

    I conect with The database returns that the Credit Amount is 0 and i disconect

    I go to deposit money press 40$ and in the event listener the database returns that the credit Amount is 40. all ok and i disconnect

    BUT IF I GO TO "see remaining credit" i re-conect inside the button listeners and says me Amount 0 AGAIN!! :eek:

    WHY? :eek:

    edit:examples deleted due to complexity
    Last edited by esgol; 01-19-2011 at 05:09 PM.

  2. #2
    esgol is offline Member
    Join Date
    Dec 2010
    Posts
    29
    Rep Power
    0

    Default

    Here i have a more clear example.
    I conect to a database, i select its table (already created ( seems the only thing that holds lol )
    We see amount 0
    We update it to +40
    We re select it now its ok 40

    However if you re-run the programma you always get the same result while you should get 80 the 2ond time 120 the 3rd etc...

    import java.sql.*;

    public class MysqlConnect{
    public static void main(String[] args) {

    //CONNECT
    System.out.println("MySQL Connect Example.");
    Connection conn = null;
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "MySQL";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "root";
    String password = "esgordoreth";
    try {
    Class.forName(driver).newInstance();
    conn = DriverManager.getConnection(url+dbName,userName,pa ssword);
    System.out.println("Connected to the database");
    } catch (Exception e) {
    e.printStackTrace();
    }

    //CREATE TABLE
    try
    {
    String table = "CREATE TABLE CREDIT(Amount integer)";
    Statement st = conn.createStatement();
    st.executeUpdate(table);
    System.out.println("Table creation process successfully!");
    }
    catch(SQLException s){
    System.out.println("Table all ready exists!");
    }

    //SELECT
    try{
    conn.setAutoCommit(false);
    Statement st = conn.createStatement();
    String sqlRetrieveAmount = "select * from CREDIT";
    ResultSet Result = null;
    Result = st.executeQuery(sqlRetrieveAmount);
    int Amount=10;
    System.out.println("Amount:");
    while (Result.next()) {
    //System.out.print(Result.getInt(1));
    Amount=Result.getInt(1);
    }
    System.out.print(Amount);
    System.out.println("");
    Result.close();
    } catch (Exception e1) {
    e1.printStackTrace();
    }

    //SELECT-UPDATE
    try{
    conn.setAutoCommit(false);
    Statement st = conn.createStatement();
    String sqlRetrieveAmount = "select * from CREDIT";
    int Amount=0;
    int UpdatedAmount=0;
    ResultSet Result = null;
    Result = st.executeQuery(sqlRetrieveAmount);

    System.out.println("Amount:");
    while (Result.next())
    {
    Amount=Result.getInt(1);
    }
    UpdatedAmount=Amount+40;
    System.out.print(Amount);
    String sqlUpdateDb = "update CREDIT set Amount="+UpdatedAmount;
    st.executeUpdate(sqlUpdateDb);
    System.out.println("\nUpdatedAmount");
    System.out.print(UpdatedAmount);

    Result = st.executeQuery(sqlRetrieveAmount);
    System.out.println("\nNow the Amount is:");
    while (Result.next()) {
    Amount=Result.getInt(1);
    }
    System.out.print(Amount);
    Result.close();

    System.out.println("Table updated.");
    } catch (Exception e) {
    e.printStackTrace();
    }

    //DISCONNECT
    try {
    conn.close();
    System.out.println("Disconnected from database");
    } catch (Exception e) {
    e.printStackTrace();
    }




    }
    }
    Netbeans Output (Same every time. WHY? )
    MySQL Connect Example.
    Connected to the database
    Table all ready exists!
    Amount:
    0
    Amount:
    0
    UpdatedAmount
    40
    Now the Amount is:
    40Table updated.
    Disconnected from database
    Last edited by esgol; 01-19-2011 at 05:10 PM.

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

    Default

    You call
    Java Code:
    conn.setAutoCommit(false);
    and then never call
    Java Code:
    conn.commit()
    nor do you call
    Java Code:
    conn.rollback()
    after exceptions. So, of course there is no data, you are discarding it by not commiting it. Nor are you guaranteeing that nothing that "half-way succeeds" is removed in case of an error.

  4. #4
    esgol is offline Member
    Join Date
    Dec 2010
    Posts
    29
    Rep Power
    0

    Default

    when shall i call

    conn.commit()
    or
    conn.rollback()

    after

    st.executeUpdate(sqlUpdateDb);

    perhaps?

    Thank you

  5. #5
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

  6. #6
    esgol is offline Member
    Join Date
    Dec 2010
    Posts
    29
    Rep Power
    0

    Default

    THAAAANKS!!! WITH CONN.COMMIT(); TI WOOOORKSSSSS!!!!!! OMGGGG!!!!!!

    F***** EXAMPLEDEPOSIT.COM examples!!! How could not have it?!!! ( or i missed it )

Similar Threads

  1. help with database using java
    By computerbum in forum New To Java
    Replies: 5
    Last Post: 04-26-2010, 01:45 PM
  2. conect from mobile to server
    By giapvantinh in forum Networking
    Replies: 0
    Last Post: 10-01-2009, 11:47 AM
  3. Compare two lists of number - what numbers arent there
    By Bishop609 in forum New To Java
    Replies: 5
    Last Post: 02-18-2009, 01:22 AM
  4. PDA, Database, and Java
    By percivalwcy in forum CLDC and MIDP
    Replies: 0
    Last Post: 08-08-2007, 03:12 AM
  5. Help, conect a JSP, SQL SERVER 2000
    By Marcus in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-06-2007, 03:04 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
  •