我编写了 一个类来做一个登录界面   从另一个类中来获取  这个界面的 参数  在处理数据库的时候始终找不到这里的参数  怎么回事呀   大家快来看看吧  这是登录界面 
package ATM;
import  java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.*;public class Login extends JFrame implements ActionListener{ static  JButton login,register,exit;
    static JLabel  UserName,UserPass,Email,label,Number;    这里是获取参数的文本框
    static  JTextField  NameJtf,EmailJtf,NumberJtf;
    static  JPasswordField  PassJtf;
public static void main(String[] args) {
Login log=new Login();
}

public Login()
{
//背景图层
label=new JLabel(new ImageIcon("image/123.jpg"));
getContentPane().add(label);

//第一行
UserName=new JLabel("帐 户 名  :");
UserName.setSize(100, 50);
UserName.setLocation(100, 50);
UserName.setForeground(Color.white);
//UserName.setFont(new Font("宋体", Font.PLAIN, 18));
label.add(UserName);

NameJtf=new JTextField();
NameJtf.setSize(150, 30);
NameJtf.setLocation(200, 55);
label.add(NameJtf);

//第二行
UserPass=new JLabel("用户密码  :");
UserPass.setSize(100,50);
UserPass.setForeground(Color.white);
UserPass.setLocation(100, 150);
label.add(UserPass);

PassJtf=new JPasswordField();
PassJtf.setEchoChar('*');
PassJtf.setSize(150,30);
PassJtf.setLocation(200, 155);
label.add(PassJtf);

//第三行
Number=new JLabel("用户帐号 :");
Number.setForeground(Color.white);
Number.setSize(100, 50);
Number.setLocation(100, 250);
label.add(Number);

NumberJtf=new JTextField();
NumberJtf.setSize(150,30);
NumberJtf.setLocation(200, 255);
label.add(NumberJtf);


//第四行
Email=new JLabel("邮          箱 :");
Email.setForeground(Color.white);
Email.setSize(100, 50);
Email.setLocation(100, 350);
label.add(Email);

EmailJtf=new JTextField("注册必填");
EmailJtf.setSize(150,30);
EmailJtf.setLocation(200, 355);
label.add(EmailJtf);

//第五行
login=new JButton("登录");
login.addActionListener(this);
login.setSelected(true);
login.setSize(85, 30);
login.setLocation(70, 460);
label.add(login);

register=new JButton("注册账户");
register.setSize(87,30);
register.setLocation(190,460);
label.add(register);

exit=new JButton("退出");
exit.addActionListener(this);
exit.setSize(85, 30);
exit.setLocation(315,460);
label.add(exit);


this.setResizable(false);
this.setVisible(true);
this.setSize(500, 600);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
} @Override
public void actionPerformed(ActionEvent e) {

if(e.getSource()==exit)
{
this.dispose();
}
if(e.getSource()==login)
{
    new Teller();
this.dispose();
}
}
}另一个类如下  这在数据库中获取上面的参数的 
package ATM;
import java.sql.*;public class LoginSql { static Connection  connection=null;
public static void main(String[] args) {

try {
//获取数据库驱动
System.out.println("获取数据库驱动");
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
System.out.println("获取数据库连接中");
//获取连接中
connection=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Bank", "sa", "sa");
System.out.println("数据库连接获取成功");

String table="BankList";
String sql="insert into BankList (UserName,UserNum,UserEmail,balance) values (?,?,?,?)";

//调用插入函数
addSql(connection,sql);

} catch (Exception e) {
// TODO: handle exception
}
}

public static  void addSql(Connection connection,String sql)
{
PreparedStatement prepare=null;
ResultSet result=null;


     经过验证    程序在这个位置就执行不了了   并且没有异常抛出
String UserName=Login.NameJtf.getText();System.out.println("是否通过 ?");
String  UserNum=Login.NameJtf.getText();
String UserEmail=Login.EmailJtf.getText();
double balanc=Teller.balance;
try {
prepare=connection.prepareStatement(sql);
//获取数据的第二列  并插入到数据库
prepare.setString(1, UserName);
//获取数据的第三列  并插入到数据库
prepare.setString(2, UserNum);
//获取数据的第四列  并插入到数据库
prepare.setString(3, UserEmail);
//获取数据的第五列  并插入到数据库
prepare.setDouble(4, balanc);



//执行所个的sql 语句
prepare.executeUpdate();

//获取集合
result=prepare.executeQuery();

while(result.next())
{
System.out.println("第一列    "+result.getInt(1)+"  第二列  "+result.getString(2)+
"  第三列   "+result.getString(3)+"  第四列   "+result.getString(4)+" 第五列  "+result.getDouble(5));
}

} catch (Exception e) {
e.printStackTrace();
}
}
}

