gpt4 book ai didi

java - 在 JDBC 中查看结果集时不检索记录

转载 作者:行者123 更新时间:2023-11-29 01:27:47 25 4
gpt4 key购买 nike

<分区>

现在我正在学习 Java 中的结果集类型。在这里,我编写了以不同方式查看记录的代码。起初我显示了 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();

}
}
}
}

输出

显示最后一行的记录编号:5GMAIL : 纳文雅虎:naveenrockz

显示第一行的记录编号:1GMAIL : Wine 雅虎 : vinothasd

显示下一行的记录编号:2GMAIL : 邮件雅虎:ajith234

请让我明白。为什么我的代码没有获取 CODE SAMPLE 1 程序中的任何记录?

下图代表emp4表中的以下记录

enter image description here

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com