现在我在java中学习ResultSet类型。在这里,我编写了以不同方式查看记录的代码。首先,我显示了emp4表中的所有记录,然后我开始以不同的方式查看这些记录(上一个,第一个,下一个)这正是我正在寻找但它不会显示所有呈现的记录在emp4表中。看到第一个程序,它不起作用,但如果我记录第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();
}
}
}
}
输出
显示最后一行的记录 ID:5 GMAIL:naveen 雅虎:naveenrockz
显示第一行的记录 ID:1 GMAIL:vinothvino 雅虎:vinothasd
显示下一行的记录 ID:2 GMAIL:ajithvirje 雅虎:ajith234
请让我理解。为什么我的代码不能获取CODE SAMPLE 1程序中的任何记录?
下图代表emp4表中的以下记录
答案 0 :(得分:4)
原因是在使用ResultSet
/ getInt()
访问数据之前,您未通过调用next()
来推进getString()
的光标。尝试这样的事情:
//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()
方法的返回值。 falsey返回值表示结果集没有行,并且在调用结果集的任何getter(rs.getInt()
/ rs.getString()
等)方法时会引发异常。
答案 1 :(得分:1)
你需要使用.next()函数跳转到结果集中的第一条记录
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3)); }