Ygmxy
V2EX  ›  Java

Java JDBC 执行 sql 语句时报错

  •  
  •   Ygmxy · Oct 22, 2019 · 6881 views
    This topic created in 2588 days ago, the information mentioned may be changed or developed.
    代码:
    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


    求助各位大佬
    33 replies  •  2019-10-23 15:40:57 +08:00
    nvkou
    nvkou
    Oct 22, 2019 via Android
    1
    这是道英语题
    lukaz
    lukaz
    Oct 22, 2019 via Android
    2
    SQL 语法错误,先确保 SQL 语句在数据库中正常执行
    MaiKuraki
    MaiKuraki
    Oct 22, 2019
    3
    这是道英语题
    lyusantu
    lyusantu
    Oct 22, 2019
    4
    localhost:3306
    shuAS
    shuAS
    Oct 22, 2019
    5
    You have an error in your SQL syntax
    mineqiqi
    mineqiqi
    Oct 22, 2019
    6
    没选 database
    ZredoC
    ZredoC
    Oct 22, 2019
    7
    同意 2L
    还有 finally 回收资源的时候,个人习惯
    if (rs != null) rs.close();
    if (state != null) state.close();
    if (conn != null) conn.close();
    dif
    dif
    Oct 22, 2019
    8
    @ZredoC 回收资源不都是先开的后关么?
    sevenstone
    sevenstone
    Oct 22, 2019 via Android
    9
    网上搜一下比这问麻烦?
    smilzman
    smilzman
    Oct 22, 2019
    10
    看着没什么问题啊,mysql 版本和 jar 版本的问题?
    ZredoC
    ZredoC
    Oct 22, 2019
    11
    @dif 没错啊,不是 connection 连接然后 statment 发 sql 和返回,再 resultset 接收返回吗,先关 rs 再关 state 再关 conn。。
    iiicarus
    iiicarus
    Oct 22, 2019
    12
    String url = "jdbc:mysql://localhost/jdbc?user=root&password=123456&serverTimezone=UTC";

    端口没有
    taogen
    taogen
    Oct 22, 2019 via iPhone
    13
    表名和字段名和数据库一样了吗?把你的 sql 到数据库客户端执行通过,再复制到代码中。
    chendy
    chendy
    Oct 22, 2019
    14
    2019 年了,关资源用 try with resource 吧(至少 statement 和 resultSet 可以)
    dog82
    dog82
    Oct 22, 2019
    15
    连接串有问题
    bjking2014
    bjking2014
    Oct 22, 2019
    16
    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
    weo0
    Oct 22, 2019
    17
    现在还有这样操作 db 的?
    heraldic
    heraldic
    Oct 22, 2019
    18
    database 没选呢
    Marstin
    Marstin
    Oct 22, 2019
    19
    百度 1 分钟解决 ×
    论坛 1 小时水贴 √
    wysnylc
    wysnylc
    Oct 22, 2019
    20
    @ZredoC #7 try/close
    try(Resource res = xxx)//可指定多个资源
    {

    work with res

    }
    HolmLoh
    HolmLoh
    Oct 22, 2019
    21
    @ZredoC #7
    如果是 java8 的话我倾向于用 try with resources
    jay0726
    jay0726
    Oct 22, 2019
    22
    try with resources +1
    aborigine
    aborigine
    Oct 22, 2019
    23
    不需要端口,默认自动 3306,你没选 db
    Jrue0011
    Jrue0011
    Oct 22, 2019❤️1
    24
    如果你的数据库在本地,端口是默认的 3306,连接的 database 叫 jdbc、table 叫 emp,emp 里存在 id 和 deptno 字段,且 deptno 字段类型是数字的话,看起来都没什么问题。那还是确认外部环境吧,比如你的 mysql 是什么版本,jdbc 驱动是什么版本,有没有多个 jdbc 的 jar 包导致冲突。。。
    Ygmxy
    Ygmxy
    OP
    Oct 22, 2019
    25
    @Jrue0011 感谢
    saberlove
    saberlove
    Oct 22, 2019
    26
    这是道英语题
    Ygmxy
    Ygmxy
    OP
    Oct 22, 2019
    27
    @Jrue0011 请问有没有是 jar 包的版本不同是不是造成我这个报错的呢?我看是跟视频学的视频里的 jar 包是 11,我是 13
    Ygmxy
    Ygmxy
    OP
    Oct 22, 2019
    28
    JieS
    JieS
    Oct 22, 2019
    29
    神 tm 英语题
    Jrue0011
    Jrue0011
    Oct 22, 2019❤️1
    30
    @Ygmxy 11、13 是指 JDK 版本吧?我说的版本是 mysql 数据库的版本和你用来连接 mysql 的 jdbc 驱动 mysql-connector-java 的版本
    hantsy
    hantsy
    Oct 23, 2019
    31
    url 连接字符格式问题,好多年没调用 Jdbc API 了,尝试将 ?& 全部 URL Encoded 转译一下。
    saltedFish666
    saltedFish666
    Oct 23, 2019
    32
    那什么这个是英语,你看下这个 sql 能不能直接执行
    Ygmxy
    Ygmxy
    OP
    Oct 23, 2019
    33
    @saltedFish666 问题解决了,谢啦
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1963 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 72ms · UTC 08:32 · PVG 16:32 · LAX 00:32 · JFK 03:32
    ♥ Do have faith in what you're doing.