com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:'where子句'中的未知列'poi'

时间:2014-04-10 06:47:41

标签: mysql exception javafx

尝试通过从TextField读取值来创建新数据库。在创建数据库之前,我决定检查它是否存在。我的代码如下。

 public void createAccount() throws ClassNotFoundException, SQLException {
    try {

        dbName = t12.getText();
        Class.forName("com.mysql.jdbc.Driver");

        connect = DriverManager
                .getConnection("jdbc:mysql://localhost:3306/project?"
                        + "user=root&password=virus");
        statement = connect.createStatement();

        preparedStatement = connect
                .prepareStatement("SELECT COUNT(*) FROM information_schema.tables \n" +
                       "WHERE table_schema = "+dbName+"");
        rs=preparedStatement.executeQuery();
        rs.next();
        int chk = rs.getInt(1);

        if(chk!=1)
        {            
        int resultset = statement.executeUpdate("create database " + dbName );

        connect = DriverManager
                .getConnection("jdbc:mysql://localhost:3306/"+ dbName +"?"
                        + "user=root&password=virus");
        statement = connect.createStatement();

        preparedStatement = connect
                .prepareStatement("create table " + dbName + " (fullname varchar(30),"
                        + "username varchar(20) primary key, password varchar(20),"
                        + "department varchar(30), semester int(2));");
        preparedStatement.executeUpdate();

        preparedStatement = connect
                .prepareStatement("insert into " + dbName + " values(?,?,?,?,?);");

        preparedStatement.setString(1, t11.getText());
        preparedStatement.setString(2, t12.getText());
        preparedStatement.setString(3, p11.getText());
        preparedStatement.setString(4, t13.getText());
        preparedStatement.setString(5, (String)comboBox.getValue());

        preparedStatement.executeUpdate();
        }

        else
        {  
            actiontarget = new Text();
            actiontarget.setFill(Color.FIREBRICK);
            actiontarget.setText("Try another User Name...!");
            actiontarget.setFont(Font.font(null, 15));
            setEffect(new BoxBlur(5, 10, 10));
            Stage usrpagestage = new Stage();
            usrpagestage.setMaxHeight(60);
            usrpagestage.setMaxWidth(200);
            usrpagestage.initStyle(StageStyle.UTILITY);
            usrpagestage.setScene(new Scene(new Warning()));
            usrpagestage.show();

            usrpagestage.setOnCloseRequest(new EventHandler<WindowEvent>() {
                @Override
                public void handle(WindowEvent t) {
                    setEffect(new BoxBlur(0, 0, 0));

                }
            });
        }

    } catch (ClassNotFoundException | SQLException e) {
        throw e;
    } finally {
        close2();
    }

}

我运行了这段代码。我填写表格中的所有字段并按下按钮。然后我得到了这个例外 -

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'poi' in 'where clause'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
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:1053)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2794)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2322)
at frontpage.FrontPage.createAccount(FrontPage.java:335)
at frontpage.FrontPage$8.handle(FrontPage.java:308)
at frontpage.FrontPage$8.handle(FrontPage.java:304)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:28)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Node.fireEvent(Node.java:6867)
at javafx.scene.control.Button.fire(Button.java:179)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:193)
at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:336)
at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:329)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:64)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3311)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3151)
at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3106)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1563)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2248)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:250)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:173)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:292)
at com.sun.glass.ui.View.handleMouseEvent(View.java:530)
at com.sun.glass.ui.View.notifyMouse(View.java:924)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67)
at java.lang.Thread.run(Thread.java:744)

为什么会生成此异常?我该如何解决?

3 个答案:

答案 0 :(得分:2)

preparedStatement = connect
                .prepareStatement("SELECT COUNT(*) FROM information_schema.tables \n" +
                       "WHERE table_schema = "+dbName+"");

preparedStatement = connect
                .prepareStatement("SELECT COUNT(*) FROM information_schema.tables \n" +
                       " WHERE table_schema = '"+dbName+"'");

你需要用字母用单引号括起where值,否则它会抛出错误。

这是一个例子

mysql> SELECT COUNT(*) FROM information_schema.tables where table_schema = users ;
ERROR 1054 (42S22): Unknown column 'users' in 'where clause'

mysql> SELECT COUNT(*) FROM information_schema.tables where table_schema = 'users' ;
+----------+
| COUNT(*) |
+----------+
|        0 |
+----------+

答案 1 :(得分:0)

您没有以正确的方式使用PreparedStatement

使用占位符?将值输入到查询中。通过使用set...方法,您可以将值设置为查询中的各种参数。

这对于处理转义字符和sql注入是安全的。

table_schema = poi的情况不会发生。但内部注意为table_name = 'poi'

更改

preparedStatement = 
  connect.prepareStatement("SELECT COUNT(*) FROM information_schema.tables \n" +
                           "WHERE table_schema = "+dbName+"");
rs=preparedStatement.executeQuery();

String sqlQuery = "SELECT COUNT(*) FROM information_schema.tables \n" +
                  "WHERE table_schema = ?";
preparedStatement = 
  connect.prepareStatement( sqlQuery );

preparedStatement.setString( 1, dbName );

rs=preparedStatement.executeQuery();

答案 2 :(得分:0)

您的查询错误。

preparedStatement = connect
                .prepareStatement("SELECT COUNT(*) FROM information_schema.tables \n" +
                       "WHERE table_schema = "+dbName+"");

那是错的。始终用单引号传递参数名称,如下所示:

preparedStatement = connect
                .prepareStatement("SELECT COUNT(*) FROM information_schema.tables \n" +
                       "WHERE table_schema = '"+dbName+"'");