您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在第1行的“null”附近使用正确的语法

问题描述 投票:0回答:1

当我尝试执行以下代码时,我收到此错误:您的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());
                }
            }

        }
    }
java jdbc ojdbc mssql-jdbc
1个回答
0
投票

我修正了错误。 my_info文件存在一些问题。我必须交换用户名,数据库和密码的顺序,并且它有效。

© www.soinside.com 2019 - 2024. All rights reserved.