com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'poi' in 'where clause'


Question

Am trying to create a new database by reading a value from a TextField. Before creating the database I decided to check whether it is present or not. My code is below.

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

}

I ran this code. I filled all the fields in the form and pressed the button. Then I got this exception -

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)

Why this exception is generated ? How can I solve it ?

1
1
4/10/2014 6:47:40 AM

Accepted Answer

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

to

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

You need to enclose the where value with signle quotes for string else the it will throw error.

Here is an example

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 |
+----------+
2
4/10/2014 6:51:36 AM

Your query is wrong.

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

That's wrong. Always pass parameter names in single quotes like this:

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

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon