Java com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:sun.reflect 的“字段列表”中的未知列“raj”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24910827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:33:21  来源:igfitidea点击:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'raj' in 'field list' at sun.reflect

javamysqljdbc

提问by Rajendra Arora

I'm making a simple phone book diary, so here is my database schema:

我正在制作一个简单的电话簿日记,所以这是我的数据库架构:

create database if not exists Rajendra;

use Rajendra;

create table if not exists family(name varchar(50), mobile varchar(50));

select * from family;

Now, i'm inserting values from input dialog boxesbut that's giving me this kind of errors:

现在,我正在插入值,input dialog boxes但这给了我这种错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'raj' in 'field list'
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2617)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2778)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2819)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1811)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1725)
    at phonebook.MainProgram.actionPerformed(MainProgram.java:76)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.AbstractButton.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access0(Unknown Source)
    at java.awt.EventQueue.run(Unknown Source)
    at java.awt.EventQueue.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.run(Unknown Source)
    at java.awt.EventQueue.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

And here is my code for java:

这是我的Java代码:

jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
                try{
                    Class.forName("com.mysql.jdbc.Driver");
                    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/Rajendra", "root", "1234");
                    Statement st=con.createStatement();
                    st.executeUpdate("INSERT into family(name, mobile)"
                            +"values("+name+", "+num+")");
                }catch(Exception e){e.printStackTrace();}
            }
        });

回答by Jens

You have to add quotes arround the strings.

您必须在字符串周围添加引号。

st.executeUpdate("INSERT into family(name, mobile)"
                            +"values('"+name+"', '"+num+"')");

A better Solution is to learn about prepared statement. It prevent sql-injection and makes the statement better readable.

更好的解决方案是了解准备好的语句。它可以防止 sql 注入并使语句更具可读性。

回答by mkazma

You have to add double quote in the executeUpdate statement so that mysql will consider it as String input like this:

您必须在 executeUpdate 语句中添加双引号,以便 mysql 将其视为字符串输入,如下所示:

st.executeUpdate("INSERT into family(name, mobile)"
                            +"values('"+name+"', '"+num+"')");

回答by SparkOn

Instead of this :

而不是这个:

Statement st=con.createStatement();
st.executeUpdate("INSERT into family(name, mobile) values("+name+", "+num+")");

Why dont you try something like this

你为什么不尝试这样的事情

PreparedStatement st=con.prepareStatement("INSERT INTO family(name, mobile) VALUES(?,?)");
st.setString(1,name);
st.setString(2,num);
st.executeUpdate();

Advantagesof PreparedStatement

优势PreparedStatement

  1. Parameterized query is a good way to avoid SQL Injection
  2. Precompilation and DB-side caching of the SQL statement leads to overall faster execution.
  1. 参数化查询是避免 SQL 注入的好方法
  2. SQL 语句的预编译和 DB 端缓存导致整体执行速度更快。