gpt4 book ai didi

MySQL 查询工作正常,但在 Netbeans 的 Java 代码中实现时出错

转载 作者:行者123 更新时间:2023-11-29 00:13:59 25 4
gpt4 key购买 nike

我有这个 sql,它根据其中一个表的最后生成的主键插入多个表字段;

INSERT INTO rentals(Customer_idCustomer,RentedDate) VALUES (1,curdate()); 
SET @last_id_in_rentals = LAST_INSERT_ID();
BEGIN;
INSERT INTO rental_movie (Movie_idMovie,Rentals_idRentals) VALUES (1,@last_id_in_rentals);
INSERT INTO transaction(idRentals,DaysRented,Cost,TotalCost) VALUES(@last_id_in_rentals,2,2,4);
COMMIT;

在 MySQL 中工作正常,但是当我在 Netbeans 中实现相同的查询时,它给了我这个错误:

"com.mysql.jdbc.excceptions.jdbc4.MysqlSyntaxErrorException:You have
an error in your SQL syntax;check the manual that corresponds to your MySQL server version for the right syntax to use near'SET @last_id_in_rentals=LAST_INSERT_ID();
BEGIN;
INSERT INTO rental_movie(Mo'at line2"

这是我的全部连接:

ResultSet rs = null;
PreparedStatement pst = null;
Statement stmt= null;
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/project";
String userid = "xxx";
String password = "";
/**
* Creates new form NewRental
*/
public NewRental() {
initComponents();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:

try{
conn = DriverManager.getConnection(url, userid, password);
String sql="SELECT idCustomer FROM customer\n" +
" WHERE idCustomer =?;";
pst=conn.prepareStatement(sql);

pst.setString(1,jTextField2.getText());
String idcustomer;
String idmovie;
idcustomer=jTextField2.getText();
idmovie=rental_movie.getText();

rs=pst.executeQuery();
if(rs.next()){

try{
asterisco1.setText("");
sql="SELECT idMovie FROM movie\n" +
" WHERE idMovie =?;";

pst=conn.prepareStatement(sql);

pst.setString(1,rental_movie.getText());

rs=pst.executeQuery();
if(rs.next()){
asterisco2.setText("");
mandatory.setText("");
sql="INSERT INTO rentals(Customer_idCustomer,RentedDate) VALUES (1,curdate()); \n" +
"SET @last_id_in_rentals = LAST_INSERT_ID();\n" +
"\n" +
"INSERT INTO rental_movie (Movie_idMovie,Rentals_idRentals) VALUES (1,@last_id_in_rentals);\n" +
"INSERT INTO transaction(idRentals,DaysRented,Cost,TotalCost) VALUES(@last_id_in_rentals,2,2,4);\n" +
"COMMIT;";
//Se crea un id de Rentas automatico.
stmt=conn.prepareStatement(sql);
stmt.executeUpdate(sql);
// sql="UPDATE rental_movie \n" +
//"INNER JOIN rentals ON rental_movie.Rentals_idRentals = rentals.idRentals\n" +
//"SET Movie_idMovie ="+idmovie+";\n";
// stmt=conn.prepareStatement(sql);
// stmt.executeUpdate(sql);
//

// sql="INSERT INTO rental_movie(Movie_idMovie,Rentals_idRentals)\n" +
// "VALUES ("+idmovie+","+idcustomer+");";
// stmt=conn.prepareStatement(sql);
// stmt.executeUpdate(sql);
// sql="INSERT INTO transaction(idRentals)\n" +
//"SELECT idRentals FROM rentals\n" +
//"WHERE rentals.Customer_idCustomer="+idcustomer+";";
// stmt=conn.prepareStatement(sql);
// stmt.executeUpdate(sql);
// sql="update transaction\n" +
//" set idRentals = (\n" +
//" select rentals.idRentals \n" +
//" from rentals\n" +
//" where rentals.idRentals = transaction.idRentals\n" +
//" ),\n" +
//"DaysRented = ,\n" +
//"Cost=,\n" +
//"TotalCost=\n" +
//" where exists (\n" +
//" select * \n" +
//" from rentals \n" +
//" where rentals.idRentals = transaction.idRentals\n" +
//" );";


}

else{
asterisco2.setForeground (Color.red);
asterisco2.setText("*");
mandatory.setForeground (Color.red);
mandatory.setText("Mandatory Field.");
JOptionPane.showMessageDialog(null, "Record could not be found. ", "Movie don't exist.", JOptionPane.INFORMATION_MESSAGE);
}}
catch(Exception e){
JOptionPane.showMessageDialog(null,e);
}


//
}
else{
asterisco1.setForeground (Color.red);
asterisco1.setText("*");
mandatory.setForeground (Color.red);
mandatory.setText("Mandatory Field.");
JOptionPane.showMessageDialog(null, "Record could not be found. ", "Client don't exist.", JOptionPane.INFORMATION_MESSAGE);

}
}
catch(Exception e){
JOptionPane.showMessageDialog(null,e);
}


}

最佳答案

尝试添加

?allowMultiQueries=true

到你的连接字符串的末尾,看起来像:

jdbc:mysql://localhost:3306/project?allowMultiQueries=true

关于MySQL 查询工作正常,但在 Netbeans 的 Java 代码中实现时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23611585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com