【问题标题】: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 'null' at line 1您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“null”附近使用正确的语法
【发布时间】:2019-03-12 02:38:59
【问题描述】:

当我尝试执行以下代码时,我收到此错误: 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“null”附近使用正确的语法

我是java新手,请耐心等待

注意:My_info 文件有数据库、用户名和密码的详细信息

public class acesss {


        public static void main(String[] args) {
            Connection connect = null;
            Statement statement = null;
            ResultSet resultSet = null;

            Properties identity = new Properties();


            String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
            String info_file = rootPath + "my_info";


            try {
              identity.load(new FileInputStream(info_file));
            } catch (Exception e) {
            }



            String user = identity.getProperty("user");
            String password = identity.getProperty("password");
            String database = identity.getProperty("database");

            try {

                Class.forName("com.mysql.cj.jdbc.Driver");


                connect = DriverManager.getConnection("jdbc:mysql://db.ca:3310", user, password);


                statement = connect.createStatement();


                statement.executeQuery("use "+ database+";");


                resultSet = statement.executeQuery("select * from product;");

                while (resultSet.next()) {

                    System.out.print(resultSet.getString("LastName")+" | ");
                    System.out.print(resultSet.getString("product_name")+" | ");
                }

            } catch (Exception e) {
                System.out.println(e.getMessage());
            } finally {


                try {
                    if (resultSet != null) {
                        resultSet.close();
                    }

                    if (statement != null) {
                        statement.close();
                    }

                    if (connect != null) {
                        connect.close();
                    }
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
            }

        }
    }

【问题讨论】:

  • 我猜database 为空 - 调试和检查
  • 通常你也按照String url = "jdbc:mysql://127.0.0.1:3306/yourdatabase";声明你的数据库
  • 您在加载属性文件时忽略了异常(空的 catch 块)。不要那样做,因为你现在确实看到了错误。可能找不到您的属性文件
  • 数据库不为空,我查过了

标签: java jdbc ojdbc mssql-jdbc


【解决方案1】:

我修正了错误。 my_info 文件有问题。我不得不交换用户名、数据库和密码的顺序,它工作正常。

【讨论】:

    猜你喜欢
    • 2016-03-04
    • 2013-11-22
    • 1970-01-01
    • 1970-01-01
    • 2019-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多