0
我有一個連接java到mc訪問來檢查用戶身份驗證的問題,當我運行這個程序時它不給我任何錯誤消息(我推測連接沒有問題),但它也不檢查帳號和密碼是否有效。其他方法如退出,存款和getBalance工作正常。
任何幫助或提示將不勝感激。使用jdbc odbc連接java到ms訪問數據庫,java用戶登錄系統
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Logic
{
private enum State { ACCOUNT_NO, PASSWORD, PROCESSING };
private State state = State.ACCOUNT_NO;
private long number;
private long acNumber = 0;
private long acPIN = 0;
private char op = ' ';
private Bank bank = new Bank();
private JTextArea display1, display2;
public Logic(JTextArea area1, JTextArea area2)
{
display1 = area1; display2 = area2;
display2.setText("Welcome: Enter your account number");
}
public void process(String button)
{
String info = null;
if (button.length() == 1)
{
char c = button.charAt(0);
if (c >= '0' && c <= '9') // Digit
{
number = number * 10 + c-'0'; // Build number
display1.setText("" + number);
}
return;
}
if (button.equals("CLR"))
{
number = 0;
display1.setText("" + number);
}
if (button.equals("Ent"))
{
switch (state)
{
case ACCOUNT_NO:
bank.setAcNumber(number);
number = 0;
state = State.PASSWORD;
display1.setText("");
display2.setText("Now enter your password");
break;
case PASSWORD:
bank.setAcPasswd(number);
number = 0;
display1.setText("");
if (bank.checkValid())
{
state = State.PROCESSING;
display2.setText("Now enter transaction");
} else {
state = State.ACCOUNT_NO;
display2.setText("Invalid: Start again");
}
break;
default :
}
return;
}
if (state != State.PROCESSING)
{
state = State.ACCOUNT_NO;
display2.setText("But you are not loged in\n" );
display2.append("Welcome: Enter your account number");
return;
}
if (button.equals("W/D")) // Clear Result
{
display1.setText("");
if (bank.withdraw(number))
{
display2.setText("Withdrawn: " + number);
} else {
display2.setText("You do not have surficient funds");
}
number = 0;
return;
}
if (button.equals("Bal")) // Clear Result
{
number = 0;
display2.setText("Your balance is: " + bank.getBalance());
return;
}
if (button.equals("Dep")) // Clear Result
{
bank.deposit(number);
display1.setText("");
display2.setText("Deposited: " + number);
number = 0;
return;
}
if (button.equals("Fin")) // Clear Result
{
state = State.ACCOUNT_NO;
number = 0;
display2.setText("Welcome: Enter your account number");
return;
}
return;
}
public long getResult()
{
return number;
}
public long getNumber()
{
return number;
}
}
銀行類。
import java.sql.*;
class Bank
{
long AcNumber = 0;
long AcPasswd = 0;
long balance = 0;
long deposit = 0;
long withdraw = 0;
long amount = 0;
Connection con;
Statement st;
ResultSet rs;
public void connect()
{
try
{
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
Class.forName(driver);
String db = "jdbc:odbc:db3";
con = DriverManager.getConnection(db);
st = con.createStatement();
}
catch (Exception ex)
{
System.err.println("Can not load JDBC/ODBC driver.");
System.exit(-1);
}
}
public void setAcNumber(long accNumber)
{
AcNumber = accNumber;
}
public void setAcPasswd(long accNumber)
{
AcPasswd = accNumber;
}
public boolean checkValid()
{
try {
String sql = "select user,pass from Table1 where user='"+AcNumber+"'and pass='"+AcPasswd+"'";
rs = st.executeQuery(sql);
int count = 0;
while(rs.next())
{
count = count + 1;
}
if(count == 1)
{
System.out.println("User Found, Now enter transaction");
}
else
{
System.out.println("user not found");
}
}
catch (Exception ex)
{
}
return true;
}
public boolean withdraw(long amount)
{
balance = balance - amount;
System.out.println("Bank: Withdrawm " + amount);
return true;
}
public void deposit(long amount)
{
balance = balance + amount;
System.out.println("Bank: deposit " + amount);
return;
}
public long getBalance() {
System.out.println("Bank: Balance: "+ balance);
return balance;
}
}
糟糕的代碼。我不會這樣寫。從學習Java編碼標準開始。您的數據成員名稱應該是小寫。大寫是.NET標準,我相信。 – duffymo