简体   繁体   中英

setString undefined for type of statement when using prepareStatement

I just connected the MySQL database to my eclipse code successfully for the first time and now I am trying to insert it into a table based on user input from a swing application. The insertion works if I hardwire the insert query with my own information, but when I try to use prepare statements to receive the input I am receiving an error that says "setString(int, String) undefined for this type of statement". Is there another more appropriate function to use rather than setString? Or is there something else I'm doing wrong?

My code:

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 doesn't have a setString(...) method. PreparedStatement does: https://docs.oracle.com/javase/8/docs/api/java/sql/PreparedStatement.html#setString-int-java.lang.String-

java.sql.Connection.prepareStatement(...) returns a PreparedStatement , so your code should look like this:

PreparedStatement stmt = con.createStatement();

instead of this:

Statement stmt= con.createStatement();

First of all change the Statement to

PreparedStatement prep = con.prepareStatement(sql);

also avoid to do methods like txtname.getText inside of the setString method of a PreparedStatement, create some variable that will do that and pass them into the setString(paramindex, param) method.

That being said i think you should right it like this:

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();

I think this would work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM