利用JDBC数据库技术,参考API文档,编写一个简单的登陆程序。程序运行的界面如下图所示:要求:
发布网友
发布时间:2022-05-02 03:40
我来回答
共1个回答
热心网友
时间:2022-05-02 05:09
数据库你自己建吧,这个很容易的。我数据库是房子C盘根目录的,叫stu.mdb(用2003建的)。你要是用2007建的,可以打开另存为2000-2003格式的就可以了。sql和数据库位置可以在代码里改。
代码:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class TestGra extends JFrame {
private Connection conn;
public TestGra() {
init();
pack();
setLocationRelativeTo(null);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void init() {
JLabel lblUserName = new JLabel("用户名");
final JTextField txtUserName = new JTextField();
JLabel lblPassword = new JLabel("密码");
final JPasswordField pwfPassword = new JPasswordField();
JButton btnLogin = new JButton("登陆");
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
getConnect();
if (conn == null) {
JOptionPane.showMessageDialog(null, "数据库链接失败");
return;
}
boolean isOK = checkUser(txtUserName.getText().trim(),
new String(pwfPassword.getPassword()));
if (isOK) {
JOptionPane.showMessageDialog(null, "登陆成功");
} else {
JOptionPane.showMessageDialog(null, "用户名或密码不正确");
}
closeConnect();
}
});
JButton btnCancel = new JButton("退出");
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
hGroup.addGroup(layout.createParallelGroup().addComponent(lblUserName)
.addComponent(lblPassword));
hGroup.addGroup(layout
.createParallelGroup()
.addComponent(txtUserName, GroupLayout.PREFERRED_SIZE, 180,
GroupLayout.PREFERRED_SIZE)
.addComponent(pwfPassword, GroupLayout.PREFERRED_SIZE, 180,
GroupLayout.PREFERRED_SIZE)
.addGroup(
Alignment.CENTER,
layout.createSequentialGroup().addComponent(btnLogin)
.addComponent(btnCancel)));
layout.setHorizontalGroup(hGroup);
GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
vGroup.addGap(15);
vGroup.addGroup(layout
.createParallelGroup(GroupLayout.Alignment.CENTER)
.addComponent(lblUserName)
.addComponent(txtUserName, GroupLayout.PREFERRED_SIZE, 25,
GroupLayout.PREFERRED_SIZE));
vGroup.addGroup(layout
.createParallelGroup(GroupLayout.Alignment.CENTER)
.addComponent(lblPassword)
.addComponent(pwfPassword, GroupLayout.PREFERRED_SIZE, 25,
GroupLayout.PREFERRED_SIZE));
vGroup.addGroup(layout
.createParallelGroup(GroupLayout.Alignment.CENTER)
.addComponent(btnLogin).addComponent(btnCancel));
layout.setVerticalGroup(vGroup);
}
/**
* 打开连接
*/
private void getConnect() {
if (conn != null) {
return;
}
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=c:/stu.mdb", "", "");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
private boolean checkUser(String name, String pwss) {
String userName = "";
Statement stmt = null;
try {
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(
"SELECT username FROM userinfo WHERE username = '" + name + "' AND password = '" + pwss +"'");
if (rs.next()) {
userName = rs.getString(1).trim();
}
stmt.close();
if (userName.equals("")) {
return false;
} else {
return true;
}
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
/**
* 关闭链接
*/
private void closeConnect() {
try {
conn.close();
conn = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
new TestGra().setVisible(true);
}
}