从java插入数据到mysql的问题

时间:2016-12-02 13:15:15

标签: java mysql database javafx

我创建了类来处理数据库实现。我构建了javafx GUI。应该将数据存储到数据库中的按钮之一。知道我使用相同的数据库登录,所以问题不在于数据库连接。

数据库实施:

        Connection conn =null;
        String dbuser = "root";
        String dbpassw = "0557724289";
        String databasename = "java_cinemaTickets";
        String url = "jdbc:mysql://127.0.0.1:3306/java_cinemaTickets";
        String driver = "com.mysql.jdbc.Driver";
        PreparedStatement ps;

public void addMovie_insetIntoDB(String title,String description,String cinema,String time,int threator,String movie_uniqeID,String image) throws ClassNotFoundException, SQLException{

     Class.forName(driver);
        conn = DriverManager.getConnection(url,dbuser,dbpassw);
        Statement st = conn.createStatement();

        ps = conn.prepareStatement("insert into movie(movie_name,description,time,threator,mviUniqeID,movie_poster,catgory,age)"+"values(?,?,?,?,?,?,?,?,?)");

       ps.setString(1, title);
       ps.setString(2, description);
       ps.setString(3, cinema);
       ps.setString(4, time);
       ps.setInt(5, threator);
       ps.setString(6, movie_uniqeID);
       ps.setString(7, image);



       ps.executeQuery();

       conn.close();

}

然后在另一个班级:

String movieName_String = movieNameToAdd_textField.getText();
    String movieDescription_String = MovieDescriptionToAdd_textArea.getText();
    String moviePoster_string= "rr";//moviePoster_textField.getText();
    String cinemaLocation_string = cinemaToAdd_comboBox.getSelectionModel().getSelectedItem().toString();
    String movieTime_string = timeToAdd_comboBox.getSelectionModel().getSelectedItem().toString();
    int  movieThretor_integer = Integer.parseInt(threatorToAdd_comboBox.getSelectionModel().getSelectedItem().toString());
    String movieUniQID="jj";



    adminAddMovie_button.setOnAction(e->{


        try {
            db.addMovie_insetIntoDB(movieName_String,movieDescription_String,cinemaLocation_string,movieTime_string,movieThretor_integer,movieUniQID,moviePoster_string);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Java_CinemaTicket.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Java_CinemaTicket.class.getName()).log(Level.SEVERE, null, ex);
        }

    primaryStage.setTitle("Admin GUI");
    primaryStage.setScene(admin_Scene);
    primaryStage.show();

    });

按下按钮将数据保存到数据库后,我收到错误

Dec 02, 2016 9:00:55 PM java_cinematicket.Java_CinemaTicket lambda$start$2
SEVERE: null
java.sql.SQLException: Can not issue data manipulation statements with      executeQuery().
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
at com.mysql.jdbc.StatementImpl.checkForDml(StatementImpl.java:504)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2223)
at java_cinematicket.JavaMongoDBConnection.addMovie_insetIntoDB(JavaMongoDBConnection.java:95)
at java_cinematicket.Java_CinemaTicket.lambda$start$2(Java_CinemaTicket.java:479)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8413)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:380)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937) 

1 个答案:

答案 0 :(得分:1)

AFAIK您需要使用executeUpdate()进行DML操作(insert/update/delete),而不是executeQuery()

仔细查看并阅读异常消息,这里的内容完全相同 java.sql.SQLException:无法使用 executeQuery()发出数据操作语句。 < / p>