我有三个java文件。DBConn -这个类连接到数据库并执行查询。
UserLogin -这个jFrame将获取用户名和密码,并与"users"表进行比较如果匹配,它将在LibSysMain java文件
中显示"Login Successfull"LibSysMain -这是显示菜单的主文件。这是应用程序运行时首先显示的jFrame。
问题是我无法在LibSysMain中设置"登录成功"消息。
我的数据库是MS Access。表名称为"users"。字段为"用户名"one_answers"密码"。它们都是字符串类型
如果你能帮助我,我将不胜感激。
我是Java新手。我使用的是NetBeans 6.9.1。以下是列表:
DBConn:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package LibSystem;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
/**
*
* @author Administrator
*/
public class DBConn {
Connection con;
Statement stmt;
ResultSet rs;
public void DoConnect(String querydb)
{
String SQL;
SQL=querydb;
try
{
// CONNECT TO THE DATABASE
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\Database\libsys\libsys.mdb";
con = DriverManager.getConnection(url);
System.out.println("nConnected to the database");
//QUERY THE DATABASE
stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
//String SQL ="Select * from Members";
rs=stmt.executeQuery(SQL);
//MOVE CURSOR AT FIRST RECORD AND FETCH DATA
rs.next();
}
catch(ClassNotFoundException e)
{
System.out.println("nClass not found. Check Path");
e.printStackTrace();
}
catch (SQLException err)
{
JOptionPane.showMessageDialog(null, err.getMessage());
}
}
}
UserLogin:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* UserLogin.java
*
* Created on Apr 23, 2011, 8:46:59 PM
*/
package LibSystem;
import java.sql.*;
import javax.swing.JOptionPane;
import java.lang.*;
/**
*
* @author Administrator
*/
public class UserLogin extends javax.swing.JFrame {
private String user;
private String password;
private String status;
/** Creates new form UserLogin */
public UserLogin() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
txtUserName = new javax.swing.JTextField();
txtPassword = new javax.swing.JTextField();
btnClose = new javax.swing.JButton();
btnLogin = new javax.swing.JButton();
lbUserName = new javax.swing.JLabel();
lbPassword = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
btnClose.setText("Close");
btnClose.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnCloseActionPerformed(evt);
}
});
btnLogin.setText("Login");
btnLogin.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnLoginActionPerformed(evt);
}
});
lbUserName.setText("User Name");
lbPassword.setText("Password");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(34, 34, 34)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(lbPassword)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 134, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(lbUserName)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 128, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(btnClose)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 64, Short.MAX_VALUE)
.addComponent(btnLogin))
.addComponent(txtUserName, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtPassword, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE))))
.addGap(35, 35, 35))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(26, 26, 26)
.addComponent(lbUserName)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(lbPassword)
.addGap(5, 5, 5)
.addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnClose)
.addComponent(btnLogin))
.addContainerGap(39, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//CREATE AN INSTANCE OF DBCONN CLASS TO CONNECT TO THE DATABASE
//WE TAKE THE USERNAME FROM THE LOGIN WINDOW AND SEARCH THE USER
//BASED ON THAT USER. IF FOUND, COMPARE IT WITH THE LOGIN WINDOW.
//IF NOT FOUND DISPLAY ERROR MESSAGE.
DBConn dbc = new DBConn();
String qry = "Select * from users where username=" + "'" + txtUserName.getText() + "'";
dbc.DoConnect(qry);
try
{
setUsername(dbc.rs.getString("username"));
setPassword(dbc.rs.getString("password"));
//TEST PRINTOUT OF USERNAME AND PASSWORD FOR DEBUGGING
System.out.println(getUsername());
System.out.println(getPassword());
if (user.equals(txtUserName.getText()) && password.equals(txtPassword.getText()))
{
JOptionPane.showMessageDialog(UserLogin.this, "Login Successfull");
this.setStatus("pass");
dispose();
}
else
{
JOptionPane.showMessageDialog(UserLogin.this, "Login unsuccessful");
setStatus("fail");
}
}
catch (SQLException err)
{
JOptionPane.showMessageDialog(UserLogin.this, err.getMessage());
}
}
private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.dispose();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new UserLogin().setVisible(true);
}
});
}
public void setUsername(String u)
{
user = u;
}
public void setPassword(String p)
{
password = p;
}
public String getUsername()
{
return user;
}
public String getPassword()
{
return password;
}
public void setStatus(String stat)
{
status = stat;
}
public String getStatus()
{
return status;
}
// Variables declaration - do not modify
private javax.swing.JButton btnClose;
private javax.swing.JButton btnLogin;
private javax.swing.JLabel lbPassword;
private javax.swing.JLabel lbUserName;
private javax.swing.JTextField txtPassword;
private javax.swing.JTextField txtUserName;
// End of variables declaration
}
LibSysMain:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* LibsysMain.java
*
* Created on Apr 23, 2011, 6:48:18 PM
*/
package LibSystem;
import javax.swing.JOptionPane;
/**
*
* @author Administrator
*/
public class LibsysMain extends javax.swing.JFrame {
String q;
/** Creates new form LibsysMain */
public LibsysMain()
{
initComponents();
//DBConn dbc = new DBConn();
//q="Select * from users";
//dbc.DoConnect(q);
}
// LibsysMain frame1 = new LibsysMain();
// UserLogin frame2 = new UserLogin(frame1);
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel2 = new javax.swing.JPanel();
jPanel1 = new javax.swing.JPanel();
jPanel3 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jSeparator1 = new javax.swing.JSeparator();
jMenuBar1 = new javax.swing.JMenuBar();
miLogin = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenuItem2 = new javax.swing.JMenuItem();
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowActivated(java.awt.event.WindowEvent evt) {
formWindowActivated(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 539, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 328, Short.MAX_VALUE)
);
jLabel1.setText("Status");
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addContainerGap(518, Short.MAX_VALUE))
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
);
jSeparator1.setAlignmentX(0.0F);
jSeparator1.setMinimumSize(new java.awt.Dimension(10, 10));
miLogin.setText("Connect");
miLogin.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
miLoginActionPerformed(evt);
}
});
jMenuItem1.setText("Login");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});
miLogin.add(jMenuItem1);
jMenuItem2.setText("Exit");
jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem2ActionPerformed(evt);
}
});
miLogin.add(jMenuItem2);
jMenuBar1.add(miLogin);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 559, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(4, 4, 4)
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
);
pack();
}// </editor-fold>
private void miLoginActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
UserLogin ul = new UserLogin();
ul.setVisible(true);
//SET THE STATUS IF LOGIN IS SUCCESSFULL
if (ul.getStatus() == "pass")
{
jLabel1.setText("Logged In");
}
else
{
jLabel1.setText("Cannot Log in");
}
}
private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.exit(0);
}
private void formWindowActivated(java.awt.event.WindowEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new LibsysMain().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JMenu miLogin;
// End of variables declaration
}
我有几个建议:
- 首先,不要像这里if (ul.getStatus() == "pass")那样比较String与==,因为这会检查两个变量是否引用相同的String对象(您不关心),而是使用equals或equalsIgnoreCase方法来检查两个String是否包含相同的String数据(您确实关心)。例如,如果(ul.getStatus () .equalsIgnoreCase("通过"))
- 确保在GUI线程的线程背景中执行所有数据库查询和其他操作,EDT可以通过使用SwingWorker对象来实现。本文将告诉您我所说的内容,并提供详细信息:Swing中的并发性。
- 考虑使用单个JFrame和模态对话框作为主应用程序的对话窗口。这可以通过使用JOptionPanes或模态jdialog来实现,特别是对于你的UserLogin窗口。
- 出于这个原因,我喜欢把我的GUI转向创建jpanel,而不是像JFrames这样的顶层窗口。通过这种方式,我可以将GUI/JPanel放置到任何类型的窗口中,这取决于情况是JFrame, JApplet, JDialog, JOptionPane,甚至嵌套到另一个JPanel中。这给了你惊人的灵活性。