我是Java的初学者,试图将值插入数据库。请找到随附的代码
我有这两张桌子-
创建表user_data(uname varchar(50),info varchar(1000),title varchar(100));
创建表用户(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);
}
}
我希望插入值。请帮助我找到结果。
谢谢。
答案 0 :(得分:2)
UserData表只有三个参数,但是您尝试添加四个值。
INSERT INTO user_data (uname, info, title) VALUES (?,?,?,?)