MySQLSyntaxErrorException

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQLSyntaxErrorException相关的知识,希望对你有一定的参考价值。

参考技术A Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'hibernatetest.xxx_table' doesn't exist

报错显示:无法找到表。

解决方案:查询是否配置自动创建表语句。

<property name="hibernate.hbm2ddl.auto">update</property>

如果创建之后还是报同样的错误:
请将:
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
替换成:
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>

原因:
Hibernate的版本与MySQL的版本不一样。

SQL 语法中的 MySQLSyntaxErrorException

【中文标题】SQL 语法中的 MySQLSyntaxErrorException【英文标题】:MySQLSyntaxErrorException in SQL syntax 【发布时间】:2015-09-08 21:01:29 【问题描述】:

我正在尝试使用准备好的语句从表中选择数据。但似乎我遇到了无法单独解决的语法错误。

try 

        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/mydb";
        String dbusername = "root";
        String dbpassword = ""; // Change it to your Password

        // Setup the connection with the DB
        connection = DriverManager.getConnection(url, dbusername,
                dbpassword);

        String query = "SELECT * FROM admin WHERE username = ? AND password = ?";

        try 
            // connection.setAutoCommit(false);
            selectUser = connection.prepareStatement(query);
            selectUser.setString(1, username);
            selectUser.setString(2, password);
            // Execute preparedstatement
            ResultSet rs = selectUser.executeQuery(query);

            // Output user details and query
            System.out.println("Your user name is " + username);
            System.out.println("Your password is " + password);
            System.out.println("Query: " + query);

            boolean more = rs.next();

            // if user does not exist set the validity variable to true
            if (!more) 
                System.out
                        .println("Sorry, you are not a registered user! Please sign up first!");
                user.setValid(false);
            

            // if user exists set the validity variable to true
            else if (more) 
                String name = rs.getString("name");

                System.out.println("Welcome " + name);
                user.setName(name);
                user.setValid(true);
            


         catch (Exception e) 
            System.out.println("Prepared Statement Error! " + e);
           
     catch (Exception e) 
        System.out.println("Log in failed: An exception has occured! " + e);
     finally 
    
    if (connection != null) 
        try 
            connection.close();
         catch (Exception e) 
            System.out.println("Connection closing exception occured! ");
        
        connection = null;
    

    return user;

我收到以下错误。

Prepared Statement Error! 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 '? AND password = ?' at line 1

但我在该代码行中没有看到任何错误。

【问题讨论】:

【参考方案1】:

您已经在此处的准备好的语句中加载了查询,

selectUser = connection.prepareStatement(query);

所以执行它,

 ResultSet rs = selectUser.executeQuery();

另请阅读,

How does PreparedStatement.executeQuery work?

【讨论】:

【参考方案2】:

改变

 ResultSet rs = selectUser.executeQuery(query);

 ResultSet rs = selectUser.executeQuery();

当您已经在connection.prepareStatement(query); 中准备好语句时,为什么还要在selectUser.executeQuery(query); 中再次传递查询

【讨论】:

谢谢!我不知道那件事。删除查询参数节省了我的时间。【参考方案3】:

你想做的就是使用这个method

 ResultSet rs = selectUser.executeQuery();

【讨论】:

以上是关于MySQLSyntaxErrorException的主要内容,如果未能解决你的问题,请参考以下文章

executeUpdate() 抛出 MySQLSyntaxErrorException

MySQLSyntaxErrorException:用户访问被拒绝

错误:MySQLSyntaxErrorException - 拒绝用户选择命令

MySQLSyntaxErrorException:未知数据库 - JDBC - Java EE

如何解决 JDBC PreparedStatement 中的 MySQLSyntaxErrorException [重复]

MySQLSyntaxErrorException