Java Reference
In-Depth Information
public
synchronized
long
startTransaction(
long
customer,
long
employee)
throws
Exception {
Connection conn
#
DriverManager.getConnection
(connection);
Statement stmt
#
conn.createStatement();
ResultSet rs
#
stmt.executeQuery(
"SELECT MAX(ID) AS M FROM TRANS");
long
code;
if
(rs.next())
code
#
rs.getLong("M")
!
1;
else
code
#
1;
stmt.executeUpdate("INSERT INTO TRANS "
!
"(ID,employee,customer,is_open)"
!
" VALUES ( "
!
code
!
", "
!
employee
!
","
!
customer
!
", 1)");
return
code;
}
public void
closeTransaction(
long
code)
throws
Exception {
Connection conn
#
DriverManager
.getConnection(connection);
Statement stmt
#
conn.createStatement();
ResultSet rs
#
stmt.executeQuery(
"SELECT * FROM TRANS WHERE ID
#
"
!
code);
Transaction trans
#
null
;
if
(rs.next()) trans
#
new
Transaction(rs);
if
(trans
##
null
)
throw new
Exception(
"Transaction non existent");
if
(!trans.open)
throw new
Exception(
"Transaction is closed");
stmt.executeUpdate("UPDATE TRANS SET is_open
#
0 "
!
"WHERE ID
#
"
!
code);
}
public synchronized void
buy(
long
code,
long
quantity,
long
transaction)
throws
Exception {
Connection conn
#
DriverManager
.getConnection(connection);
Statement stmt
#
conn.createStatement();
ResultSet rs;
Transaction t;
try
{
rs
#
stmt.executeQuery("SELECT * FROM TRANS WHERE ID
#
"
!
transaction);