注册 登录
编程论坛 J2EE论坛

这个ResultSet得到为什么是空的

roy_guo 发布于 2006-09-24 15:45, 852 次点击

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());
}
}
}

7 回复
#2
无理取闹2006-09-24 15:53

没注释啊!!

#3
roy_guo2006-09-24 16:13

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

#4
z1089799792006-09-24 16:26
你先看看
con,stmt是不是空的
#5
可可℃乐2006-09-24 18:52

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

#6
yueritian2006-09-25 08:53

....真的好长..

先确定数据库有值没

#7
z1089799792006-09-25 09:31

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

#8
hardes2006-09-26 17:14

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

1