[英]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
来做到这一点。
ResultSet
in JTable
JTable
显示ResultSet
That said, there are other problems in your code. 也就是说,您的代码中还存在其他问题。
Class#forName()
everytime is unnecessary. Class#forName()
都是不必要的。 Just once during application's startup is enough. close()
on each of ResultSet
, Statement
and Connection
inside a finally
block will cause a resource leak. finally
块中的每个ResultSet
, Statement
和Connection
上调用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. ex
doesn't give worthy information. ex
不会提供有价值的信息。 It'll only print the exception type and message which isn't very helpful during debugging. 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.