我想從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)
任何人都可以提供幫助嗎?
這似乎是一個編碼問題,我看你使用的是一些阿拉伯文字符 – 2015-02-12 09:11:38
從來沒有使用用戶輸入直接進行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
錯誤消息非常明確:「您的SQL中有錯誤」。您應該在代碼中添加一些調試輸出,以便查看查詢的樣子。我想,你遇到了編碼問題,因爲你使用阿拉伯字符。 – user1438038 2015-02-12 09:15:45