2015-09-28 37 views
0

這是用戶在程序啓動後將插入數據的textField的代碼。它不更新mysql表。爲什麼它不起作用?插入數據的用戶已經進入mysql(java)

textField = new JTextField(); 
    textField.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 

      try {Class.forName("com.mysql.jdbc.Driver"); 
       Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/csprogram","root",""); 
       price=scan.next(); 
       String query = "INSERT * INTO `graphics` (price (KM)) VALUES (?);"; 
       PreparedStatement preparedStmt = conn.prepareStatement(query); 
       preparedStmt.setString (1, price); 
       preparedStmt.executeUpdate(); 
      } 
       catch (Exception d) { 
       System.out.println("Error found "+ d); 
      } 

     } 
    }); 
+0

你必須在'INSERT'無效的語法。嘗試使用'「插入圖形(價格)VALUES(?)」'併發布錯誤消息,如果它仍然無法正常工作。 –

+0

'graphics'的表格定義如何? –

+0

我可能需要更新表格內的所有列嗎?因爲價格(KM)是其中之一8.它不顯示任何程序的錯誤消息。它只是凍結程序,我不得不停止控制檯。我應該如何獲得表格定義,我沒有在我的代碼中? – Ibrahimf

回答

0

你正在做的一切內部actionPerformed()方法,這是方法的本地。如果你想更新mysql表,你可以打開相同的東西或相應地改變你的程序。

+0

我應該如何更改代碼? – Ibrahimf

0

試試這個,

textField = new JTextField(); 
textField.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 

     try {Class.forName("com.mysql.jdbc.Driver"); 
      Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/csprogram","root",""); 
      price=scan.next(); 
// Insert query shouldn't have * 
      String query = "INSERT INTO `graphics` (price (KM)) VALUES (?);"; 
      PreparedStatement preparedStmt = conn.prepareStatement(query); 
      preparedStmt.setString (1, price); 
      preparedStmt.executeUpdate(); 
     } 
      catch (Exception d) { 
      System.out.println("Error found "+ d); 
     } 

    } 
});