2017-06-10 5 views
-2

私のJavaアプリケーションプロジェクトでは、SQLとEclipseで次のエラーが発生しています。sql in javaエラー

java.sql.SQLException: Can not issue NULL query. 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964) 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897) 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886) 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860) 
at com.mysql.jdbc.StatementImpl.checkNullOrEmptyQuery(StatementImpl.java:485) 
at com.mysql.jdbc.StatementImpl.executeUpdateInternal(StatementImpl.java:1487) 
at com.mysql.jdbc.StatementImpl.executeLargeUpdate(StatementImpl.java:2607) 
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1480) 
at main.MainScrn.lambda$4(MainScrn.java:282) 
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:54) 
at javafx.event.Event.fireEvent(Event.java:198) 
at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470) 
at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398) 
at javafx.scene.Scene$MouseHandler.process(Scene.java:3766) 
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) 

データベースに挿入するための私のコードは、以下に、データベースに挿入される値は、文字列とデータベースの整数フィールドの整数のx値に変換するテキストフィールドからのものです。

next.setOnMouseClicked(e -> { 
     try { 
      // 1. Get a connection 
      Connection myConn = DriverManager.getConnection(
        "jdbc:mysql://localhost/PizzaMS?verifyServerCertificate=false&useSSL=true", "root", "password"); 
      // 2. Create a statement 
      Statement myStmt = myConn.createStatement(); 
      // 3. Execute SQL query 
      ResultSet myRs = myStmt 
        .executeQuery("select * from CustomerInfo where Phone = '" + phone.getText() + "'"); 
      // 4. process result set 
      if (myRs.next()) { 

      } else { 
       Statement stmt = myConn.prepareStatement(
         "INSERT INTO `CustomerInfo`(`Phone`, `Name`, `Address Field 1`, `Address Field 2`, `City`, `State`, `Zip`, `Driver Instructions`) VALUES ('"+phone+"','"+name+"','"+af1+"','"+af2+"','"+city+"','"+state+"','"+zip+"','"+di+"')"); 
       stmt.executeUpdate(null); 
       myConn.commit(); 
      } 
     } catch (Exception exc) { 
      exc.printStackTrace(); 
     } 
    }); 
+1

'stmtはを。 executeUpdate(null); '来る。エラーメッセージと、コード –

+0

をデバッグする方法を学ぶが、もし私がnullを取り除くと、stmt.executeUpdate();を使用することはできません。 – z123

+1

そして、文字列連結を使用するのではなく、安全な方法でパラメータを渡すことができるように、準備されたステートメントの点を完全に見逃しました。 PreparedStatement.executeUpdate()を呼び出すには、変数の型がStatementでなくPreparedStatementである必要があります。 –

答えて

0

あなたのコードは非常に代わりに使用できる構文エラーまたはSQLインジェクションを引き起こす可能性のPreparedStatementを使用してください:

String query = "select * from CustomerInfo where Phone = ?"; 
try (PreparedStatement pstm = connection.prepareStatement(query)) { 
    pstm.setString(1, phone.getText()); 

    pstm.executeQuery();//<<<---for select you have to use executeQuery 
} 

あなたが使用できるインサートと同じこと:

String query = "INSERT INTO `CustomerInfo`(`Phone`, `Name`, `Address Field 1`, " 
     + "`Address Field 2`, `City`, `State`, `Zip`, " 
     + "`Driver Instructions`) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"); 

try (PreparedStatement pstm2 = connection.prepareStatement(query)) { 
    pstm2.setString(1, phone); 
    pstm2.setString(2, name); 
    ... 
    pstm2.executeUpdate();//<<----you have to use executeUpdate() not executeUpdate(null) 
}