当我使用下面的示例代码向数据库输入数据时,控制台上会显示行System.out.println("Insertion complete")
。当我进入数据库时,我发现存储的数据是空白的。
我认为问题可能是我的sqlquery,但我没有看到任何问题。
以下是我的代码示例:
public class SignUpFrame extends JFrame {
public SignUpFrame() {
super("SIGN UP");
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
setVisible(true);
setResizable(false);
setLocation(500, 250);
getContentPane().setBackground(Color.WHITE);
ImageIcon FrameIcon = new ImageIcon("images/signup.png");
JLabel frameicon = new JLabel(FrameIcon);
frameicon.setBounds(150, 0, 100, 100);
add(frameicon);
JLabel firstNamelbl,LastNameLbl,EmailAddressLbl,PasswordLbl,ConfirmPasswordLbl;
JTextField firstNametxt,LastNametxt,EmailAddresstxt;
JPasswordField passwordtxt,ConfirmPasswordtxt;
JButton OKbtn;
JButton CANCELbtn;
firstNamelbl = new JLabel("FIRST NAME:");
firstNametxt = new JTextField();
firstNametxt.setBounds(150, 110, 200, 20);
firstNamelbl.setBounds(50, 110, 100, 20);
LastNameLbl = new JLabel("USERNAME:");
LastNametxt = new JTextField();
LastNametxt.setBounds(150, 140, 200, 20);
LastNameLbl.setBounds(50,140, 100, 20);
EmailAddressLbl = new JLabel("EMAIL ADDRESS:");
EmailAddresstxt = new JTextField();
EmailAddresstxt.setBounds(150, 170, 200, 20);
EmailAddressLbl.setBounds(50, 170, 150, 20);
PasswordLbl = new JLabel("PASSWORD:");
passwordtxt = new JPasswordField();
passwordtxt.setBounds(150, 200, 200, 20);
PasswordLbl.setBounds(50, 200, 150, 20);
ConfirmPasswordLbl = new JLabel("CONFIRM PASSWORD:");
ConfirmPasswordtxt = new JPasswordField();
ConfirmPasswordtxt.setBounds(200, 230, 150, 20);
ConfirmPasswordLbl.setBounds(50, 230, 150, 20);
add(firstNamelbl);
add(firstNametxt);
add(LastNameLbl);
add(LastNametxt);
add(EmailAddressLbl);
add(EmailAddresstxt);
add(PasswordLbl);
add(passwordtxt);
add(ConfirmPasswordLbl);
add(ConfirmPasswordtxt);
OKbtn = new JButton("OK");
OKbtn.setBounds(100, 270, 50, 20);
add(OKbtn);
char [] pass = passwordtxt.getPassword();
String passWord = new String(pass);
String FIRSTNAME,USERNAME,EMAIL,PASSWORD;
FIRSTNAME = firstNametxt.getText().toString();
USERNAME =LastNametxt.getText().toString();
EMAIL = EmailAddresstxt.getText().toString();
PASSWORD = passWord;
OKbtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String url = "jdbc:mysql://localhost:3306/System";
String user = "root";
String password = "";
Connection con;
try {
con = DriverManager.getConnection(url, user, password);
Statement statement = con.createStatement();
String sqlquery = "insert into signUp_tb "+
"(FirstName,UserName,Email,Password) values ('"+FIRSTNAME+"','"+USERNAME+"','"+EMAIL+"','"+PASSWORD+"')";
statement.executeUpdate(sqlquery);
System.out.println("Insertion complete");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
CANCELbtn = new JButton("CANCEL");
CANCELbtn.setBounds(170, 270, 100, 20);
add(CANCELbtn);
CANCELbtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
dispose();
}
});
}
}
答案 0 :(得分:4)
您需要在FIRSTNAME
中初始化USERNAME
,ActionListener
等确定按钮。当没有输入数据时,您已初始化了Strings
,这就是为什么所有内容都是空白的。当用户按下按钮时,您希望获得JTextFields
值。不是之前。
在SQLite上测试。
OKbtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
char [] pass = passwordTxt.getPassword();
String PASSWORD = new String(pass);
String FIRSTNAME,USERNAME,EMAIL;
FIRSTNAME = firstNameTxt.getText();
USERNAME = lastNameTxt.getText();
EMAIL = emailAddressTxt.getText();
String url = "jdbc:mysql://localhost:3306/System";
String user = "root";
String password = "";
Connection con;
try {
con = DriverManager.getConnection(url, user, password);
Statement statement = con.createStatement();
String sqlquery = "insert into signUp_tb "+
"(FirstName,UserName,Email,Password) values ('"+FIRSTNAME+"','"+USERNAME+"','"+EMAIL+"','"+PASSWORD+"')";
statement.executeUpdate(sqlquery);
System.out.println("Insertion complete");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
setVisible(true)
应该是你做的最后一件事。应该是这样的:
SwingUtilities.invokeLater(() -> {
this.setVisible(true);
});
所以你将从EDT推出它。
修改:关于setLayout(null)
- 尝试A Visual Guide to Layout Managers。
虽然可以不使用布局管理器,但应该使用 布局管理员,如果可能的话。布局管理器使其更容易 调整依赖于外观和感觉的组件外观 不同的字体大小,容器的大小变化,以及不同的大小 语言环境。布局管理器也可以被其他人轻松重用 容器,以及其他程序。