我正在嘗試使用Java創建登錄表單。Java Access登錄表單問題db
我創建了表單,但似乎無法連接到數據庫,或者從數據庫中檢索用戶名和密碼。
這裏是Login.java代碼:
package login;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
public class Login {
Connection con;
Statement st;
ResultSet rs;
JFrame f = new JFrame("User Login");
JLabel l = new JLabel("Username");
JLabel l1 = new JLabel("Password");
JTextField t = new JTextField(10);
JTextField t1 = new JTextField(10);
JButton b = new JButton("Login");
public Login()
{
connect();
frame();
}
public void connect()
{
try
{
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
Class.forName(driver);
String db = "jdbc:odbc:credentials";
con = DriverManager.getConnection(db);
st = con.createStatement();
}
catch(Exception ex)
{
}
}
public void frame()
{
f.setSize(600,400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
JPanel p = new JPanel();
p.add(l);
p.add(t);
p.add(l1);
p.add(t1);
p.add(b);
f.add(p);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
try
{
String username = t.getText().trim();
String password = t1.getText().trim();
String sql = "select user, pass from users where user = '" +username+"'and pass = '"+password+"'" ;
rs = st.executeQuery(sql);
int count = 0;
while(rs.next())
{
count ++;
}
if(count == 1)
{
}
else if(count > 1)
{
JOptionPane.showMessageDialog(null, "Duplicate User, Access Denied!");
}
else
{
JOptionPane.showMessageDialog(null, "User Not Found!");
}
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null, "Catch Error");
}
}
});
}
public static void main(String[] args) {
new Login();
}
}
當程序運行時,它繞過用戶的檢索和通過,並輸出「catch錯誤」,這是我成立。
這可能是一些愚蠢的,就像一個錯過的字符,但任何幫助將不勝感激。
非常感謝。
你有什麼異常? – jtahlborn