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

时间:2014-05-12 14:22:11

标签: mysql sql

我有这个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);
        }


    }                                        

1 个答案:

答案 0 :(得分:1)

尝试添加

?allowMultiQueries=true

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

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