我编写了 一个类来做一个登录界面 从另一个类中来获取 这个界面的 参数 在处理数据库的时候始终找不到这里的参数 怎么回事呀 大家快来看看吧 这是登录界面
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();
}
}
}
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();
}
}
}
解决方案 »
- 老师教做软件,连接数据库,数据库在我这机子上,别人用我的软件,如何连接我的数据库呢?请帮下小忙!!!
- 带autoboxing 对象的比较问题!
- ResultSet 有没有选择数据行的限制??
- 在jdk1.3中有什么办法替换jdk1.4的replaceAll的方法?
- 关于list的请教
- 请大家指点一下:作为一个合格或者优秀的JAVA程序员应具备的知识结构?
- 请问两个基本的java问题
- 执行顺序?JUnit4 注解
- 有点迷茫 希望有人指导一下 如果有同样困惑的进来讨论一下
- 今天找到了字符集的资料,爽!散分!
- jtable怎么跨窗口刷新数据显示呢?有截图 求大家帮帮
- 用Java写先进先出算法,置换算法,急!!!!!
String UserNum=Login.NameJtf.getText();
String UserEmail=Login.EmailJtf.getText();
double balanc=Teller.balance;
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();
}
}
}
因为NameJtf是静态变量,因此Login类并没有被实例化,
所以你的Login.NameJtf应该是空