已经在Tomcat/lib 放入了数据库驱动(.jar)
配置Tomcat/conf context.xml 里的配置
代码如下
<Context>
<Resource name="jdbc/ownHome" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="sa" password="666666" driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver" url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=ownHome"/></Context>
DBConnection是这样写的:
public class DBConnection {
private Connection dbConn = null;
private String s_name=null,s_password=null;
private Statement stmt=null;
private ResultSet rs=null;
public ResultSet getRs(){return rs;}
public DBConnection(){
Context initContext;
try {
initContext = new InitialContext(); Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/ownHome"); dbConn = ds.getConnection();
Statement stmt = dbConn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY) ;
String sql="select * from USERS";
rs= stmt.executeQuery(sql);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public ResultSet getRs(){return rs;}
}
servlet是这样写的:
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
int id=0;
PrintWriter m_writer = resp.getWriter();
ResultSet rs = null;
DBConnection m_connection = new DBConnection();
rs = m_connection.getRs();
try {
while(rs.next()){
id=rs.getInt(1);}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
m_writer.println(id);
}
报错:
type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception java.lang.NullPointerException
servlet.testServlet.doGet(testServlet.java:42)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.20 logs.报的异常是在while(rs.next()这一句,也就是说rs是空的,请问为什么?
配置Tomcat/conf context.xml 里的配置
代码如下
<Context>
<Resource name="jdbc/ownHome" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="sa" password="666666" driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver" url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=ownHome"/></Context>
DBConnection是这样写的:
public class DBConnection {
private Connection dbConn = null;
private String s_name=null,s_password=null;
private Statement stmt=null;
private ResultSet rs=null;
public ResultSet getRs(){return rs;}
public DBConnection(){
Context initContext;
try {
initContext = new InitialContext(); Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/ownHome"); dbConn = ds.getConnection();
Statement stmt = dbConn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY) ;
String sql="select * from USERS";
rs= stmt.executeQuery(sql);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public ResultSet getRs(){return rs;}
}
servlet是这样写的:
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
int id=0;
PrintWriter m_writer = resp.getWriter();
ResultSet rs = null;
DBConnection m_connection = new DBConnection();
rs = m_connection.getRs();
try {
while(rs.next()){
id=rs.getInt(1);}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
m_writer.println(id);
}
报错:
type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception java.lang.NullPointerException
servlet.testServlet.doGet(testServlet.java:42)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.20 logs.报的异常是在while(rs.next()这一句,也就是说rs是空的,请问为什么?
在这里的时候是空的啊,那肯定是在DBConnection类的构造函数里面有问题。