简体   繁体   English

可以使用JDBC从数据库中检索一条记录,但不能检索另一条记录

[英]Can retrieve one record from database but not another using JDBC

So, for a school project, I am building a discord bot. 因此,对于一个学校项目,我正在构建一个不和谐的机器人。 One of the features that I have built in is that he can retrieve gif links from a MySQL database, and send them in a message. 我内置的功能之一是,他可以从MySQL数据库检索gif链接,并将其发送给消息。 Now, my issue is that I am only able to retrieve one record from my database, and no other records. 现在,我的问题是我只能从数据库中检索一条记录,而没有其他记录。 If I put the query that I use into MySQL workbench and run it, it will retrieve those records. 如果将我使用的查询放入MySQL工作台并运行它,它将检索这些记录。

This is the method for retrieving the gifs 这是检索gif的方法

public static ArrayList<Gif> GetGifsFromDB(String msg){
    ArrayList<Gif> gifs = new ArrayList<>();

    try(Connection conn = (Connection)DriverManager.getConnection(url, userName, password)){
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        Statement stmnt = conn.createStatement();
        String sql = "Select * from gif WHERE Type = '" + msg + "'";
        stmnt.execute(sql);

        try(ResultSet rs = stmnt.getResultSet()){
            while(rs.next()){
            Gif g = new Gif();
            g.setID(rs.getInt("GifID"));
            g.setURL(rs.getString("GifURL"));
                System.out.println(g.getID() + g.getURL());
            gifs.add(g);
        }
        rs.close();
        conn.close();
        }

    }
    catch(SQLException ex){
        System.err.println(ex.getMessage());
    }
    catch(Exception ex){
        System.err.println(ex.getMessage());
    }

    return gifs;
}

The "Type" in the database it just a category. 数据库中的“类型”只是一个类别。 With the test data I have in there, the 3 types are no, surprised and lonely. 根据我所拥有的测试数据,这3种类型都不是,令人惊讶且孤独。 Only no returns a gif. 只有no会返回gif。

Remove closing ResultSet and Connection lines: 删除结束的ResultSet和Connection行:

 rs.close();
 conn.close();

You are already closing it using try-with-resources 您已经在使用try-with-resources关闭它

Issue ended up being with MySql not committing records to the database. 问题最终是MySql没有将记录提交到数据库。 Once workbench was refreshed, the added records disappeared. 刷新工作台后,添加的记录消失。 Rather strange that even though the records weren't in the database, they could be retrieve. 奇怪的是,即使记录不在数据库中,也可以对其进行检索。

Most likely your msg is not exactly matching with any of the values for the database Type column. 您的msg很可能与数据库“ Type列的任何值都不完全匹配。

Test by running 通过运行进行测试

SELECT COUNT(*) FROM gif WHERE Type = '... put msg content here ...'

Do this manually directly on the database. 直接在数据库上手动执行此操作。

You can also try to put following line of code at the end: 您也可以尝试将以下代码放在最后:

System.out.println("Number of Selected Gifs: "+gifs.size());

If either of those results zero, then it means that msg was not exactly matched with Type . 如果这些结果之一为零,则表示msgType并不完全匹配。 Maybe uppercase/lowercase issue? 也许是大写/小写的问题?

Also to avoid SQL Injection, and other issues, please strongly consider using bind variables using a PreparedStatement . 另外,为避免SQL注入和其他问题,请强烈考虑使用PreparedStatement使用绑定变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM