java数据库操作,老是提示ResultSet已关闭
java数据库操作,老是提示ResultSet已关闭,这是怎么回事,代码如下:importjava.sql.*;publicclassDemo{publicstaticv...
java数据库操作,老是提示ResultSet已关闭,这是怎么回事,代码如下:
import java.sql.*;
public class Demo{
public static void main(String args[]){
try{Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");}
catch(ClassNotFoundException e){System.out.println(e);}
Que que=new Que();
Del del=new Del("113");
del.delRecord(); //删除键值“111”对应的记录
que.queRecords(); //查询此时的数据库,并将记录显示出来
}
}
class Del {
String number;
public Del(String number){
this.number=number;
}
public void delRecord(){ //删除某个键值对应的记录
Connection con;
Statement sql;
ResultSet rs;
String url="jdbc:sqlserver://127.0.0.1:1433;Database=test";
String user="test";
String password="123456";
try{
con=DriverManager.getConnection(url,user,password);
sql=con.createStatement();
rs=sql.executeQuery("select * from message");
while(rs.next()){
if(this.number.equals(rs.getString(1).trim()))
{
sql.executeUpdate("delete from message where number='"+this.number+"'");
break;
}
}
if(rs.isAfterLast())
{
System.out.println("数据库中无该主键所对应的值!");
}
con.close();
}
catch(SQLException e){
System.out.println(""+e);
}
}
}
class Que {
public Que(){}
public void queRecords(String number){
Connection con;
Statement sql;
ResultSet rs;
String url="jdbc:sqlserver://127.0.0.1:1433;Database=test";
String user="test";
String password="123456";
try {
con=DriverManager.getConnection(url,user,password);
sql=con.createStatement();
rs=sql.executeQuery("select * from message where number='"+number+"'");
rs.next();
String numbe=rs.getString(1).trim();
String nam=rs.getString(2).trim();
Date bir=rs.getDate(3);
double sal=rs.getDouble(4);
System.out.println(numbe+","+nam+","+bir+","+sal);
con.close();
}
catch(SQLException e){ System.out.println(""+e);
}
}
public void queRecords(){
Connection con;
Statement sql;
ResultSet rs;
String url="jdbc:sqlserver://127.0.0.1:1433;Database=test";
String user="test";
String password="123456";
try {
con=DriverManager.getConnection(url,user,password);
sql=con.createStatement();
rs=sql.executeQuery("select * from message");
while(rs.next())
{
String number=rs.getString(1).trim();
//System.out.println(number.length()) ;
String name=rs.getString(2).trim();
Date birth=rs.getDate(3);
float salary=rs.getFloat(4);
System.out.println(number+","+name+","+birth+","+salary);
}
con.close();
}
catch(SQLException e){
System.out.println(""+e);
}
}
}
解决了还有加分!
非常感谢一楼的回答。原来是这样啊,那有没有什么好办法解决呢?
二楼说的试过了,没用。
一楼的能给我个好的解决方法吗?非常感谢了。 展开
import java.sql.*;
public class Demo{
public static void main(String args[]){
try{Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");}
catch(ClassNotFoundException e){System.out.println(e);}
Que que=new Que();
Del del=new Del("113");
del.delRecord(); //删除键值“111”对应的记录
que.queRecords(); //查询此时的数据库,并将记录显示出来
}
}
class Del {
String number;
public Del(String number){
this.number=number;
}
public void delRecord(){ //删除某个键值对应的记录
Connection con;
Statement sql;
ResultSet rs;
String url="jdbc:sqlserver://127.0.0.1:1433;Database=test";
String user="test";
String password="123456";
try{
con=DriverManager.getConnection(url,user,password);
sql=con.createStatement();
rs=sql.executeQuery("select * from message");
while(rs.next()){
if(this.number.equals(rs.getString(1).trim()))
{
sql.executeUpdate("delete from message where number='"+this.number+"'");
break;
}
}
if(rs.isAfterLast())
{
System.out.println("数据库中无该主键所对应的值!");
}
con.close();
}
catch(SQLException e){
System.out.println(""+e);
}
}
}
class Que {
public Que(){}
public void queRecords(String number){
Connection con;
Statement sql;
ResultSet rs;
String url="jdbc:sqlserver://127.0.0.1:1433;Database=test";
String user="test";
String password="123456";
try {
con=DriverManager.getConnection(url,user,password);
sql=con.createStatement();
rs=sql.executeQuery("select * from message where number='"+number+"'");
rs.next();
String numbe=rs.getString(1).trim();
String nam=rs.getString(2).trim();
Date bir=rs.getDate(3);
double sal=rs.getDouble(4);
System.out.println(numbe+","+nam+","+bir+","+sal);
con.close();
}
catch(SQLException e){ System.out.println(""+e);
}
}
public void queRecords(){
Connection con;
Statement sql;
ResultSet rs;
String url="jdbc:sqlserver://127.0.0.1:1433;Database=test";
String user="test";
String password="123456";
try {
con=DriverManager.getConnection(url,user,password);
sql=con.createStatement();
rs=sql.executeQuery("select * from message");
while(rs.next())
{
String number=rs.getString(1).trim();
//System.out.println(number.length()) ;
String name=rs.getString(2).trim();
Date birth=rs.getDate(3);
float salary=rs.getFloat(4);
System.out.println(number+","+name+","+birth+","+salary);
}
con.close();
}
catch(SQLException e){
System.out.println(""+e);
}
}
}
解决了还有加分!
非常感谢一楼的回答。原来是这样啊,那有没有什么好办法解决呢?
二楼说的试过了,没用。
一楼的能给我个好的解决方法吗?非常感谢了。 展开
3个回答
展开全部
/*
* 直接给你写了一个工具类,拿去用就行了
* */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Database {
private static Connection conn;
private static Statement stm;
private static ResultSet rs;
private static final String url = "jdbc:sqlserver://127.0.0.1:1433;Database=test";
private static String user = "test";
private static String password = "123456";
public static Connection getConnection() throws SQLException {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return DriverManager.getConnection(url, user, password);
}
public static Statement getStatement() throws SQLException{
conn = getConnection();
stm = conn.createStatement();
return stm;
}
public static ResultSet executeQuery(String sql) throws SQLException{
rs = getStatement().executeQuery(sql);
return rs;
}
public static ResultSet executeQueryById(String id) throws SQLException{
String sql = "select * from message where number=";
rs = getStatement().executeQuery(sql+id);
return rs;
}
public static void del(String id) throws SQLException{
String sql = "delete from message where number=";
getStatement().executeUpdate(sql+id);
}
public static void close(){
try {
if(conn != null) conn.close();
if(stm != null) stm.close();
if(rs != null) rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
* 直接给你写了一个工具类,拿去用就行了
* */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Database {
private static Connection conn;
private static Statement stm;
private static ResultSet rs;
private static final String url = "jdbc:sqlserver://127.0.0.1:1433;Database=test";
private static String user = "test";
private static String password = "123456";
public static Connection getConnection() throws SQLException {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return DriverManager.getConnection(url, user, password);
}
public static Statement getStatement() throws SQLException{
conn = getConnection();
stm = conn.createStatement();
return stm;
}
public static ResultSet executeQuery(String sql) throws SQLException{
rs = getStatement().executeQuery(sql);
return rs;
}
public static ResultSet executeQueryById(String id) throws SQLException{
String sql = "select * from message where number=";
rs = getStatement().executeQuery(sql+id);
return rs;
}
public static void del(String id) throws SQLException{
String sql = "delete from message where number=";
getStatement().executeUpdate(sql+id);
}
public static void close(){
try {
if(conn != null) conn.close();
if(stm != null) stm.close();
if(rs != null) rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2009-11-24
展开全部
sql=con.createStatement();
把这句改成:
sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY );
再试试
把这句改成:
sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY );
再试试
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在默认情况下,同一时间每个 Statement 对象在只能打开一个 ResultSet 对象。因此,如果读取一个 ResultSet 对象与读取另一个交叉,则这两个对象必须是由不同的 Statement 对象生成的。如果存在某个语句的打开的当前 ResultSet 对象,则 Statement 接口中的所有执行方法都会隐式关闭它。
你的delRecord方法里,rs开始由executeQuery得到,然后在while循环内,又执行了executeUpdate,所以rs被关闭,在执行rs.isAfterLast()时出现异常。。
rs=sql.executeQuery("select * from message");
while(rs.next()){
if(this.number.equals(rs.getString(1).trim()))
{
sql.executeUpdate("delete from message where number='"+this.number+"'");
break;
}
}
你的delRecord方法里,rs开始由executeQuery得到,然后在while循环内,又执行了executeUpdate,所以rs被关闭,在执行rs.isAfterLast()时出现异常。。
rs=sql.executeQuery("select * from message");
while(rs.next()){
if(this.number.equals(rs.getString(1).trim()))
{
sql.executeUpdate("delete from message where number='"+this.number+"'");
break;
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询