老师说是成员变量和局部变量没有分开才出现问题,但是我真的不知道问题在哪。有没....
import java.awt.Container;import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
class Login extends JFrame{
JLabel jj = new JLabel("登录界面");
JLabel j1 = new JLabel("账号");
JLabel j2 = new JLabel("密码");
JTextField tt = new JTextField();
JPasswordField pass = new JPasswordField();
JButton b1 = new JButton("登录");
JButton b2 = new JButton("取消");
JLabel zc = new JLabel("用户不存在,请注册");
JButton b3 = new JButton("注册");
Container c = this.getContentPane();
static String str;
public Login() {
super("用户登录界面");
this.setBounds(200,200,500,500);
this.setLayout(null);
jj.setBounds(220,0,100,70);
j1.setBounds(100,75,70,60);
j2.setBounds(100,115,70,60);
tt.setBounds(140,80,120,35);
pass.setBounds(140,120,120,35);
b1.setBounds(140,160,60,40);
b2.setBounds(210,160,60,40);
zc.setBounds(50,350,150,70);
b3.setBounds(210,360,60,40);
c.add(jj);
c.add(j1);
c.add(j2);
c.add(tt);
c.add(pass);
c.add(b1);
c.add(b2);
c.add(zc);
c.add(b3);
bh a = new bh();
b1.addActionListener(a);
b2.addActionListener(a);
b3.addActionListener(a);
this.setVisible(true);
}
class bh implements ActionListener{
public void actionPerformed(ActionEvent e) {
JButton bt = (JButton)e.getSource();
String username = tt.getText().trim();
String Pass = String.valueOf(pass.getPassword());
JFrame dd2 = new JFrame("注册界面");
Container x = dd2.getContentPane();
JLabel yh = new JLabel("用户注册");
JLabel zy = new JLabel("用户名");
JLabel zm = new JLabel("密码");
JLabel zm2 = new JLabel("确认密码");
JButton log=new JButton("注册");
JButton can=new JButton("取消");
JTextField y1 = new JTextField();
JPasswordField y2 = new JPasswordField();
JPasswordField y3 = new JPasswordField();
if(bt==b1) {
/*if(tt.getText().equals("888")&&Pass.equalsIgnoreCase("222333"))
JOptionPane.showMessageDialog(null, "登陆成功");
else
JOptionPane.showMessageDialog(null, "登陆失败");
}*/
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost:1433;databasename=student";
String user = "sa";
String password = "123456";
Connection con = DriverManager.getConnection(url, user, password);
Statement stmt=con.createStatement();
String sel = "select * from student where name='"+username+"' and pass='"+Pass+"'";
ResultSet rset=stmt.executeQuery(sel);
if(rset.next())
JOptionPane.showMessageDialog(null, "登陆成功");
else
JOptionPane.showMessageDialog(null, "登陆失败");
con.close();
}catch(Exception e1){
e1.printStackTrace();}}
if(bt==b2) {
tt.setText("");
pass.setText("");
}
if(bt==b3) {
dd2.setBounds(200,200,500,500);
x.setLayout(null);
yh.setBounds(130,0,60,50);
zy.setBounds(100,60,60,50);
zm.setBounds(100,110,60,50);
zm2.setBounds(100,160,120,50);
log.setBounds(120,230,60,40);
can.setBounds(200,230,60,40);
y1.setBounds(160,65,120,35);
y2.setBounds(160,115,120,35);
y3.setBounds(160,165,120,35);
x.add(yh);
x.add(zy);
x.add(zm);
x.add(zm2);
x.add(y1);
x.add(y2);
x.add(y3);
x.add(log);
x.add(can);
dd2.setVisible(true);
log.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e2){
String setuser = zy.getText();
String setpass = String.valueOf(y2.getPassword());
String setpass2 = String.valueOf(y3.getPassword());
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost:1433;databasename=student";
String user = "sa";
String password = "123456";
Connection con = DriverManager.getConnection(url, user, password);
Statement stmt=con.createStatement();
String sel = "select * from student where name="+"'"+setuser+"'";
ResultSet rset=stmt.executeQuery(sel);
//rset.next();
//System.out.println(rset.getString(1));
//rset.beforeFirst();
//if(rset.next())
// str = rset.getString();
//System.out.println(str);
if(rset.next())
JOptionPane.showMessageDialog(null, "用户已存在");
else
JOptionPane.showMessageDialog(null, "。。。");
con.close();
}catch(Exception e3){
e3.printStackTrace();}
}
});
}
}
}
}
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
new Login();
}
} 这个是用户登录还有注册界面。然后注册界面链接 sql server。但是不管在注册界面不管输入的用户名是不是存在。都只是显示 “。。。” 把bh类中的
JFrame dd2 = new JFrame("注册界面");
Container x = dd2.getContentPane();
JLabel yh = new JLabel("用户注册");
JLabel zy = new JLabel("用户名");
JLabel zm = new JLabel("密码");
JLabel zm2 = new JLabel("确认密码");
JButton log=new JButton("注册");
JButton can=new JButton("取消");
JTextField y1 = new JTextField();
JPasswordField y2 = new JPasswordField();
JPasswordField y3 = new JPasswordField();
搬到bh中作为成员变量import java.awt.Container;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
class Login extends JFrame{
JLabel jj = new JLabel("登录界面");
JLabel j1 = new JLabel("账号");
JLabel j2 = new JLabel("密码");
JTextField tt = new JTextField();
JPasswordField pass = new JPasswordField();
JButton b1 = new JButton("登录");
JButton b2 = new JButton("取消");
JLabel zc = new JLabel("用户不存在,请注册");
JButton b3 = new JButton("注册");
Container c = this.getContentPane();
static String str;
public Login() {
super("用户登录界面");
this.setBounds(200,200,500,500);
this.setLayout(null);
jj.setBounds(220,0,100,70);
j1.setBounds(100,75,70,60);
j2.setBounds(100,115,70,60);
tt.setBounds(140,80,120,35);
pass.setBounds(140,120,120,35);
b1.setBounds(140,160,60,40);
b2.setBounds(210,160,60,40);
zc.setBounds(50,350,150,70);
b3.setBounds(210,360,60,40);
c.add(jj);
c.add(j1);
c.add(j2);
c.add(tt);
c.add(pass);
c.add(b1);
c.add(b2);
c.add(zc);
c.add(b3);
bh a = new bh();
b1.addActionListener(a);
b2.addActionListener(a);
b3.addActionListener(a);
this.setVisible(true);
}
class bh implements ActionListener{
JFrame dd2 = new JFrame("注册界面");
Container x = dd2.getContentPane();
JLabel yh = new JLabel("用户注册");
JLabel zy = new JLabel("用户名");
JLabel zm = new JLabel("密码");
JLabel zm2 = new JLabel("确认密码");
JButton log=new JButton("注册");
JButton can=new JButton("取消");
JTextField y1 = new JTextField();
JPasswordField y2 = new JPasswordField();
JPasswordField y3 = new JPasswordField();
public void actionPerformed(ActionEvent e) {
JButton bt = (JButton)e.getSource();
String username = tt.getText().trim();
String Pass = String.valueOf(pass.getPassword());
if(bt==b1) {
/*if(tt.getText().equals("888")&&Pass.equalsIgnoreCase("222333"))
JOptionPane.showMessageDialog(null, "登陆成功");
else
JOptionPane.showMessageDialog(null, "登陆失败");
}*/
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost:1433;databasename=student";
String user = "sa";
String password = "123456";
Connection con = DriverManager.getConnection(url, user, password);
Statement stmt=con.createStatement();
String sel = "select * from student where name='"+username+"' and pass='"+Pass+"'";
ResultSet rset=stmt.executeQuery(sel);
if(rset.next())
JOptionPane.showMessageDialog(null, "登陆成功");
else
JOptionPane.showMessageDialog(null, "登陆失败");
con.close();
}catch(Exception e1){
e1.printStackTrace();}}
if(bt==b2) {
tt.setText("");
pass.setText("");
}
if(bt==b3) {
dd2.setBounds(200,200,500,500);
x.setLayout(null);
yh.setBounds(130,0,60,50);
zy.setBounds(100,60,60,50);
zm.setBounds(100,110,60,50);
zm2.setBounds(100,160,120,50);
log.setBounds(120,230,60,40);
can.setBounds(200,230,60,40);
y1.setBounds(160,65,120,35);
y2.setBounds(160,115,120,35);
y3.setBounds(160,165,120,35);
x.add(yh);
x.add(zy);
x.add(zm);
x.add(zm2);
x.add(y1);
x.add(y2);
x.add(y3);
x.add(log);
x.add(can);
dd2.setVisible(true);
log.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e2){
String setuser = zy.getText();
String setpass = String.valueOf(y2.getPassword());
String setpass2 = String.valueOf(y3.getPassword());
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost:1433;databasename=student";
String user = "sa";
String password = "123456";
Connection con = DriverManager.getConnection(url, user, password);
Statement stmt=con.createStatement();
String sel = "select * from student where name="+"'"+setuser+"'";
ResultSet rset=stmt.executeQuery(sel);
//rset.next();
//System.out.println(rset.getString(1));
//rset.beforeFirst();
//if(rset.next())
// str = rset.getString();
//System.out.println(str);
if(rset.next())
JOptionPane.showMessageDialog(null, "用户已存在");
else
JOptionPane.showMessageDialog(null, "。。。");
con.close();
}catch(Exception e3){
e3.printStackTrace();}
}
});
}
}
}
}
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
new Login();
}
}
页:
[1]