java - 无法弄清楚如何摆脱mysql上的语法错误
问题描述
我正在尝试在 java 主文件上运行一条语句,并且我能够连接到数据库,但不运行 sql 语句我的代码是:
try {
System.out.println("Connecting to the database...");
conn=DriverManager.getConnection(DB_URL,USER,PASS);
System.out.println("Connected to database successfully");
System.out.println("Inserting");
stmt=conn.createStatement();
String sql="USE TheEmployeeDatabase" +
"SELECT * FROM EmployeeTable";
stmt.executeUpdate(sql);
System.out.println("worked");
} catch (SQLException e) {
e.printStackTrace();
}
并得到错误
You have an error in your SQL syntax
解决方案
String sql="USE TheEmployeeDatabase " <--- 你需要空格,否则你会连接 TheEmployeeDatabaseSELECT
推荐阅读
- sql - 合并两个查询以获取两列中的数据
- azure - 如何在 azure logic app 中获取两个日期时间变量的时间差(以毫秒为单位)
- xcode - Nativescript:iOS 模拟器的错误构建
- javascript - 是否有可靠的跨浏览器方式来捕获页面导航/重新加载/关闭?
- android - 已应用 Hilt Android Gradle 插件,但未找到 com.google.dagger:hilt-android 依赖项
- amazon-web-services - 使用 cloudformation 创建的 AWS 中手动更新的堆栈将来会导致冲突吗?
- java - 使用数据层 API 发送磨损传感器数据值
- bash - 将参数传递给 Makefile 时出现问题
- keras - 在训练网络时,iou_score 超过 100
- javascript - 使用 Ajax 将变量从 JS 传递到 PHP 时数据为空