jsp标签连接数据库的问题
在jsp 中写个标签,这个标签GetVodLecture的主要功能是连接上数据库,然后将记录写出来,标签的具体内容如下 :
package com.tag;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.db.*;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.*;
public class GetVodLecture extends BodyTagSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private String sqlstr="select * from vod";
private DBConnectionManager connMgr;
private Connection connection = null;
private Statement s=null;
private ResultSet rs = null;
public void setSqlstr(String sqlstr){
this.sqlstr = sqlstr;
}
public String getSqlstr(){
return this.sqlstr;
}
public ResultSet GetResultSet(){
return rs;
}
public int doStartTag() throws JspException {
int ProcessBody = SKIP_BODY;
try {
pageContext.getOut().print("hello world");
pageContext.getOut().print(sqlstr);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
connMgr = DBConnectionManager.getInstance();
connection = connMgr.getConnection(DBConnectionManager.DB_CON);
if (connection == null) return ProcessBody;
try {
s = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = s.executeQuery(sqlstr);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ProcessBody;
}
public int doEndTag() throws JspException{
try {
if(rs == null) return EVAL_PAGE;
else {
while(rs.next())
pageContext.getOut().println(rs.getString("lecturename"));}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
connMgr.closeResultSet(rs);
connMgr.closeStatement(s);
connMgr.freeConnection(DBConnectionManager.DB_CON,connection);
return EVAL_PAGE;
}
public static void main(String[] args) {
GetVodLecture getvodlec = new GetVodLecture();
getvodlec.setSqlstr("select * from vod");
try {
System.out.println(getvodlec.doStartTag());
ResultSet rs1 =getvodlec.GetResultSet();
if(rs1 == null){
System.out.println("无记录");
}
try {
while(rs1.next())
System.out.println(rs1.getString("lecturename"));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (JspException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
运行标签这个类的main()函数是可以连接上数据库,将数据库中的记录写出来的。
但是将标签用于jsp页面中就总是连接不上数据库,但我测试这个标签在jsp页面中确实是运行的,只不过运行到连接数据库的代码的时候就出现问题,连不上数据库。
请问这是怎么回事?我都找了一个星期了,也没发现问题在哪?