代码如下:
package com.ts;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import javax.swing.*;
public class Login extends JFrame {
private JButton esc =new JButton("取消");
private JLabel user=new JLabel("用户名");
private JLabel pass=new JLabel("密 码");
private JTextField txtUser=new JTextField(10);
JPasswordField txtPass=new JPasswordField(10);
JButton ok=new JButton("确定");
private static String userStr;
Statement stem=null;
static Connection con=null;
ResultSet res=null;
Login(){
this.setTitle("登录系统");
this.show(true);
this.setSize(200, 100);
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
JPanel panel3=new JPanel();
panel1.add(user);
panel1.add(txtUser);
panel2.add(pass);
panel2.add(txtPass);
panel3.add(ok);
panel3.add(esc);
this.add(panel1,BorderLayout.NORTH);
this.add(panel2,BorderLayout.CENTER);
this.add(panel3,BorderLayout.SOUTH);
Toolkit kit=Toolkit.getDefaultToolkit();
Dimension screenSize=kit.getScreenSize();
int screenWidth=screenSize.width;
int screenHigth=screenSize.height;
this.setLocation(screenWidth/3, screenHigth/4);
this.setResizable(false);
this.pack(); //使窗体适应大小
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
esc.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(1);
}
});
//登陆事件
ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
try {
//加载数据库
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con=DriverManager.getConnection("jdbc:sqlserver://localhost:1434;databaseName=Info","sa","111111111");
userStr = txtUser.getText();
res = stem.executeQuery("select * from addInfo where id='"
+ userStr + "' and pass='" + pass + "'");
if(txtUser.getText()==txtUser ){ //这里该怎么写啊
JOptionPane.showMessageDialog(null, " 登陆成功","提示",JOptionPane.INFORMATION_MESSAGE);
}
else{
JOptionPane.showMessageDialog(null, " 登陆失败","提示",JOptionPane.INFORMATION_MESSAGE);
}
}
catch (Exception e2) {
sqlClose();
}
}
});
}
public void sqlClose() {// 关闭连接
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Login();
}
}
求高手