标题:这个ResultSet得到为什么是空的
取消只看楼主
roy_guo
Rank: 1
等 级:新手上路
帖 子:107
专家分:0
注 册:2006-4-27
 问题点数:0 回复次数:1 
这个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 
2006-09-24 15:45
roy_guo
Rank: 1
等 级:新手上路
帖 子:107
专家分:0
注 册:2006-4-27
得分:0 

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


彪悍的人生不需要解释~~~
2006-09-24 16:13



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-92631-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.692459 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved