简体   繁体   English

java数据库连接检索

[英]java database connection retrieval

I am using the code below to display the records from my database in the swing form. 我使用下面的代码以摆动形式显示我的数据库中的记录。 However, when I click on the button it only shows the first record of the database (table in database has 5 records) 但是,当我点击按钮时,它只显示数据库的第一条记录(数据库中的表有5条记录)

private void btnnextActionPerformed(java.awt.event.ActionEvent evt) {                                        
    try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con=DriverManager.getConnection("jdbc:odbc:StudentDetails");
            Statement stm=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
            ResultSet rs=stm.executeQuery("Select * from NurseryStDetails");

                if(rs.first())
                {
                    txtstID.setText(rs.getString(1));
                    txtname.setText(rs.getString(2));
                    txtaddress.setText(rs.getString(3));
//                    int i=rs.getInt(4);
//                    String s=String.valueOf(i);
//                    txtage.setText(rs.getString(s));
//                    int j=rs.getInt(5);
//                    String t=String.valueOf(j);
                    txtage.setText(rs.getString(4));
                    txttelephone.setText(rs.getString(5));
                    txtgNIC.setText(rs.getString(6));
                    txtgname.setText(rs.getString(7));
                }
            }
        catch(Exception ex)
            {
                System.out.println("Exception caught"+ex);
            }

    }   

is it 是吗

if(rs.first()) ??

try 尝试

while (rs.next())

This will help u to iterate through the resultset. 这将有助于您遍历结果集。

Well, you've indeed written the code that way. 好吧,你确实已经编写了代码。 Here's a cite of the Javadoc of ResultSet#first() : 这里是ResultSet#first()Javadoc的引用:

Moves the cursor to the first row in this ResultSet object. 将光标移动到此ResultSet对象的第一行。

Returns: true if the cursor is on a valid row; 返回:如果游标在有效行上,则返回 true ; false if there are no rows in the result set. 如果结果集中没有行,则返回false

You see, all it does it moving the cursor to the first row. 你看,它只是将光标移动到第一行。 Nothing more. 而已。

You basically need to replace this by ResultSet#next() in a while loop. 你基本上需要在while循环中用ResultSet#next()替换它。 It will then iterate through all rows as long as there's a row. 只要有一行,它就会遍历所有行。

while (rs.next()) {
    // ...
}

But there's another problem. 但还有另一个问题。 If you make only that change, then the code would end up to display only the data of the last row since you're reusing the same text components for that everytime. 如果只进行了那次更改,那么代码最终只会显示最后一行的数据,因为您每次都重复使用相同的文本组件。 You want to display each row separately in new fields in your UI. 您希望在UI的新字段中单独显示每一行。 I don't do Swing, so I can't give a detailed answer from top of my head, but I at least know that you would like use to use JTable for this. 我不做Swing,所以我无法从头脑中给出详细的答案,但我至少知道你想使用JTable来做到这一点。

See also: 也可以看看:


That said, there are other problems in your code. 也就是说,您的代码中还存在其他问题。

  1. Calling Class#forName() everytime is unnecessary. 每次调用Class#forName()都是不必要的。 Just once during application's startup is enough. 在应用程序启动期间只需一次即可。
  2. Not calling close() on each of ResultSet , Statement and Connection inside a finally block will cause a resource leak. 不在finally块中的每个ResultSetStatementConnection上调用close()将导致资源泄漏。 If you continue running this for a long term, the DB will run out of connections sooner or later and your application will not be able to connect it anymore and break. 如果你长期继续运行它,数据库迟早会用完连接,你的应用程序将无法再连接它并中断。 Fix it accordingly. 相应地修复它。
  3. Just printing the ex doesn't give worthy information. 只打印ex不会提供有价值的信息。 It'll only print the exception type and message which isn't very helpful during debugging. 它只会打印异常类型和消息,这在调试过程中不是很有用。 You'd like to do ex.printStackTrace() instead. 你想做ex.printStackTrace()而不是。

if(rs.first()) 如果(rs.first())

That checks if you're on the first record in the resultset 检查您是否在结果集中的第一条记录上

JDBC ResultSet object can be taken to be pointing to a location before the start of the result rows of the query being executed. 可以将JDBC ResultSet对象指向正在执行的查询的结果行的开始之前的位置。 the first rs.next() makes it point to the first row returned.subsequent calls to rs.next() moves it forwards in the resultant rows. 第一个rs.next()使它指向返回的第一行。对rs.next()的后续调用将它在结果行中向前移动。 Hence to display all the results use the concept 因此,要显示所有结果,请使用该概念

while(rs.next()){
  //use rs to get the details of the current row.
}

rs.next() returns true if the next row exists. 如果下一行存在,则rs.next()返回true。

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

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