gpt4 book ai didi

java - prepareStatement 为 getPassword 抛出错误

转载 作者:行者123 更新时间:2023-12-01 20:20:00 28 4
gpt4 key购买 nike

我是 eclipse 的新手,我正在尝试创建一个注册界面。作为一个外壳,它工作得很好,即如果没有给出输入,它会接受输入并显示警告,但它没有连接到数据库。我现在尝试使用prepareStatement 将其连接到我的数据库,但我收到一些参数的setString 错误。我尝试使用建议修复之一来更改错误,即“将 p1 的类型更改为“字符串””,但没有任何改变。如何使用 setString 作为密码?

我知道查询中并不是要求每个 txtField,我想先尝试几个,然后再尝试插入所有字段。

“ConnectDB”连接的工作方式与我将其连接在一个单独的类中一样,但它是一个只有一个插入的简单准备语句。我做错了什么?

  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', 'Eugene1@gmail.com', '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 子句中的列一样多的占位符(问号),并且它们不能被引用。另外,请记住,一列的名称不得包含任何空格。

关于java - prepareStatement 为 getPassword 抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58940681/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com