2015-02-12 47 views
1

我想從JTextField中,但與一些條件選擇項目從數據庫中的Java

插入數據庫中的數據,所以我嘗試這個代碼

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
    try { 
     Class.forName("com.mysql.jdbc.Driver"); 
     String m = "IMCDietitian"; 
     String unicode= "?useUnicode=yes&characterEncoding=UTF-8"; 
     Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/"+m+""+unicode+"","root",""); 
     System.out.println("connected"); 
     String dept = jComboBox1.getSelectedItem().toString(); 
     if (jTextField6.getText() != null) 
     { 
      String pname = jTextField6.getText(); 
      Statement st = conn.createStatement(); 
      ResultSet rs = st.executeQuery("SELECT * FROM '"+dept+"' WHERE pname = '"+pname+"' "); 
      while (rs.next()) 
      { 
       if (!rs.getString(8).equals("-") && rs.getString(1).equals(pname)) 
       { 
        if (jCheckBox1.isSelected() == true) 
        { 
         st.executeQuery("INSERT INTO '"+dept+"' VALUES ('"+jTextField6.getText()+"', '"+jTextField5.getText()+"', '"+jTextField4.getText()+"', 'يوجد', '"+jTextField3.getText()+"', '"+jTextField2.getText()+"', '"+jTextField1.getText()+"', '-'"); 
        } 
        if (jCheckBox1.isSelected() == false) 
        { 
         st.executeQuery("INSERT INTO '"+dept+"' VALUES ('"+jTextField6.getText()+"', '"+jTextField5.getText()+"', '"+jTextField4.getText()+"', 'لا يوجد', '-', '"+jTextField2.getText()+"', '"+jTextField1.getText()+"', '-'"); 
        } 
       } 
       if (!rs.getString(1).equals(pname)) 
       { 
        if (jCheckBox1.isSelected() == true) 
        { 
         st.executeQuery("INSERT INTO '"+dept+"' VALUES ('"+jTextField6.getText()+"', '"+jTextField5.getText()+"', '"+jTextField4.getText()+"', 'يوجد', '"+jTextField3.getText()+"', '"+jTextField2.getText()+"', '"+jTextField1.getText()+"', '-'"); 
        } 
        if (jCheckBox1.isSelected() == false) 
        { 
         st.executeQuery("INSERT INTO '"+dept+"' VALUES ('"+jTextField6.getText()+"', '"+jTextField5.getText()+"', '"+jTextField4.getText()+"', 'لا يوجد', '-', '"+jTextField2.getText()+"', '"+jTextField1.getText()+"', '-'"); 
        } 
       } 
      } 
     } 
    } 
    catch (Exception e) { 
     e.printStackTrace(); 
    } 

}           

,但這個錯誤是accour

com.mysql.jdbc.exceptions.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 ''gc' WHERE pname = 'محمد'' at line 1 
     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
     at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) 
     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) 
     at java.lang.reflect.Constructor.newInstance(Constructor.java:513) 
     at com.mysql.jdbc.Util.handleNewInstance(Util.java:406) 
     at com.mysql.jdbc.Util.getInstance(Util.java:381) 
     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030) 
     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956) 
     at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491) 
     at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423) 
     at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936) 
     at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060) 
     at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2536) 
     at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2465) 
     at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1383) 
     at dietition.EDietListJFrame.jButton1ActionPerformed(EDietListJFrame.java:2351) 
     at dietition.EDietListJFrame.access$900(EDietListJFrame.java:29) 
     at dietition.EDietListJFrame$10.actionPerformed(EDietListJFrame.java:963) 
     at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995) 
     at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318) 
     at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387) 
     at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242) 
     at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236) 
     at java.awt.Component.processMouseEvent(Component.java:6263) 
     at javax.swing.JComponent.processMouseEvent(JComponent.java:3267) 
     at java.awt.Component.processEvent(Component.java:6028) 
     at java.awt.Container.processEvent(Container.java:2041) 
     at java.awt.Component.dispatchEventImpl(Component.java:4630) 
     at java.awt.Container.dispatchEventImpl(Container.java:2099) 
     at java.awt.Component.dispatchEvent(Component.java:4460) 
     at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574) 
     at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238) 
     at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168) 
     at java.awt.Container.dispatchEventImpl(Container.java:2085) 
     at java.awt.Window.dispatchEventImpl(Window.java:2475) 
     at java.awt.Component.dispatchEvent(Component.java:4460) 
     at java.awt.EventQueue.dispatchEvent(EventQueue.java:599) 
     at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) 
     at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) 
     at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) 
     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) 
     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) 
     at java.awt.EventDispatchThread.run(EventDispatchThread.java:122) 

++++那是什麼? wkey 0x42(66)

任何人都可以提供幫助嗎?

+0

這似乎是一個編碼問題,我看你使用的是一些阿拉伯文字符 – 2015-02-12 09:11:38

+2

從來沒有使用用戶輸入直接進行sql查詢,這是一個潛在的安全風險http://www.w3schools.com/sql/sql_injection.asp 使用準備好的語句代替http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html – wastl 2015-02-12 09:13:01

+0

錯誤消息非常明確:「您的SQL中有錯誤」。您應該在代碼中添加一些調試輸出,以便查看查詢的樣子。我想,你遇到了編碼問題,因爲你使用阿拉伯字符。 – user1438038 2015-02-12 09:15:45

回答

0

嘗試阿拉伯語文本之前添加N

代碼的所有結構的
SELECT * FROM table WHERE column=N'arabic text' 
+0

它不起作用 – 2015-02-12 09:50:25

0

首先是完全沒有什麼人會希望在生產級代碼來看看。直接從UI組件訪問數據庫很可能在某個時間點成爲問題。

然而,你得到的例外是非常清楚。 JDBC驅動程序無法解釋您的SQL語句。原因是它無法理解插入語句中的阿拉伯字符。

您應該在評論中按照用戶wastl的提示使用prepared statements。這不僅可以幫助您避免SQL injections形式的潛在安全風險,還可以幫助在您的語句中使用非ASCII字符編碼。

一旦您已經理解並掌握了準備好的語句,您可能想要將其轉換爲更復雜的數據模型白皮膚對象到關係映射,如javax.persistence及其着名的Hibernate實現中所示。

0

,因爲它似乎語法錯誤是由於一個逗號[「],你可能需要使用反斜槓跳過[]前一個逗號[」],併爲此目的,你可以使用下面所提到的語法:

更新user set firstName ='Ù...ØÙ...د\''其中userid ='test';

相關問題