javabean连接sqlserver2000 myeclipse下编程!!!
我的这个javabean是在其他程序里写的
那个程序可以很好的连接数据库
但是copy到这个程序里后就不能连接数据库了 搞不懂啊
什么都没变 。。。。。求教!!!
下面是我的javabean代码:
package DBC;
import java.sql.*;
public class DBBean {
private Connection conn=null;
private Statement st=null;
private PreparedStatement pst=null;
private ResultSet rt=null;
//获得连接
public Connection getConn() throws
ClassNotFoundException,
SQLException{
String url="com.microsoft.sqlserver.jdbc.SQLServerDriver";
//加载驱动
Class.forName(url);
//获得连接
String drl="jdbc:sqlserver://localhost;databaseName=imgs;user=sa;password=123";
conn=DriverManager.getConnection(drl);
//返回连接
return conn;
}
//执行查询语句并返回结果
//滚动记录集
public ResultSet getResult(String sql) throws Class
NotFoundException, SQLException{
conn=new DBBean().getConn();
st=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rt=st.executeQuery(sql);
return rt;
}
//只进记录集
public ResultSet getResult2(String sql) throws ClassNotFoundException, SQLException{
conn=new DBBean().getConn();
st=conn.createStatement();
rt=st.executeQuery(sql);
return rt;
}
//更新数据库
public void update(String sql) throws SQLException, ClassNotFoundException{
conn=getConn();
st=conn.createStatement();
st.executeUpdate(sql);
}
public void update(String sql,String[]args) throws SQLException, ClassNotFoundException{
conn=getConn();
pst=conn.prepareStatement(sql);
for(int i=0;i<args.length;i++){
pst.setString(i+1,args[i]);
}
pst.executeUpdate();
}
//关闭数据库连接
public void close(){
try {
if(rt!=null) rt.close();
if(st!=null) st.close();
if(pst!=null) pst.close();
if(conn!=null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
这里是我的调用javabean的代码:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.io.InputStream"%>
<jsp:useBean id="db" class="DBC.DBBean"></jsp:useBean>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<%
String sql="select pic from img2 where id=4";
ResultSet rs=db.getResult(sql);
while(rs.next()){
response.setContentType("image/jpeg");
ServletOutputStream sout=response.getOutputStream();
InputStream in=rs.getBinaryStream(1);
byte b[]=new byte[0x7a120];
for(int