查看 JDBC 中的结果集时不检索记录
Doesn't retrieve the records while viewing a ResultSet in JDBC
现在我正在学习 java 中的结果集类型。在这里,我编写了以不同方式查看记录的代码。起初我显示了 emp4 table 中的全部记录然后我开始以不同的方式查看这些记录(最后,第一,下一个)这正是我正在寻找的但它不会显示所有emp4 table 中显示的记录。请参阅第一个程序,它不起作用,但如果我记录了第 41 行(请参阅第二个程序中的这个),它就可以正常工作。有什么问题 ?我的代码有什么问题吗???
代码示例 1
package demojdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class MysqlCon{
private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_CONNECTION = "jdbc:mysql://localhost:3306/vinoth";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "vino";
public static void main(String args[])throws SQLException{
//Creating statement and connection
Connection dbConnection = null;
Statement stmt = null;
try{
//Creating class driver
Class.forName(DB_DRIVER);
//Creating Database Connection
dbConnection = DriverManager.getConnection(DB_CONNECTION,DB_USER,DB_PASSWORD);
//Creating statement
stmt = dbConnection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
//Creating query
String sql = "SELECT id,gmail,yahoo from emp4";
//Creating ResultSet
ResultSet rs = stmt.executeQuery(sql);
//Displaying database
System.out.println("Displaying records before doing some operations");
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
System.out.println("Displaying records for last row");
rs.last();
int id = rs.getInt("id");
String gmail = rs.getString("gmail");
String yahoo = rs.getString("yahoo");
//Displaying records in last row
System.out.println("ID : "+id);
System.out.println("GMAIL : "+gmail);
System.out.println("YAHOO : "+yahoo);
System.out.println();
rs.first();
System.out.println("Displaying records for first row");
id = rs.getInt("id");
gmail = rs.getString("gmail");
yahoo = rs.getString("yahoo");
//Displaying records in last row
System.out.println("ID : "+id);
System.out.println("GMAIL : "+gmail);
System.out.println("YAHOO : "+yahoo);
System.out.println();
rs.next();
System.out.println("Displaying records for next row");
id = rs.getInt("id");
gmail = rs.getString("gmail");
yahoo = rs.getString("yahoo");
//Displaying records in last row
System.out.println("ID : "+id);
System.out.println("GMAIL : "+gmail);
System.out.println("YAHOO : "+yahoo);
}catch(SQLException e){
e.printStackTrace();
}catch(ClassNotFoundException e){
System.out.println("Plese check the driver class path "+e.getMessage());
}finally{
if(stmt != null){
stmt.close();
}
if(dbConnection != null){
dbConnection.close();
}
}
}
}
在这里,代码可以正常工作......
代码示例 2
package demojdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class MysqlCon{
private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_CONNECTION = "jdbc:mysql://localhost:3306/vinoth";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "vino";
public static void main(String args[])throws SQLException{
//Creating statement and connection
Connection dbConnection = null;
Statement stmt = null;
try{
//Creating class driver
Class.forName(DB_DRIVER);
//Creating Database Connection
dbConnection = DriverManager.getConnection(DB_CONNECTION,DB_USER,DB_PASSWORD);
//Creating statement
stmt = dbConnection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
//Creating query
String sql = "SELECT id,gmail,yahoo from emp4";
//Creating ResultSet
ResultSet rs = stmt.executeQuery(sql);
//Displaying database
System.out.println("Displaying records before doing some operations");
//System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
System.out.println("Displaying records for last row");
rs.last();
int id = rs.getInt("id");
String gmail = rs.getString("gmail");
String yahoo = rs.getString("yahoo");
//Displaying records in last row
System.out.println("ID : "+id);
System.out.println("GMAIL : "+gmail);
System.out.println("YAHOO : "+yahoo);
System.out.println();
rs.first();
System.out.println("Displaying records for first row");
id = rs.getInt("id");
gmail = rs.getString("gmail");
yahoo = rs.getString("yahoo");
//Displaying records in last row
System.out.println("ID : "+id);
System.out.println("GMAIL : "+gmail);
System.out.println("YAHOO : "+yahoo);
System.out.println();
rs.next();
System.out.println("Displaying records for next row");
id = rs.getInt("id");
gmail = rs.getString("gmail");
yahoo = rs.getString("yahoo");
//Displaying records in last row
System.out.println("ID : "+id);
System.out.println("GMAIL : "+gmail);
System.out.println("YAHOO : "+yahoo);
}catch(SQLException e){
e.printStackTrace();
}catch(ClassNotFoundException e){
System.out.println("Plese check the driver class path "+e.getMessage());
}finally{
if(stmt != null){
stmt.close();
}
if(dbConnection != null){
dbConnection.close();
}
}
}
}
输出
显示最后一行的记录
编号:5
GMAIL : 纳文
雅虎:naveenrockz
显示第一行的记录
编号:1
GMAIL : 葡萄酒
雅虎 : vinothasd
显示下一行的记录
编号:2
GMAIL : 邮件
雅虎:ajith234
请让我明白。为什么我的代码没有获取 CODE SAMPLE 1 程序中的任何记录?
下图代表emp4中的以下记录table
原因是在使用getInt()
/ getString()
访问数据之前,您没有通过调用next()
来推进ResultSet
的游标。尝试这样的事情:
//Creating ResultSet
ResultSet rs = stmt.executeQuery(sql);
//Displaying database
System.out.println("Displaying records before doing some operations");
if (rs.next()) {
System.out.println(rs.getInt(1) + " "
+ rs.getString(2) + " " + rs.getString(3));
}
如果要遍历整个结果集,请改用 while (rs.next())
。
您的第二个代码片段有效,因为您在第一次访问列值之前使用 rs.last()
将光标移动到最后一个位置。
请注意,在访问列值之前,您应该始终检查 rs.next()
/ rs.last()
/ rs.first()
方法的 return 值。错误的 return 值表示结果集没有行,并且会导致在调用任何 getter(rs.getInt()
/ rs.getString()
等)方法时抛出异常结果集的数量。
需要使用.next()函数跳转到结果集中的第一条记录
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3)); }
现在我正在学习 java 中的结果集类型。在这里,我编写了以不同方式查看记录的代码。起初我显示了 emp4 table 中的全部记录然后我开始以不同的方式查看这些记录(最后,第一,下一个)这正是我正在寻找的但它不会显示所有emp4 table 中显示的记录。请参阅第一个程序,它不起作用,但如果我记录了第 41 行(请参阅第二个程序中的这个),它就可以正常工作。有什么问题 ?我的代码有什么问题吗???
代码示例 1
package demojdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class MysqlCon{
private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_CONNECTION = "jdbc:mysql://localhost:3306/vinoth";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "vino";
public static void main(String args[])throws SQLException{
//Creating statement and connection
Connection dbConnection = null;
Statement stmt = null;
try{
//Creating class driver
Class.forName(DB_DRIVER);
//Creating Database Connection
dbConnection = DriverManager.getConnection(DB_CONNECTION,DB_USER,DB_PASSWORD);
//Creating statement
stmt = dbConnection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
//Creating query
String sql = "SELECT id,gmail,yahoo from emp4";
//Creating ResultSet
ResultSet rs = stmt.executeQuery(sql);
//Displaying database
System.out.println("Displaying records before doing some operations");
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
System.out.println("Displaying records for last row");
rs.last();
int id = rs.getInt("id");
String gmail = rs.getString("gmail");
String yahoo = rs.getString("yahoo");
//Displaying records in last row
System.out.println("ID : "+id);
System.out.println("GMAIL : "+gmail);
System.out.println("YAHOO : "+yahoo);
System.out.println();
rs.first();
System.out.println("Displaying records for first row");
id = rs.getInt("id");
gmail = rs.getString("gmail");
yahoo = rs.getString("yahoo");
//Displaying records in last row
System.out.println("ID : "+id);
System.out.println("GMAIL : "+gmail);
System.out.println("YAHOO : "+yahoo);
System.out.println();
rs.next();
System.out.println("Displaying records for next row");
id = rs.getInt("id");
gmail = rs.getString("gmail");
yahoo = rs.getString("yahoo");
//Displaying records in last row
System.out.println("ID : "+id);
System.out.println("GMAIL : "+gmail);
System.out.println("YAHOO : "+yahoo);
}catch(SQLException e){
e.printStackTrace();
}catch(ClassNotFoundException e){
System.out.println("Plese check the driver class path "+e.getMessage());
}finally{
if(stmt != null){
stmt.close();
}
if(dbConnection != null){
dbConnection.close();
}
}
}
}
在这里,代码可以正常工作......
代码示例 2
package demojdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class MysqlCon{
private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_CONNECTION = "jdbc:mysql://localhost:3306/vinoth";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "vino";
public static void main(String args[])throws SQLException{
//Creating statement and connection
Connection dbConnection = null;
Statement stmt = null;
try{
//Creating class driver
Class.forName(DB_DRIVER);
//Creating Database Connection
dbConnection = DriverManager.getConnection(DB_CONNECTION,DB_USER,DB_PASSWORD);
//Creating statement
stmt = dbConnection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
//Creating query
String sql = "SELECT id,gmail,yahoo from emp4";
//Creating ResultSet
ResultSet rs = stmt.executeQuery(sql);
//Displaying database
System.out.println("Displaying records before doing some operations");
//System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
System.out.println("Displaying records for last row");
rs.last();
int id = rs.getInt("id");
String gmail = rs.getString("gmail");
String yahoo = rs.getString("yahoo");
//Displaying records in last row
System.out.println("ID : "+id);
System.out.println("GMAIL : "+gmail);
System.out.println("YAHOO : "+yahoo);
System.out.println();
rs.first();
System.out.println("Displaying records for first row");
id = rs.getInt("id");
gmail = rs.getString("gmail");
yahoo = rs.getString("yahoo");
//Displaying records in last row
System.out.println("ID : "+id);
System.out.println("GMAIL : "+gmail);
System.out.println("YAHOO : "+yahoo);
System.out.println();
rs.next();
System.out.println("Displaying records for next row");
id = rs.getInt("id");
gmail = rs.getString("gmail");
yahoo = rs.getString("yahoo");
//Displaying records in last row
System.out.println("ID : "+id);
System.out.println("GMAIL : "+gmail);
System.out.println("YAHOO : "+yahoo);
}catch(SQLException e){
e.printStackTrace();
}catch(ClassNotFoundException e){
System.out.println("Plese check the driver class path "+e.getMessage());
}finally{
if(stmt != null){
stmt.close();
}
if(dbConnection != null){
dbConnection.close();
}
}
}
}
输出
显示最后一行的记录 编号:5 GMAIL : 纳文 雅虎:naveenrockz
显示第一行的记录 编号:1 GMAIL : 葡萄酒 雅虎 : vinothasd
显示下一行的记录 编号:2 GMAIL : 邮件 雅虎:ajith234
请让我明白。为什么我的代码没有获取 CODE SAMPLE 1 程序中的任何记录?
下图代表emp4中的以下记录table
原因是在使用getInt()
/ getString()
访问数据之前,您没有通过调用next()
来推进ResultSet
的游标。尝试这样的事情:
//Creating ResultSet
ResultSet rs = stmt.executeQuery(sql);
//Displaying database
System.out.println("Displaying records before doing some operations");
if (rs.next()) {
System.out.println(rs.getInt(1) + " "
+ rs.getString(2) + " " + rs.getString(3));
}
如果要遍历整个结果集,请改用 while (rs.next())
。
您的第二个代码片段有效,因为您在第一次访问列值之前使用 rs.last()
将光标移动到最后一个位置。
请注意,在访问列值之前,您应该始终检查 rs.next()
/ rs.last()
/ rs.first()
方法的 return 值。错误的 return 值表示结果集没有行,并且会导致在调用任何 getter(rs.getInt()
/ rs.getString()
等)方法时抛出异常结果集的数量。
需要使用.next()函数跳转到结果集中的第一条记录
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3)); }