Re: .Java File Error
- From: mrfurley15@xxxxxxxxx
- Date: 22 Dec 2005 21:22:50 -0800
Thanks everyone!
One more thing. I got my three java files up and running (finally!).
Can anyone help me with the jsp file? I think there is something wrong
with that.
TransferConfirmation.jsp:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>TRANSFER RESULTS</title>
</head>
<body>
<jsp:include page="menu.html" flush="true" />
<jsp:useBean id="customer" scope="session" class="Bank.Customer" />
<%
String[] fromAccountInfo =
customer.getAccountInfo(request.getParameter("FromAccount"));
String[] toAccountInfo =
customer.getAccountInfo(request.getParameter("ToAccount"));
String[] customerID=request.getParameter("customerID");
String[] OutString="";
if(test.Transfer(Amount,FromAccount,ToAccount,customerID)) {
%>
<h3>Transfer Complete!</h3><br>
<br><h4> Use the menu to continue</h4><br>
<br>
<table cellpadding="2" cellspacing="2" border="1" style="text-align:
left; width: 50%;">
<!--Following is the table of the results of the transfer-->
<tr><td>Date</td><td>Transferred From</td><td>Transferred
To</td></tr><tr>
<td><jsp:getProperty name="customer" property="currentDate"
/></td><td><%
out.println(fromAccountInfo[0]); %></td><td><%
out.println(toAccountInfo[0]); %></td></tr><tr></tr><tr>
<td>New Available</td><td><% out.println(fromAccountInfo[2]);
%></td><td><% out.println(toAccountInfo[2]); %></td></tr>
<td>New Balance</td><td><% out.println(fromAccountInfo[1]);
%></td><td><% out.println(toAccountInfo[1]); %></td></tr>
} else
out.println("<tr>Transfer Failed"+test.getOutString()+"</tr>");%>
</table>
</body>
</html>
---------------------------------------
Customer.java
package Bank;
import java.util.*;
import java.text.*;
import java.io.Serializable;
import Bank.DBI;
public class Customer implements Serializable {
// session bean named "customer" Tomcat keeps alive between client
requests
private String customerID;
private String firstname;
private String lastname;
private String error;
protected DBI dbi;
/****************/
/* Constructors */
/****************/
/* Beans must have a zero argument constructor */
public void Customer() {
}
/***********/
/* Methods */
/***********/
public boolean init() {
boolean proceed = false;
try{
this.dbi = new Bank.DBI();
proceed = this.dbi.connect();
}catch(Exception e) {
e.printStackTrace();
}
return(proceed);
}
/** Public Accesor Methods, required get/sets
* for jsp:getProperty, jsp:setProperty */
public String getCustomerID() {
return(this.customerID);
}
public void setCustomerID(String value) {
this.customerID = value;
}
public String getFirstname() {
return(this.firstname);
}
public void setFirstname(String value) {
this.firstname = value;
}
public String getLastname() {
return(this.lastname);
}
public void setLastname(String value) {
this.lastname = value;
}
public String getError() {
return(this.error);
}
public void setError(String value) {
this.error = value;
}
public String[] getAccountInfo(String accountID) {
try {
return this.dbi.getAccountInfo(accountID);
} catch (Exception e) {
String[] tmp = {"Error obtaining account information.
System error: " + e};
return tmp;
}
}
public String getCurrentDate()
{
Date now = new Date();
DateFormat df = DateFormat.getDateInstance();
String s = df.format(now);
return s;
}
public String getAccountSummary() {
String outString = "ACCOUNT SUMMARY ERROR";
try{
String[][] AccountArray =
this.dbi.getAllAccountInfo(this.customerID);
outString = "";
for(int j=0;j<10;j++) {
outString += "<br> <tr>";
if(AccountArray[j][0]==null) { break;}
for(int i=0;i<4;i++) {
if(i==2||i==1) {
outString += "<td ALIGN=\"right\">";
outString += "$"+AccountArray[j][i];
} else if(i==0) {
outString += "<td>";
outString += "<form
action=\"AccountDetails.jsp\" method=\"post\">";
outString += "<input
type=\"Hidden\" name=\"AccountNumber\" value=\"";
outString +=
"\""+AccountArray[j][i]+">";
outString += "<button
type=\"Submit\"\">"+AccountArray[j][i]+"</button>";
}
else { outString += "<td>";
outString += AccountArray[j][i];
}
outString += "</td>";
if(i==3) outString += "</tr";
}}
}catch(Exception e) {
e.printStackTrace();
}
return(outString);
}
public String getTransfer() {
String outString = "TRANSFER ERROR";
try{
String[][] AccountArray =
this.dbi.getAllAccountInfo(this.customerID);
outString = "<tr><td>Transfer Amount $</td><td><input
name=\"Amount\"></tr><br>";
for(int j=0;j<2;j++) {
if(j==0) {
outString += "<tr><td>From</td>";
outString += "<td><select size=\"1\"
name=\"FromAccount\">";
}
else {
outString += "<tr><td>To</td>";
outString += "<td><select size=\"1\"
name=\"ToAccount\">";
}
for(int i=0;i>-1;i++) {
if(AccountArray[i][0]==null) break;
outString += "<option
VALUE=\""+AccountArray[i][0]+"\" >";
outString += ""+AccountArray[i][0]+" -
"+AccountArray[i][2];
outString += "</option>";
}
outString += "</select></td></tr>";
}
}catch(Exception e) {
e.printStackTrace();
}
return(outString);
}
}
------------------------------------------------------------------
DBI.java
package Bank;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Vector;
public class DBI {
// All Bank database methods are here
Connection conn = null;
String[] accountFields = {"AccountID", "Balance", "Available", "Type"};
public DBI() throws NamingException, SQLException {
}
public boolean connect() throws NamingException, SQLException {
// Connect to the Bank database
// Using the Tomcat Server.xml Context to locate the data
Context initCtx = new InitialContext();
Context envCtx = (Context)initCtx.lookup("java:comp/env");
if(envCtx == null ) throw new NamingException("Boom - No
Context");
// Use jdbc to connect with sql statements
DataSource ds = (DataSource)envCtx.lookup("jdbc/TestDB");
//if ds is not null, the datasource was established (Tomcat
connection pool open with database)
if(ds!=null) {
// Get a connection from the Tomcat database connection
pool with Bank server
conn = ds.getConnection();
// If we get the connection ...
if(conn != null) { return true; }
}
return false;
}
public ResultSet execQuery(String str) throws SQLException {
// Build a jdbc statement from an SQL query string
Statement stmt = conn.createStatement();
// Execute the query and fetch the ResultSet from the database
ResultSet rst = stmt.executeQuery(str);
return rst;
}
public int execUpdate(String str) throws SQLException {
Statement stmt = conn.createStatement();
return stmt.executeUpdate(str);
}
public boolean checkCustPin(String custID, String pin)
throws SQLException {
// rst gets the results of the Select query looking for custID
and pin
ResultSet rst = this.execQuery("SELECT CustomerId,
Firstname, Lastname, PIN FROM Customer WHERE CustomerId = "+custID+"
and PIN = "+pin);
// Go to first row of the ResultSet; if there are no rows,
customerID and pin do not match
boolean result=(rst.first());
return result;
}
public String[] getCustNames(String custID) throws SQLException {
// Retrive the Firstname and Lastname fields of the custID
String[] result = new String[2];
ResultSet rst = this.execQuery("SELECT CustomerId,
Firstname, Lastname, PIN FROM Customer WHERE CustomerId = "+custID);
if(rst.first()) {
result[0] = rst.getString("Firstname");
result[1] = rst.getString("Lastname");
}
return result;
}
public Vector getAccounts(String customerID) throws SQLException {
Vector accounts = new Vector(10);
// Get a vector of all accounts that belong to this customer
ResultSet rst = this.execQuery("SELECT AccountId FROM
UserAccounts WHERE CustomerId = "+customerID);
if(rst.first()) {
do {
accounts.add(rst.getString(this.accountFields[0]));
} while(rst.next());
}
return(accounts);
}
public Vector getInternalAccounts(String customerID) throws
SQLException {
Vector accounts = new Vector(10);
// Get a vector of all accounts that belong to this
customer
ResultSet rst = this.execQuery("SELECT AccountId FROM
UserAccounts WHERE CustomerId = "+customerID);
if(rst.first()) {
do {
accounts.add(rst.getString(this.accountFields[0]));
} while(rst.next());
}
return(accounts);
}
public String[] getAccountInfo(String accountID) throws SQLException {
String[] info = new String[4];
info[0] = accountID;
// Get the Account info array for an account
ResultSet rst = this.execQuery("SELECT Balance, Available,
Type FROM Accounts WHERE AccountId = "+accountID);
for(int i=1;i<4;i++) {
if(rst.first()) {
info[i]=rst.getString(this.accountFields[i]);
}
}
return(info);
}
public String[][] getAllAccountInfo(String customerID) throws
SQLException {
// Get all account info for all accounts for this customerID
String[][] accountInfo = new String[10][4];
// Get a vector of string AccountIds of this customer
Vector accounts = this.getAccounts(customerID);
for(int i=0;i<accounts.size();i++) {
// Get the array of AccountInfo fields for each AccountId
accountInfo[i] =
this.getAccountInfo((String)accounts.get(i));
}
if(accountInfo[0][0] == null) throw new SQLException("No
Accounts Found");
return(accountInfo);
}
public boolean doTransfer(String fromAccount, String toAccount,
String amount)
throws SQLException
{
String balance;
Integer ttid = 0;
this.execQuery("BEGIN");
this.execQuery("SET AUTOCOMMIT = 0");
int fromRows = this.execUpdate("UPDATE Accounts SET
Balance=Balance-" + amount + ", Available=Available-" + amount + "
WHERE AccountId = " + fromAccount);
int toRows = this.execUpdate("UPDATE Accounts SET
Balance=Balance+" + amount + ", Available=Available+" + amount + "
WHERE AccountId = " + toAccount);
balance = (getAccountInfo(fromAccount))[1];
int fromHistoryRows = this.execUpdate("INSERT INTO
transactions
(DateTime,AccountId,Amount,TransferTID,Balance,Status,TransType) VALUES
(NOW(), '" + fromAccount + "', '" + amount + "', '11111', '" + balance
+ "', 'Posted','WI')");
ResultSet rst = this.execQuery("SELECT LAST_INSERT_ID()");
if(rst.first()) {
ttid = rst.getInt(1);
}
balance = (getAccountInfo(toAccount))[1];
int toHistoryRows = this.execUpdate("INSERT INTO transactions
(DateTime,AccountId,Amount,TransferTID,Balance,Status,TransType) VALUES
(NOW(), '" + toAccount + "', '" + amount + "', " + ttid + ", '" +
balance + "', 'Posted','DI')");
this.execQuery("COMMIT");
this.execQuery("SET AUTOCOMMIT = 1");
return (fromRows > 0 && toRows > 0 && fromHistoryRows > 0 &&
toHistoryRows > 0);
}
}
--------------------------------------------------------------
TransferControl.java
package Bank;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.sql.*;
import java.sql.*;
import Bank.DBI;
import Bank.Customer;
import Bank.Control;
import java.util.Vector;
public class TransferControl extends Bank.Control
{
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
String fromAccount = request.getParameter("FromAccount");
String toAccount = request.getParameter("ToAccount");
String amount = request.getParameter("Amount");
HttpSession session = request.getSession();
Bank.Customer custbean =
(Bank.Customer)session.getAttribute("customer");
boolean proceed = this.transfer(custbean, fromAccount,
toAccount, amount);
if(proceed) {
/* Transfer Succeded */
gotoPage("/TransferConfirmation.jsp", request, response);
}
else {
/* Transfer Failed */
gotoPage("/Transfer.jsp", request, response);
}
}
private boolean transfer(Bank.Customer bean, String fromAccount, String
toAccount, String
amount) throws ServletException
{
String[] fromAccountInfo;
Vector accounts;
Vector internalAccounts;
boolean transferStatus;
if(amount.equals(""))
{
bean.setError("Error: No amount was entered.");
return false;
}
if(!testNum(amount))
{
bean.setError("Error: Invalid format. Enter number
only.");
return false;
}
if(fromAccount.equals(toAccount))
{
bean.setError("Error: Two different accounts must be used
to make a transfer.");
return false;
}
try
{
accounts = bean.dbi.getAccounts(bean.getCustomerID());
internalAccounts =
bean.dbi.getInternalAccounts(bean.getCustomerID());
}
catch(Exception e)
{
bean.setError("Error: Unable to access customer accounts.
System error: " + e);
return false;
}
if(!accounts.contains(fromAccount))
{
bean.setError("Error: Account" + fromAccount + "is not a
valid account. Please try again!");
return false;
}
if(!accounts.contains(toAccount))
{
bean.setError("Error: Account " + toAccount + " is not a
valid account. Please try again!");
return false;
}
if(!internalAccounts.contains(fromAccount) ||
!internalAccounts.contains(toAccount))
{
bean.setError("Error: Transfers for external accounts have
not yet been implemented.");
return false;
}
try
{
fromAccountInfo = bean.dbi.getAccountInfo(fromAccount);
}
catch(Exception e)
{
bean.setError("Error: Unable to access account information.
System error: "+ e);
return false;
}
if(Float.parseFloat(fromAccountInfo[2]) <
Float.parseFloat(amount))
{
bean.setError("Error: The \"From\" account does not have
enough funds to make this transfer happen. Only $" + fromAccountInfo[2]
+ "is available.");
return false;
}
try
{
transferStatus = bean.dbi.doTransfer(fromAccount,
toAccount, amount);
}
catch(Exception e)
{
bean.setError("Error: Unable to perform transfer. System
error: "+ e);
return false;
}
return transferStatus;
}
}
----
My transfer works, but it doesn't show on screen that it went through
so something has to be wrong with the TransferConfirmation.jsp
Any help will be appreciated.
.
- References:
- .Java File Error
- From: mrfurley15
- Re: .Java File Error
- From: IchBin
- Re: .Java File Error
- From: IchBin
- Re: .Java File Error
- From: IchBin
- .Java File Error
- Prev by Date: Re: Logical Operation With byte[] -- What is it doing?
- Next by Date: Re: Logical Operation With byte[] -- What is it doing?
- Previous by thread: Re: .Java File Error
- Next by thread: Displaying graphics
- Index(es):