我有一个名为Customer的mySQL数据库,我正在尝试从图形用户界面中的文本字段插入数据。我运行了下面的代码,在控制台中它显示“连接成功”,没有错误。我检查信息是否已经插入到我的mySQL数据库的Customer表中,但没有插入任何内容。有人知道为什么吗?
button.setOnAction(e -> {
Connection dbConnection = null;
PreparedStatement preparedStatement = null;
try {
Customer cust = new Customer();
dbConnection = Connect();
String sql="Insert into CIS3270.Customer(firstName,lastName, email,userNAME,Address,Zip,State,SecurityQ, Password, ConfirmPassword,SSN)VALUES (?,?,?,?,?,?,?,?,?,?,?)";
preparedStatement = dbConnection.prepareStatement(sql);
preparedStatement.setString(1,cust.getFirstName());
preparedStatement.setString(2,cust.getLastName());
preparedStatement.setString(3,cust.getEmail());
preparedStatement.setString(4,cust.getUserNAME());
preparedStatement.setString(5,cust.getAddress());
preparedStatement.setString(6,cust.getZip());
preparedStatement.setString(7,cust.getState());
preparedStatement.setString(8,cust.getSecurityQuestion());
preparedStatement.setString(9,cust.getPassWORD());
preparedStatement.setString(10,cust.getConfirmPassword());
preparedStatement.setString(11,cust.getSSN());
preparedStatement.executeBatch();
preparedStatement.executeUpdate();
dbConnection.close();
preparedStatement.close();
LoginScreen loginPage = new LoginScreen();
loginPage.start(primaryStage);
}
catch(Exception e1) {
e1.printStackTrace();
}
});
public static Connection Connect() {
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = (Connection) DriverManager.getConnection("jdbc:mysql://(ip adress):3306/CIS3270", "root", "password");
} catch (Exception e) {
System.out.println("Can not connect");
}
if (con != null) {
System.out.println("Connected Successfully");
}
return con;
}
发布于 2018-04-23 07:32:40
您不需要批量插入,因为您插入的是单行。
您的代码不执行任何操作的原因是因为您正在执行一个空的批处理。您也需要调用preparedStatement.addBatch()
。或者删除executeBatch()
调用,因为您似乎也调用了executeUpdate()
。
https://stackoverflow.com/questions/49975543
复制