解决方案 »

  1.   

    DeBug试下。看程序走到哪里出了问题
      

  2.   

    数据库连接成功了的蛤    我经过测试  是在控制台打印   System.out.println("是否通过 ?");这里前面那一句就没有成功   也就是说没有获取到参数 这就是获取参数的语句 String UserName=Login.NameJtf.getText();System.out.println("是否通过 ?");
    String UserNum=Login.NameJtf.getText();
    String UserEmail=Login.EmailJtf.getText();
    double balanc=Teller.balance;
      

  3.   

    想问问你,你的登陆事件干嘛了?你有把你设置的参数传到Login页面去吗??
      

  4.   

    随便修改了下。大概思路是这样,自己再慢慢修改。//保存登录信息类
    public class LoginInfo {
    String nameJtf = "";
    String userPass = ""; public String getNameJtf() {
    return nameJtf;
    } public LoginInfo(String nameJtf, String userPass) {
    super();
    this.nameJtf = nameJtf;
    this.userPass = userPass;
    } public void setNameJtf(String nameJtf) {
    this.nameJtf = nameJtf;
    } public String getUserPass() {
    return userPass;
    } public void setUserPass(String userPass) {
    this.userPass = userPass;
    }
    }
    //登陆界面类
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.*;public class Login extends JFrame implements ActionListener { static JButton login, register, exit;
    static JLabel UserName, UserPass, Email, label, Number; static JTextField NameJtf, EmailJtf, NumberJtf;
    static JPasswordField PassJtf; public static void main(String[] args) {
    Login log = new Login();
    } public Login() {
    // 背景图层
    label = new JLabel(new ImageIcon("image/123.jpg"));
    getContentPane().add(label); // 第一行
    UserName = new JLabel("帐 户 名 :");
    UserName.setSize(100, 50);
    UserName.setLocation(100, 50);
    UserName.setForeground(Color.white);
    // UserName.setFont(new Font("宋体", Font.PLAIN, 18));
    label.add(UserName); NameJtf = new JTextField();
    NameJtf.setSize(150, 30);
    NameJtf.setLocation(200, 55);
    label.add(NameJtf); // 第二行
    UserPass = new JLabel("用户密码 :");
    UserPass.setSize(100, 50);
    UserPass.setForeground(Color.white);
    UserPass.setLocation(100, 150);
    label.add(UserPass); PassJtf = new JPasswordField();
    PassJtf.setEchoChar('*');
    PassJtf.setSize(150, 30);
    PassJtf.setLocation(200, 155);
    label.add(PassJtf); // 第三行
    Number = new JLabel("用户帐号 :");
    Number.setForeground(Color.white);
    Number.setSize(100, 50);
    Number.setLocation(100, 250);
    label.add(Number); NumberJtf = new JTextField();
    NumberJtf.setSize(150, 30);
    NumberJtf.setLocation(200, 255);
    label.add(NumberJtf); // 第四行
    Email = new JLabel("邮 箱 :");
    Email.setForeground(Color.white);
    Email.setSize(100, 50);
    Email.setLocation(100, 350);
    label.add(Email); EmailJtf = new JTextField("注册必填");
    EmailJtf.setSize(150, 30);
    EmailJtf.setLocation(200, 355);
    label.add(EmailJtf); // 第五行
    login = new JButton("登录");
    login.addActionListener(this);
    login.setSelected(true);
    login.setSize(85, 30);
    login.setLocation(70, 460);
    label.add(login); register = new JButton("注册账户");
    register.setSize(87, 30);
    register.setLocation(190, 460);
    label.add(register); exit = new JButton("退出");
    exit.addActionListener(this);
    exit.setSize(85, 30);
    exit.setLocation(315, 460);
    label.add(exit); this.setResizable(false);

    this.setSize(500, 600);
    this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    this.setVisible(true);
    } public void actionPerformed(ActionEvent e) { if (e.getSource() == exit) {
    this.dispose();
    }
    if (e.getSource() == login) {
    this.dispose();
    String name = Login.NameJtf.getText();
    String pass = Login.PassJtf.getText();
    LoginInfo loginInfo=new LoginInfo(name, pass);
    System.out.println(loginInfo.nameJtf);
    LoginSql loginSql=new LoginSql(loginInfo);

    }
    }
    }
    //业务处理类
    import java.sql.*;public class LoginSql {  private Connection connection = null;
     private LoginInfo loginInfo=null; public LoginSql(LoginInfo loginInfo){ try {
    // 获取数据库驱动
    System.out.println("获取数据库驱动");
    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
    System.out.println("获取数据库连接中");
    // 获取连接中
    connection = DriverManager
    .getConnection(
    "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Bank",
    "sa", "sa");
    System.out.println("数据库连接获取成功"); String table = "BankList";
    String sql = "insert into BankList (UserName,UserNum,UserEmail,balance) values (?,?,?,?)"; // 调用插入函数
    addSql(connection,sql,loginInfo); } catch (Exception e) {
    // TODO: handle exception
    }
    }
      
    public void addSql(Connection connection, String sql,LoginInfo loginInfo) {
    PreparedStatement prepare = null;
    ResultSet result = null; String UserName = loginInfo.getNameJtf();
    System.out.println("是否通过 ?");
    String UserNum = Login.NameJtf.getText();
    String UserEmail = Login.EmailJtf.getText();
    try {
    prepare = connection.prepareStatement(sql);
    // 获取数据的第二列 并插入到数据库
    prepare.setString(1, UserName);
    // 获取数据的第三列 并插入到数据库
    prepare.setString(2, UserNum);
    // 获取数据的第四列 并插入到数据库
    prepare.setString(3, UserEmail); // 执行所个的sql 语句
    prepare.executeUpdate(); // 获取集合
    result = prepare.executeQuery(); while (result.next()) {
    System.out.println("第一列 " + result.getInt(1) + " 第二列 "
    + result.getString(2) + " 第三列 " + result.getString(3)
    + " 第四列 " + result.getString(4) + " 第五列 "
    + result.getDouble(5));
    } } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
      

  5.   

    楼主,你用Login.NameJtf.getText();的时候
    因为NameJtf是静态变量,因此Login类并没有被实例化,
    所以你的Login.NameJtf应该是空
      

  6.   

    貌似没什么问题蛤  现在还没试验    我之前那个获取法错误原因是在   获取的数据应该是在界面初始化的数据么   ???那个时候的数据全是null  ???
      

  7.   

    String UserNum=Login.NameJtf.getText();NameJtf不对NumberJtf
      

  8.   

    看不到new Teller();这个类在哪里
      

  9.   

    请问  6楼的    在LoginSql 中 定义的   private LoginInfo loginInfo=null; 没有对他进行实例化呀  怎么得的到 获取的数据呢    可以解释一下么  ??? 方便的话留个QQ吧