[英]Why the preparedStatement is not working in jdbc?
我在 mysql 中创建了一个名为 user 的表。 然后我创建了一个简单的 j 框架,其中包含一个文本字段和一个按钮。 按下按钮后,按钮将在数据库中添加名称。 但是在insertData.InsertData$2.actionPerformed(InsertData.java:86)处显示错误意味着准备好的语句存在一些问题。 我试过没有准备好的陈述,但问题没有解决。 最重要的是我的表有 4 列称为 (name, id, Email,Phone)
package insertData;
import java.awt.EventQueue;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JFrame;
import javax.swing.JTextField;
import com.mysql.cj.xdevapi.Statement;
import com.sun.jdi.connect.spi.Connection;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class InsertData {
private JFrame frame;
private JTextField textField;
Connection connection;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
InsertData window = new InsertData();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public InsertData() {
initialize();
createConnection();
}
/**
* Initialize the contents of the frame.
*/
public void createConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");//load driver
java.sql.Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/shykat_db","root","root"); //Establish connection
System.out.println("sucess");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
textField = new JTextField();
textField.setBounds(10, 25, 174, 44);
frame.getContentPane().add(textField);
textField.setColumns(10);
JButton btnNewButton = new JButton("Submit\r\n");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
String name=textField.getText();
String drop="insert into user values(?) ";
java.sql.PreparedStatement ps= ((java.sql.Connection) connection).prepareStatement(drop);
System.out.println(ps);
ps.setString(1, name);
ps.executeUpdate();
//ResultSet rs = ps.executeQuery();
//System.out.println(rs);
//java.sql.Statement stm=((java.sql.Connection) connection).createStatement();
//String drop="insert into user values(' "+name+" ')";
//stm.execute(drop);
//stm.close();
//int count=ps.executeUpdate();
//System.out.println(count+"-----Data updated");
ps.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
});
btnNewButton.setBounds(313, 26, 89, 44);
frame.getContentPane().add(btnNewButton);
}
}
如果只想插入多列表的某些列,则必须在表名之后声明要插入的列
INSERT INTO t(column,list,goes,here)
VALUES(samenumber,ofvalues,asthere,arecolumns)
您的用户表至少有 4 列,您不能说insert into user values(?)
,因为您的数据库不会准备猜测 go 应该在哪一列中。尝试insert into user(name) values(?)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.