我是eclipse的新手,正在尝试创建注册界面。作为外壳,它运行良好,即如果未提供输入,它将接受输入并显示警告,但未连接到数据库。我现在正在尝试使用prepareStatement将其连接到数据库,但是我正在接收一些参数的setString的错误。我尝试使用一种建议修复程序(“将p1的类型更改为'String')更改错误,但没有任何更改。如何使用setString作为密码?
我知道查询中并非要求每个txtField,我想先尝试一些txtField,然后再尝试插入所有txtField。
“ ConnectDB”连接的工作方式与我在单独的类中进行的连接一样,但是它是仅一个插入的简单prepareStatement。我究竟做错了什么?
package project_files;
import javax.swing.*;
import Connection.ConnectDB;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.Arrays;
public class registration_test extends JFrame implements ActionListener
{
Connection con = null;
PreparedStatement pst = null;
JLabel l1, l2, l3, l4, l5, l6, l7, l8;
JTextField txtname, txtid, txtDOB, txtState, txtNum;
JButton btnSubmit, btn2;
JPasswordField p1, p2;
private JLabel Lname;
private JTextField textLname;
public registration_test()
{
setVisible(true);
setSize(700, 700);
getContentPane().setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Registration Form");
l1 = new JLabel("Registration Form");
l1.setForeground(Color.blue);
l1.setFont(new Font("Serif", Font.BOLD, 20));
l2 = new JLabel("First Name:");
l3 = new JLabel("Email-ID:");
l4 = new JLabel("Create Passowrd:");
l5 = new JLabel("Confirm Password:");
l6 = new JLabel("DOB");
l7 = new JLabel("State:");
l8 = new JLabel("Phone No:");
txtname = new JTextField();
txtid = new JTextField();
p1 = new JPasswordField();
p2 = new JPasswordField();
txtDOB = new JTextField();
txtState = new JTextField();
txtNum = new JTextField();
btnSubmit = new JButton("Submit");
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
PreparedStatement st = ConnectDB.prepareStatement("INSERT INTO userdatabase . users (FirstName, LastName, Email, Password, Phone Number) VALUES ('?','?','?','?'");
st.setString(1, txtname.getText());
st.setString(2, textLname.getText());
st.setString(3, txtid.getText());
st.setString(4, p1.getPassword());
st.setString(5, txtNum.getText());
pst.executeUpdate();
JOptionPane.showMessageDialog(null, "REGISTER SUCCESSFULLY");
}
catch(Exception ex){
JOptionPane.showMessageDialog(null, ex);
}
JFrame frmregistration_test = new JFrame("Submit");
if (txtname.getText().isEmpty())
{
JOptionPane.showMessageDialog(null, "All fields must be filled in", "Login Warning", JOptionPane.WARNING_MESSAGE);
}
else if (textLname.getText().isEmpty())
{
JOptionPane.showMessageDialog(null, "All fields must be filled in", "Login Warning", JOptionPane.WARNING_MESSAGE);
}
else if (txtDOB.getText().isEmpty())
{
JOptionPane.showMessageDialog(null, "All fields must be filled in", "Login Warning", JOptionPane.WARNING_MESSAGE);
}
else if (txtNum.getText().isEmpty())
{
JOptionPane.showMessageDialog(null, "All fields must be filled in", "Login Warning", JOptionPane.WARNING_MESSAGE);
}
else if (txtState.getText().isEmpty())
{
JOptionPane.showMessageDialog(null, "All fields must be filled in", "Login Warning", JOptionPane.WARNING_MESSAGE);
}
else if (txtid.getText().isEmpty())
{
JOptionPane.showMessageDialog(null, "All fields must be filled in", "Login Warning", JOptionPane.WARNING_MESSAGE);
}
else if (p1.getPassword().length == 0 || p2.getPassword().length == 0){
JOptionPane.showMessageDialog(null, "Passwords fields can not be empty.", "Woops", JOptionPane.ERROR_MESSAGE);
}
else if (!Arrays.equals(p1.getPassword(), p2.getPassword())) {
JOptionPane.showMessageDialog(null, "Passwords do not match.", "Woops", JOptionPane.ERROR_MESSAGE);
}
else {
JOptionPane.showMessageDialog(null, "Registered Successfully", "Login Warning", JOptionPane.WARNING_MESSAGE);
}
}});
btn2 = new JButton("Clear");
btnSubmit.addActionListener(this);
btn2.addActionListener(this);
l1.setBounds(100, 30, 400, 30);
l2.setBounds(80, 70, 200, 30);
l3.setBounds(80, 361, 200, 30);
l4.setBounds(80, 150, 200, 30);
l5.setBounds(80, 190, 200, 30);
l6.setBounds(80, 230, 200, 30);
l7.setBounds(80, 270, 200, 30);
l8.setBounds(80, 310, 200, 30);
txtname.setBounds(300, 70, 200, 30);
txtid.setBounds(300, 361, 200, 30);
p1.setBounds(300, 150, 200, 30);
p2.setBounds(300, 190, 200, 30);
txtDOB.setBounds(300, 230, 200, 30);
txtState.setBounds(300, 270, 200, 30);
txtNum.setBounds(300, 310, 200, 30);
btnSubmit.setBounds(50, 402, 100, 30);
btn2.setBounds(168, 402, 100, 30);
getContentPane().add(l1);
getContentPane().add(l2);
getContentPane().add(txtname);
getContentPane().add(l3);
getContentPane().add(txtid);
getContentPane().add(l4);
getContentPane().add(p1);
getContentPane().add(l5);
getContentPane().add(p2);
getContentPane().add(l6);
getContentPane().add(txtDOB);
getContentPane().add(l7);
getContentPane().add(txtState);
getContentPane().add(l8);
getContentPane().add(txtNum);
getContentPane().add(btnSubmit);
getContentPane().add(btn2);
Lname = new JLabel("Last Name");
Lname.setBounds(80, 112, 70, 14);
getContentPane().add(Lname);
textLname = new JTextField();
textLname.setBounds(300, 111, 200, 28);
getContentPane().add(textLname);
textLname.setColumns(10);
}
public static void main(String args[])
{
new registration_test();
}
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
如果您需要查看连接,它是:
package Connection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class ConnectDB {
//DB Connection variables
static Connection connection = null;
static String databaseName = "";
static String url = "jdbc:mysql://localhost:3306/" +databaseName;
static String username = "root";
static String password = "pass1234";
public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
connection = DriverManager.getConnection(url, username, password);
PreparedStatement ps = connection.prepareStatement("INSERT INTO `userdatabase` . `user` (`UserId`, `FirstName`, `LastName`, `Phone Number`, `Email`, `Password`) VALUES ('5', 'Eugene', 'Miller', '586231234', '[email protected]', 'password');");
int status = ps.executeUpdate();
if (status != 0) {
System.out.println("Datebase was Connected");
System.out.println("Record was Inserted");
}
}
public static PreparedStatement prepareStatement(String string) {
// TODO Auto-generated method stub
return null;
}
}
最佳答案
到目前为止,我发现了一些错误:
方法ConnectDb.prepareStatement
应该返回一个非空的有效PreparedStatement对象(如@Abra明智指出的那样)。在实现它时,请记住正确地初始化connection
对象。
查看在方法prepareStatement
中设置的SQL语句:您必须在VALUES子句中放置与INSERT
子句中的列一样多的占位符(问号),并且不能用引号引起来。另外,请记住,一栏的名称不得包含任何空格。