Results 1 to 6 of 6
- 01-19-2011, 04:37 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 29
- Rep Power
- 0
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 complexityLast edited by esgol; 01-19-2011 at 05:09 PM.
- 01-19-2011, 05:08 PM #2
Member
- Join Date
- Dec 2010
- Posts
- 29
- Rep Power
- 0
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...
Netbeans Output (Same every time. WHY? )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();
}
}
}
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 databaseLast edited by esgol; 01-19-2011 at 05:10 PM.
- 01-19-2011, 06:24 PM #3
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
You call
and then never callJava Code:conn.setAutoCommit(false);
nor do you callJava Code:conn.commit()
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.Java Code:conn.rollback()
- 01-19-2011, 09:37 PM #4
Member
- Join Date
- Dec 2010
- Posts
- 29
- Rep Power
- 0
when shall i call
conn.commit()
or
conn.rollback()
after
st.executeUpdate(sqlUpdateDb);
perhaps?
Thank you
- 01-19-2011, 09:55 PM #5
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
See the examples here: Using Transactions (The Java™ Tutorials > JDBC(TM) Database Access > JDBC Basics)
- 01-20-2011, 08:22 AM #6
Member
- Join Date
- Dec 2010
- Posts
- 29
- Rep Power
- 0
Similar Threads
-
help with database using java
By computerbum in forum New To JavaReplies: 5Last Post: 04-26-2010, 01:45 PM -
conect from mobile to server
By giapvantinh in forum NetworkingReplies: 0Last Post: 10-01-2009, 11:47 AM -
Compare two lists of number - what numbers arent there
By Bishop609 in forum New To JavaReplies: 5Last Post: 02-18-2009, 01:22 AM -
PDA, Database, and Java
By percivalwcy in forum CLDC and MIDPReplies: 0Last Post: 08-08-2007, 03:12 AM -
Help, conect a JSP, SQL SERVER 2000
By Marcus in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 07-06-2007, 03:04 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks