从java中的数据库中选择项目

时间:2015-02-12 09:07:09

标签: java

我想从jtextfield插入数据库但有一些条件

所以我尝试了这段代码

 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();
    }

}                                        

但是这个错误是

 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)

任何人都可以提供帮助吗?

3 个答案:

答案 0 :(得分:0)

尝试在阿拉伯语文本之前添加N

SELECT * FROM table WHERE column=N'arabic text'

答案 1 :(得分:0)

首先,代码的结构完全不是人们希望在生产等级代码中看到的。直接从UI组件访问数据库很可能在某个时间点成为问题。

然而,你得到的例外情况非常清楚。 JDBC驱动程序无法解释您的SQL语句。原因是它无法理解插入语句中的阿拉伯字符。

您应该在评论中遵循用户wastl的提示并使用prepared statements。这不仅可以帮助您以SQL injections的形式避免潜在的安全风险,还可以帮助您在语句中使用非ASCII字符编码。

一旦理解并掌握了准备好的语句,您可能希望转移到javax.persistence及其着名的Hibernate实现中可用的更精细的数据模型whit对象到关系映射。

答案 2 :(得分:0)

由于单个逗号[']似乎是语法错误,您可能需要在单个逗号[']之前使用反斜杠[]跳过它,为此您可以使用下面提到的语法:

更新用户设置firstName ='Ù...ØÙ...د\''其中userid ='test';