鱼C论坛

 找回密码
 立即注册
查看: 2088|回复: 2

老师说是成员变量和局部变量没有分开才出现问题,但是我真的不知道问题在哪。有没....

[复制链接]
发表于 2019-11-24 11:16:50 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
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();
        }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-11-24 11:18:32 | 显示全部楼层
这个是用户登录还有注册界面。然后注册界面链接 sql server。但是不管在注册界面不管输入的用户名是不是存在。都只是显示 “。。。”
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-28 10:44:04 | 显示全部楼层
把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中作为成员变量
  1. import java.awt.Container;
  2. import java.awt.event.*;
  3. import java.sql.*;

  4. import javax.swing.*;
  5. class Login extends JFrame{
  6.         JLabel jj = new JLabel("登录界面");
  7.         JLabel j1 = new JLabel("账号");
  8.         JLabel j2 = new JLabel("密码");
  9.         JTextField tt = new JTextField();
  10.         JPasswordField pass = new JPasswordField();
  11.         JButton b1 = new JButton("登录");
  12.         JButton b2 = new JButton("取消");
  13.         JLabel zc = new JLabel("用户不存在,请注册");
  14.         JButton b3 = new JButton("注册");
  15.         Container c = this.getContentPane();
  16.         static String str;
  17.       
  18.         public Login() {
  19.                 super("用户登录界面");
  20.                 this.setBounds(200,200,500,500);
  21.                 this.setLayout(null);
  22.                 jj.setBounds(220,0,100,70);
  23.                 j1.setBounds(100,75,70,60);
  24.                 j2.setBounds(100,115,70,60);
  25.                 tt.setBounds(140,80,120,35);
  26.                 pass.setBounds(140,120,120,35);
  27.                 b1.setBounds(140,160,60,40);
  28.                 b2.setBounds(210,160,60,40);
  29.                 zc.setBounds(50,350,150,70);
  30.                 b3.setBounds(210,360,60,40);
  31.                 c.add(jj);
  32.                 c.add(j1);
  33.                 c.add(j2);
  34.                 c.add(tt);
  35.                 c.add(pass);
  36.                 c.add(b1);
  37.                 c.add(b2);
  38.                 c.add(zc);
  39.                 c.add(b3);
  40.                 bh a = new bh();
  41.                 b1.addActionListener(a);
  42.                 b2.addActionListener(a);
  43.                 b3.addActionListener(a);
  44.                 this.setVisible(true);
  45.         }
  46.       
  47.         class bh implements ActionListener{
  48.                         JFrame dd2 = new JFrame("注册界面");
  49.                         Container x = dd2.getContentPane();
  50.                         JLabel yh = new JLabel("用户注册");
  51.                         JLabel zy = new JLabel("用户名");
  52.                         JLabel zm = new JLabel("密码");
  53.                         JLabel zm2 = new JLabel("确认密码");
  54.                         JButton log=new JButton("注册");
  55.                         JButton can=new JButton("取消");
  56.                         JTextField y1 = new JTextField();
  57.                         JPasswordField y2 = new JPasswordField();
  58.                         JPasswordField y3 = new JPasswordField();

  59.                 public void actionPerformed(ActionEvent e) {
  60.                         JButton bt = (JButton)e.getSource();
  61.                         String username = tt.getText().trim();
  62.                         String Pass = String.valueOf(pass.getPassword());
  63.                        
  64.                         
  65.                        
  66.                         if(bt==b1) {
  67.                                 /*if(tt.getText().equals("888")&&Pass.equalsIgnoreCase("222333"))
  68.                                         JOptionPane.showMessageDialog(null, "登陆成功");
  69.                                 else
  70.                                         JOptionPane.showMessageDialog(null, "登陆失败");
  71.                         }*/
  72.                          try{
  73.                                  Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  74.                                           String url = "jdbc:sqlserver://localhost:1433;databasename=student";
  75.                                           String user = "sa";
  76.                                           String password = "123456";
  77.                                           Connection con = DriverManager.getConnection(url, user, password);
  78.                                           Statement stmt=con.createStatement();
  79.                                           String sel = "select * from student where name='"+username+"' and pass='"+Pass+"'";
  80.                                           ResultSet rset=stmt.executeQuery(sel);
  81.                                           if(rset.next())
  82.                                                   JOptionPane.showMessageDialog(null, "登陆成功");
  83.                                           else
  84.                                                   JOptionPane.showMessageDialog(null, "登陆失败");
  85.                                          
  86.                                   con.close();
  87.                          }catch(Exception e1){
  88.                                     e1.printStackTrace();}}
  89.                         if(bt==b2) {
  90.                                 tt.setText("");
  91.                                 pass.setText("");
  92.                         }
  93.                         if(bt==b3) {
  94.                               
  95.                                 dd2.setBounds(200,200,500,500);
  96.                               
  97.                                 x.setLayout(null);
  98.                               
  99.                                 yh.setBounds(130,0,60,50);
  100.                               
  101.                                 zy.setBounds(100,60,60,50);
  102.                               
  103.                                 zm.setBounds(100,110,60,50);
  104.                               
  105.                                 zm2.setBounds(100,160,120,50);
  106.                               
  107.                                 log.setBounds(120,230,60,40);
  108.                               
  109.                                 can.setBounds(200,230,60,40);
  110.                               
  111.                                 y1.setBounds(160,65,120,35);
  112.                               
  113.                                 y2.setBounds(160,115,120,35);
  114.                               
  115.                                 y3.setBounds(160,165,120,35);
  116.                               
  117.                                 x.add(yh);
  118.                                 x.add(zy);
  119.                                 x.add(zm);
  120.                                 x.add(zm2);
  121.                                 x.add(y1);
  122.                                 x.add(y2);
  123.                                 x.add(y3);
  124.                                 x.add(log);
  125.                                 x.add(can);
  126.                                 dd2.setVisible(true);
  127.                               
  128.                                 log.addActionListener(new ActionListener(){
  129.                                         public void actionPerformed(ActionEvent e2){
  130.                                                 String setuser = zy.getText();
  131.                                                 String setpass = String.valueOf(y2.getPassword());
  132.                                                 String setpass2 = String.valueOf(y3.getPassword());
  133.                                                 try{
  134.                                                         Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  135.                                                           String url = "jdbc:sqlserver://localhost:1433;databasename=student";
  136.                                                           String user = "sa";
  137.                                                           String password = "123456";
  138.                                                           Connection con = DriverManager.getConnection(url, user, password);
  139.                                                           Statement stmt=con.createStatement();
  140.                                                           String sel = "select * from student where name="+"'"+setuser+"'";
  141.                                                           ResultSet rset=stmt.executeQuery(sel);
  142.                                                           //rset.next();
  143.                                                           //System.out.println(rset.getString(1));
  144.                                                           //rset.beforeFirst();
  145.                                                          
  146.                                                           //if(rset.next())
  147.                                                           //         str = rset.getString();
  148.                                                           //System.out.println(str);
  149.                                                           if(rset.next())
  150.                                                                   JOptionPane.showMessageDialog(null, "用户已存在");
  151.                                                           else
  152.                                                                   JOptionPane.showMessageDialog(null, "。。。");
  153.                                                           con.close();
  154.                                          }catch(Exception e3){
  155.                                                     e3.printStackTrace();}
  156.                                         }
  157.                                 });
  158.                 }
  159.         }
  160. }
  161. }

  162. public class Test {
  163.         public static void main(String[] args) {
  164.                 // TODO Auto-generated method stub
  165.                 new Login();
  166.         }
  167. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-19 06:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表