java ResultSet问题请各位帮帮忙啊
import java.sql.*;
import java.io.*;
public class student{
String mySqlDriver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost/myproject";
public ResultSet searchstudent(String id)
{String searchsql="select * from student where id=?";
PreparedStatement ps=null;
ResultSet rset=null;
Connection con=null;
try{Class.forName(mySqlDriver);
con=DriverManager.getConnection(url,"root","791346");
ps=con.prepareStatement(searchsql);
ps.setString(1,id);
rset=ps.executeQuery();
}catch(Exception e){e.printStackTrace();}
finally{try{if(ps!=null)ps.close();
if(con !=null)con.close();
if(rset !=null)rset.close();
}catch(
SQLException ignore){}
}return rset;
}
public static void main(String[] args){
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
try{student stu=new student();
System.out.println("请输入id:");
String id=input.readLine();
ResultSet result=stu.searchstudent(id);
if(result==null&&!result.next())
System.out.println("表名或列名输入有误或到了末尾");
else {
System.out.println("查询结果为:");
do{
String result1=result.getString("id");
String result2=result.getString("sno");
String result3=result.getString("sname");
result1=new String(result1.getBytes("ISO-8859-1"),"GB2312");
result2=new String(result2.getBytes("ISO-8859-1"),"GB2312");
result3=new String(result3.getBytes("ISO-8859-1"),"GB2312");
System.out.println(result+result2+result3);
}while (result.next());
}
result.close();
}catch(Exception e){}
}
}
上面程序为什么不能显示ResultSet结果集的数据啊?但下面的就可以,各位帮忙看看啊
import java.sql.*;
import java.io.*;
public class test{
public static void main(String[] args) {
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
try{
String mySqlDriver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost/myproject";
Class.forName(mySqlDriver);
System.out.println("OK,DownLoad the driver");
Connection con=DriverManager.getConnection(url,"root","791346");
System.out.print("请输入id:");
String id=input.readLine();
System.out.println("GOOG,Connect the DataBase");
String sql="SELECT * FROM student where id=?";
PreparedStatement statement = con.prepareStatement(sql);
statement.setString(1, id);
ResultSet rs = statement.executeQuery();
if(!rs.next())
System.out.println("表名或列名输入有误或到了末尾");
else {
System.out.println("查询结果为:");
do
{
String result=rs.getString("id");
String result2=rs.getString("sno");
String result3=rs.getString("sname");
result=new String(result.getBytes("ISO-8859-1"),"GB2312");
result2=new String(result2.getBytes("ISO-8859-1"),"GB2312");
result3=new String(result3.getBytes("ISO-8859-1"),"GB2312");
System.out.println(result+result2+result3);
}while(rs.next());
}
rs.close();
con.close();
}catch(Exception ex){
System.out.println(ex);
System.exit(0);
}
}
}
------解决方案--------------------
因为上面你已经将Connection关闭,所以得不到结果集