Here is my code for a JDBC program to do account transfer. I am having trouble with my transfer code. Can anyone help. This is urgent.
import java.sql.*;
/*****************************************************
class BankAccountTransfer {
/** Make a transfer from two accounts owned by same customer.
* @param custNum the 6-digit customer number
* @param custPasswd the customer's password
* @param acctNumFrom the 8-digit acct# to transfer from; owned by custNum
* @param acctNumTo the 8-digit acct# to transfer to; owned by custNum
* @param amt The amount to transfer.
* @return a string with a user-friendly result.
* The string starts with "ERROR" iff the transfer didn't go through.
*/
public static String makeTransfer( String custNum, String custPasswd,
String acctNumFrom, String acctNumTo, double amt ) {
String msgStart = "Transfer from " + acctNumFrom + " to " + acctNumTo + " ";
String errMsgStart = "ERROR -- " + msgStart + " cancelled:\n ";
try {
String q1 = "select password from bankcustomers where custid = '" + custNum + "'";
ResultSet passwdResults = conn.createStatement().executeQuery(q1);
boolean gotSomeResults = passwdResults.next();
if (!gotSomeResults) {
return errMsgStart + "No such customer number " + custNum;
}
// If we make it here, 'passwdResults' is pointing at the first (only) customer record.
if (! custPasswd.equals( passwdResults.getString("password") )) {
return errMsgStart + "Incorrect password";
}
// If we make it here, we have verified that the password and custNum passed in
// to this method really do match what is off in the database.
conn.setAutoCommit(false);
// TODO: Complete the transfer.
// Print an error message and return if:
// - either the 'from' or 'to' account doesn't exist w/ expected owner
// - insufficient funds
if (amt < 0) {
return errMsgStart + "insufficient funds"
+ (amt);
}
// Otherwise use 'conn.createStatement().executeUpdate(...)'
// to change the balances in the two accounts.
return msgStart + "succeeded.";
conn.commit();
conn.setAutoCommit(true);
}
catch (SQLException e) {
return errMsgStart + "DB error " + e.toString();
}
}
/** Make several transfers, to test.
* (This method opens *and* closes one db connection.)
*/
public static void main (String args []) throws SQLException {
try {
DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
conn = DriverManager.getConnection( DB_CONNECT_INFO, DB_USER, DB_USER_PASSWD );
System.out.println( makeTransfer( "111111", "highlander",
"00000001", "00000002", 10.00 ) ); // succeed
System.out.println( makeTransfer( "111119", "highlander",
"00000001", "00000002", 12.34 ) ); // fail - no such user
System.out.println( makeTransfer( "111111", "lowlander",
"00000001", "00000002", 12.34 ) ); // fail-wrong passwd
System.out.println( makeTransfer( "111111", "highlander",
"00000001", "00000002", 12345.67 ) ); // fail-insuf funds
System.out.println( makeTransfer( "111111", "highlander",
"00000001", "00000003", 12.34 ) ); // fail-don't own acct
System.out.println( makeTransfer( "111111", "highlander",
"00000003", "00000002", 12.34 ) ); // fail-don't own acct
System.out.println( makeTransfer( "222222", "ruready",
"00000003", "00000003", 250.66 ) ); // succeed
System.out.println( makeTransfer( "222222", "ruready",
"00000003", "00000004", 3500.23 ) ); // succeed
System.out.println( makeTransfer( "222222", "ruready",
"00000003", "00000003", 550.23 ) ); // succeed
System.out.println( makeTransfer( "222222", "ruready",
"00000003", "00000003", 17550.23 ) ); // fail-insuf funds
System.out.println( makeTransfer( "222222", "dreadred",
"00000003", "00000004", 550.23 ) ); // fail-wrong passwd
System.out.println( makeTransfer( "222223", "ruready",
"00000003", "00000004", 550.23 ) ); // fail - no such user
// see the bankAccountSchema.sql for her password etc.
}
catch (SQLException e){
System.err.println ("Could not load the db "+e);
}
finally {
if (conn != null) conn.close(); // N.B. This alone could raise an exception.
}
}
// Class fields:
private static Connection conn; // init'd at start of main(), closed at the end.
private static final String DB_CONNECT_INFO = "jdbc:oracle:thin:@Picard.radford.edu:1521:teaching";
private static final String DB_USER = "sajohnson"; // do NOT include the "@ITEC"
private static final String DB_USER_PASSWD = "Welcome";
}