Java JDBC 执行 sql 语句时报错

2019-10-22 08:50:11 +08:00
 Ygmxy
代码:
import java.sql.*;

public class PreparedStatement {
public static void main(String[] args) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e){
e.printStackTrace();
};
String url = "jdbc:mysql://localhost/jdbc?user=root&password=123456&serverTimezone=UTC";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;

try {

conn = DriverManager.getConnection(url);

stmt = conn.createStatement();

rs = stmt.executeQuery("select id from emp where deptno > 1"); //执行这行代码时报错

while (rs.next()) {
System.out.println(rs.getObject("id"));
}
} catch (SQLException e){
e.printStackTrace();
} finally {
try {
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}


报错信息:
java.sql.SQLSyntaxErrorException: 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 '?' at line 1
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.StatementImpl.executeQuery(StatementImpl.java:1200)
at PreparedStatement.main(PreparedStatement.java:21)
Exception in thread "main" java.lang.NullPointerException
at PreparedStatement.main(PreparedStatement.java:30)

Process finished with exit code 1


求助各位大佬
5390 次点击
所在节点    Java
33 条回复
nvkou
2019-10-22 08:56:56 +08:00
这是道英语题
lukaz
2019-10-22 08:59:48 +08:00
SQL 语法错误,先确保 SQL 语句在数据库中正常执行
MaiKuraki
2019-10-22 09:01:08 +08:00
这是道英语题
lyusantu
2019-10-22 09:15:54 +08:00
localhost:3306
shuAS
2019-10-22 09:17:02 +08:00
You have an error in your SQL syntax
mineqiqi
2019-10-22 09:18:36 +08:00
没选 database
ZredoC
2019-10-22 09:22:32 +08:00
同意 2L
还有 finally 回收资源的时候,个人习惯
if (rs != null) rs.close();
if (state != null) state.close();
if (conn != null) conn.close();
dif
2019-10-22 09:26:12 +08:00
@ZredoC 回收资源不都是先开的后关么?
sevenstone
2019-10-22 09:30:15 +08:00
网上搜一下比这问麻烦?
smilzman
2019-10-22 09:31:38 +08:00
看着没什么问题啊,mysql 版本和 jar 版本的问题?
ZredoC
2019-10-22 09:38:03 +08:00
@dif 没错啊,不是 connection 连接然后 statment 发 sql 和返回,再 resultset 接收返回吗,先关 rs 再关 state 再关 conn。。
iiicarus
2019-10-22 09:41:29 +08:00
String url = "jdbc:mysql://localhost/jdbc?user=root&password=123456&serverTimezone=UTC";

端口没有
taogen
2019-10-22 09:46:26 +08:00
表名和字段名和数据库一样了吗?把你的 sql 到数据库客户端执行通过,再复制到代码中。
chendy
2019-10-22 09:53:50 +08:00
2019 年了,关资源用 try with resource 吧(至少 statement 和 resultSet 可以)
dog82
2019-10-22 09:59:07 +08:00
连接串有问题
bjking2014
2019-10-22 10:00:56 +08:00
String url = "jdbc:mysql://localhost/jdbc?user=root&password=123456&serverTimezone=UTC";

String url = "jdbc:mysql://localhost [:3306] /jdbc?user=root&password=123456&serverTimezone=UTC";
weo0
2019-10-22 10:10:15 +08:00
现在还有这样操作 db 的?
heraldic
2019-10-22 10:15:54 +08:00
database 没选呢
Marstin
2019-10-22 10:37:23 +08:00
百度 1 分钟解决 ×
论坛 1 小时水贴 √
wysnylc
2019-10-22 10:59:32 +08:00
@ZredoC #7 try/close
try(Resource res = xxx)//可指定多个资源
{

work with res

}

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/611606

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX