我第一次将mysql数据库成功连接到我的eclipse代码,现在我试图根据来自swing应用程序的用户输入插入表中。如果我使用自己的信息对插入查询进行硬连接,则插入将起作用,但是当我尝试使用prepareStatements接收输入时,会收到一条错误消息,指出“此类型的语句未定义setString(int,String)”。除了setString之外,还有其他更合适的函数吗?还是我做错了什么?
我的代码:
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
{
/**
*
*/
JLabel l1, l2, l3, l4, l5, l6, l7, l8;
JTextField txtname, txtid, txtDOB, txtState, txtNum;
JButton btnSubmit, btn2;
JPasswordField p1;
JPasswordField 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 {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/userdatabase", "root", "pass1234");
Statement stmt= con.createStatement();
String sql = ("INSERT INTO user ( `FirstName`, `LastName`, `Phone Number`, `Email`, `Password`) VALUES (?, ?, ?, ?, ?);");
stmt.setString(1, txtname.getText());
stmt.setString(2, textLname.getText());
stmt.setString(3, txtid.getText());
stmt.setString(4, txtNum.getText());
stmt.setString(5, p1.getText());
stmt.executeUpdate(sql);
JOptionPane.showMessageDialog(null, "REGISTER SUCCESSFULLY");
con.close();
} catch(Exception e) {System.out.print (e);}
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();
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
java.sql.Statement
没有setString(...)
方法。 PreparedStatement
会:https://docs.oracle.com/javase/8/docs/api/java/sql/PreparedStatement.html#setString-int-java.lang.String-
[java.sql.Connection.prepareStatement(...)
返回一个PreparedStatement
,因此您的代码应如下所示:
PreparedStatement stmt = con.createStatement();
代替此:
Statement stmt= con.createStatement();
首先将Statement
更改为
PreparedStatement prep = con.prepareStatement(sql);
也要避免在PreparedStatement的setString方法内部执行类似txtname.getText
之类的方法,创建一些将要执行此操作的变量,并将其传递到setString(paramindex, param)
方法中。
话虽如此,我认为你应该这样纠正:
Class.forname ...
Connection con = ...
String sql = "INSERT INTO user (`FirstName`, `LastName`, `Phone Number`, `Email`, `Password`) VALUES (?, ?, ?, ?, ?)";
String fname = txtname.getText();
String lname = txtLname.getText();
/* etc.. */
PreparedStatement prep = con.prepareStatement(sql);
prep.setString(1,fname);
prep.setString(2,lname);
prep.excuteUpdate();
我认为这会起作用。