1

I am a beginner to Java, trying to insert values to the database. Please find the code attached

These two tables I have -

create table user_data(uname varchar(50) ,info varchar(1000),title varchar(100));

create table users ( uname varchar(20), email varchar(20), pswd varchar(20), country varchar(20), state varchar(20), phone_no varchar(20));

public void actionPerformed(ActionEvent e) {

    if (e.getSource() == encode)
     {

        String s1 = ta.getText();
        //System.out.println(s1);
        String s2 = tf.getText();
        int i=0;
        String[] datas = new String[20];
        for (String line : ta.getText().split("\\n"))
        {
            datas[i]=line ;
            i=i+1;
        }
        StringBuilder strBuilder = new StringBuilder();
        for (int j = 0; j < i; j++) {
           strBuilder.append(datas[j]+'\n');
        }
        String newString = strBuilder.toString();
        //System.out.println(newString);

        String s3 = tf1.getText();

            try
           {
    Connection con=ConnectionProvider.getCon();
    AES a=new AES(newString,s2,0);
    //RSA rs = new RSA(newString);

    PreparedStatement ps=con.prepareStatement("INSERT INTO user_data (uname, info, title) VALUES (?,?,?,?)");
    ps.setString(1, st);
    ps.setString(2, newString);
    ps.setString(3, s3);
    ps.setString(4, s2);
    ps.executeUpdate(); 


    JOptionPane.showMessageDialog(null,"Database Updated");
    this.dispose();
    new reservations(st);
           }
            catch (Exception ex) 
            {
               System.out.println(ex);
           }
     }
    else if(e.getSource()== reset)
    {
        ta.setText("");
        tf.setText("");
        tf1.setText("");
    }
    else{
        this.dispose();
        new reservations(st);
    }
}

I am expecting the values to be inserted. Please help me in finding the result.

Thanks in advance.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

1 Answers1

3

UserData table has only three parameters but you try to add four values.

INSERT INTO user_data (uname, info, title) VALUES (?,?,?,?)
Greg
  • 188
  • 13