展开全部
package Login;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import java.awt.Color;
public class Login extends JFrame {
public Login() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
getContentPane().setLayout(null);
getContentPane().setBackground(Color.green);
jLabel1.setFont(new java.awt.Font("宋体", Font.PLAIN, 20));
jLabel1.setText("用户名:");
jLabel1.setBounds(new Rectangle(21, 24, 80, 35));
jButton1.setBounds(new Rectangle(59, 164, 88, 42));
jButton1.setText("OK");
jButton1.addActionListener(new Login_jButton1_actionAdapter(this));
jButton2.setBounds(new Rectangle(201, 164, 88, 42));
jButton2.setText("Cancel");
jButton2.addActionListener(new Login_jButton2_actionAdapter(this));
this.getContentPane().add(jLabel1);
Password.setBounds(new Rectangle(109, 76, 202, 36));
Name.setBounds(new Rectangle(109, 26, 203, 34));
this.getContentPane().add(jLabel2);
this.getContentPane().add(Password);
this.getContentPane().add(Name);
this.getContentPane().add(jButton1);
this.getContentPane().add(jButton2);
jLabel2.setFont(new java.awt.Font("宋体", Font.PLAIN, 20));
jLabel2.setText("密 码:");
jLabel2.setBounds(new Rectangle(21, 81, 80, 35));
}
public static void main(String[] args) {
Login frame = new Login();
frame.setBounds(500, 500, 350, 280);
frame.setTitle("用户登录");
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible(true);
}
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JTextField Name = new JTextField();
JPasswordField Password = new JPasswordField();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
public void jButton2_actionPerformed(ActionEvent e) {
System.exit(0);
}
public void jButton1_actionPerformed(ActionEvent e) {
String tname = Name.getText().trim();
if (tname.equals("")) {
JOptionPane.showMessageDialog(this, "请输入用户名");
Name.requestFocus();
return;
}
String tpassword = Password.getText().trim();
if (tpassword.equals("")) {
JOptionPane.showMessageDialog(this, "请输入密码");
Password.requestFocus();
return;
}
Connection conn = null;
Statement st = null;
ResultSet rs = null;
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=Students";
String sql = "select * from register where Name='" + tname + "'";
//加载注册驱动e69da5e887aa62616964757a686964616f31333262356665
try {
Class.forName(driver);
//获得连接
conn = DriverManager.getConnection(url, "sa", "root");
//创建一个Statement对象
st = conn.createStatement();
//确认信息
rs = st.executeQuery(sql);
if (rs.next()) {
String Name2 = rs.getString(1).trim();
String Password2 = rs.getString(2).trim();
if (tname.equals(Name2) && tpassword.equals(Password2)) {
JOptionPane.showMessageDialog(this, "登陆成功!");
} else {
if (tname.equals(Name2)) {
if (!tpassword.equals(Password2)) {
JOptionPane.showMessageDialog(this, "密码错误!");
Password.requestFocus();
}
} else {
JOptionPane.showMessageDialog(this, "用户名错误!");
Name.requestFocus();
}
}
} else {
JOptionPane.showMessageDialog(this, "没有该用户!");
Name.requestFocus();
}
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (SQLException ex) {
ex.printStackTrace();
}
//关闭
finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException ex1) {
ex1.printStackTrace();
}
}
if (st != null) {
try {
st.close();
} catch (SQLException ex2) {
ex2.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException ex3) {
ex3.printStackTrace();
}
}
}
}
}
class Login_jButton1_actionAdapter implements ActionListener {
private Login adaptee;
Login_jButton1_actionAdapter(Login adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
class Login_jButton2_actionAdapter implements ActionListener {
private Login adaptee;
Login_jButton2_actionAdapter(Login adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}
已赞过
已踩过<
你对这个回答的评价是?
评论
收起