无效的列名 Gull

Invalid Column name Gull

我正在使用 Sql 服务器和 Netbeans。我正在使用以下代码执行更新查询

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

        //Connection establishment
        Connection con=DriverManager.getConnection("jdbc:sqlserver://DESKTOP-CU5U75J\SQLSERVER1;databaseName=SQLConnection","Fateh","Fateh");
        //Statement Object
        Statement st=con.createStatement();
        //For DML use executeUpdate method of Statement Class
        st.executeUpdate("INSERT INTO Emp"
                +"  VALUES("+2+",+Gull)");
        //Commit Statement
        con.commit();

        JOptionPane.showMessageDialog(this, "Message", "The Data is Entered", JOptionPane.INFORMATION_MESSAGE);

    } catch (ClassNotFoundException ex) {
        Logger.getLogger(Employe.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(Employe.class.getName()).log(Level.SEVERE, null, ex);
    }
}                                      

我收到以下错误

com.microsoft.sqlserver.jdbc.SQLServerException: Invalid column name 'Gull'.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:217)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1635)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.doExecuteStatement(SQLServerStatement.java:865)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement$StmtExecCmd.doExecute(SQLServerStatement.java:762)
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:6276)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1794)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:184)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:159)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeUpdate(SQLServerStatement.java:687)
    at Employe.addBtnActionPerformed(Employe.java:117)
    at Employe.access[=11=]0(Employe.java:17)
    at Employe.actionPerformed(Employe.java:55)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6533)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6298)
    at java.awt.Container.processEvent(Container.java:2237)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2295)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4889)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4526)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4467)
    at java.awt.Container.dispatchEventImpl(Container.java:2281)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access0(EventQueue.java:97)
    at java.awt.EventQueue.run(EventQueue.java:709)
    at java.awt.EventQueue.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
    at java.awt.EventQueue.run(EventQueue.java:731)
    at java.awt.EventQueue.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

请帮帮我

这一行有问题:

st.executeUpdate("INSERT INTO Emp"
            +"  VALUES("+2+",+Gull)");

更准确地说,字符串连接是乱七八糟的:

"INSERT INTO Emp"+"NALUES("+2+",+Gull)"

将评估为这样的字符串:INSERT INTO Emp VALUES(2,+Gull).

因为它抱怨 Gull 是无效的列名,所以没有这样的列。

所以我怀疑它是某个变量,您想将哪个值粘贴到那里。届时,您可能想尝试一下:

"INSERT INTO Emp" + "VALUES(" + 2 + "," + Gull + ")"

计算结果为:

INSERT INTO Emp VALUES(2,[whatever value Gull has])

但是,请记住,如果 Gull 是字符串,则必须用引号引起来:

"INSERT INTO Emp" + "VALUES(" + 2 + ",'" + Gull + "')"

最后但同样重要的是,您很容易 SQL 注射!您可能也想考虑一下。