【发布时间】: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