当前位置: 代码迷 >> Java Web开发 >> 这个ResultSet得到为什么是空的
  详细解决方案

这个ResultSet得到为什么是空的

热度:341   发布时间:2006-09-24 15:45:45.0
这个ResultSet得到为什么是空的

import java.sql.*;

public class Conn
{
private static Connection con;
private Statement stmt;
private ResultSet rs;
private static final String drivername = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
private static final String url = "jdbc:microsoft:sqlserver://guoxuefeng:1433;DatabaseName=project1";
private static final String username = "sa";
private static final String userpass = "guoxuefeng";

// get the con of the database
private static synchronized Connection getCon() throws Exception
{
try
{
Class.forName(drivername);
con = DriverManager.getConnection(url,username,userpass);
return con;
}
catch(SQLException e)
{
throw e;
}
}

public Statement getStemtread()
{
try
{
con = getCon();
stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
return stmt;
}
catch(Exception e)
{
//throw e;
}
return null;
}

public ResultSet getRs(String sql)
{
try
{
stmt = getStemtread();
rs = stmt.executeQuery(sql);
return rs;
}
catch(Exception e)
{
//throw e;
}
return null;
}

public Statement getStmt()
{
try
{
con = getCon();
stmt = con.createStatement();
return stmt;
}
catch(Exception e)
{
//throw e;
}
return null;
}

public synchronized void close()
{
try
{
if(rs!=null)
{
rs.close();
rs=null;
}
}
catch(Exception e)
{
//throw e;
}
try
{
if(stmt!=null)
{
stmt.close();
stmt = null;
}
}
catch(Exception e)
{
//throw e;
}
try
{
if(con!=null)
{
con.close();
con = null;
}
}
catch(Exception e)
{
//throw e;
}
}

public static void main(String[] args)
{
String t = "select * from NewsHead";
Conn con = new Conn();

try
{
ResultSet rs = con.getRs(t);
if(rs == null)
{
System.out.println("dadas");
return;
}
while(rs.next())
{
System.out.println(rs.getString(2));
}
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
}
}

搜索更多相关主题的帖子: ResultSet  

----------------解决方案--------------------------------------------------------

没注释啊!!


----------------解决方案--------------------------------------------------------

不好意思,这个就是自己写的一个连接数据库的类,main是用来测试的,但是读不出值来


----------------解决方案--------------------------------------------------------
你先看看
con,stmt是不是空的
----------------解决方案--------------------------------------------------------

为什么一个连接数据库的类要写那么长
看着多难受啊~


----------------解决方案--------------------------------------------------------

....真的好长..

先确定数据库有值没


----------------解决方案--------------------------------------------------------

guoxuefeng是不是你计算机名
3个驱动有没有
我运行你这段代码rs不是空的


----------------解决方案--------------------------------------------------------

楼主对关键字好象有特别的偏爱哦!呵呵
连接数据库做个测试我看这try块就写了一半了!这简直跟搞体力活没啥区别嘛!干嘛呢这是...


----------------解决方案--------------------------------------------------------
  相关解决方案