当前位置: 技术问答>java相关
在JSP利用JAVABEAN完成数据库的调用以及查询时出现的问题?JAVA高手请进
来源: 互联网 发布时间:2015-05-26
本文导语: 如果在JSP中调用一般的JAVABEAN(没有数据库操作),就一切正常,但如果我在JAVABEAN中调用了数据库,运行就会出错: JAVABEAN为:DBconn 其代码很简单: import java.sql.*; public class DBconn { String DBDriver = "sun.jdbc.odb...
如果在JSP中调用一般的JAVABEAN(没有数据库操作),就一切正常,但如果我在JAVABEAN中调用了数据库,运行就会出错:
JAVABEAN为:DBconn 其代码很简单:
import java.sql.*;
public class DBconn {
String DBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String ConnStr = "jdbc:odbc:sample1";
Connection conn = null;
ResultSet rs = null;
public DBconn() {
try {
Class.forName(DBDriver);
}catch(java.lang.ClassNotFoundException e) {
System.err.println("DBconn (): " + e.getMessage());
}
}
public ResultSet getResultSet(String sql) {
rs = null;
try {
conn = DriverManager.getConnection(ConnStr,"sa","");
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}catch(SQLException ex) {
System.err.println("aq.executeQuery: " + ex.getMessage());
}
return rs;
}
JSP文件也很简单:
可是系统总出这样的错:
javax.servlet.ServletException
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:508)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:405)
at org.apache.tomcat.core.Handler.service(Handler.java:287)
at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:812)
at org.apache.tomcat.core.ContextManager.service(ContextManager.java:758)
at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:213)
at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416)
at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:501)
at java.lang.Thread.run(Thread.java:484)
但只要把ResultSet RS = DBconn1.executeQuery("SELECT * FROM employee"); 和
RS.close(); 注释了,系统就不出错了。
数据库的连接应该没有问题,因为不用JAVABEAN,用JSP直接连接数据库就是对的。
JAVABEAN为:DBconn 其代码很简单:
import java.sql.*;
public class DBconn {
String DBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String ConnStr = "jdbc:odbc:sample1";
Connection conn = null;
ResultSet rs = null;
public DBconn() {
try {
Class.forName(DBDriver);
}catch(java.lang.ClassNotFoundException e) {
System.err.println("DBconn (): " + e.getMessage());
}
}
public ResultSet getResultSet(String sql) {
rs = null;
try {
conn = DriverManager.getConnection(ConnStr,"sa","");
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}catch(SQLException ex) {
System.err.println("aq.executeQuery: " + ex.getMessage());
}
return rs;
}
JSP文件也很简单:
可是系统总出这样的错:
javax.servlet.ServletException
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:508)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:405)
at org.apache.tomcat.core.Handler.service(Handler.java:287)
at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:812)
at org.apache.tomcat.core.ContextManager.service(ContextManager.java:758)
at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:213)
at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416)
at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:501)
at java.lang.Thread.run(Thread.java:484)
但只要把ResultSet RS = DBconn1.executeQuery("SELECT * FROM employee"); 和
RS.close(); 注释了,系统就不出错了。
数据库的连接应该没有问题,因为不用JAVABEAN,用JSP直接连接数据库就是对的。
|
系统提示servlet出错,其实就是jsp出错,因为实际上jsp就是servlet,
肯定不是数据源或别的数据库问题,因为不是sqlexception,
你试试
肯定不是数据源或别的数据库问题,因为不是sqlexception,
你试试
|
在WEB-INFOclasses目录下建立classes目录
然后把你写的javabean 放到这个目录下。
试下一下代码。
package classes;
import java.sql.*;
import java.io.Serializable;
public class DBconn implements java.io.Serializable{
String DBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String ConnStr = "jdbc:odbc:sample1";
Connection conn = null;
ResultSet rs = null;
public DBconn() {
try {
Class.forName(DBDriver);
}catch(java.lang.ClassNotFoundException e) {
System.err.println("DBconn (): " + e.getMessage());
}
}
public ResultSet getResultSet(String sql) {
rs = null;
try {
conn = DriverManager.getConnection(ConnStr,"sa","");
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}catch(SQLException ex) {
System.err.println("aq.executeQuery: " + ex.getMessage());
}
return rs;
}
然后把你写的javabean 放到这个目录下。
试下一下代码。
package classes;
import java.sql.*;
import java.io.Serializable;
public class DBconn implements java.io.Serializable{
String DBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String ConnStr = "jdbc:odbc:sample1";
Connection conn = null;
ResultSet rs = null;
public DBconn() {
try {
Class.forName(DBDriver);
}catch(java.lang.ClassNotFoundException e) {
System.err.println("DBconn (): " + e.getMessage());
}
}
public ResultSet getResultSet(String sql) {
rs = null;
try {
conn = DriverManager.getConnection(ConnStr,"sa","");
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}catch(SQLException ex) {
System.err.println("aq.executeQuery: " + ex.getMessage());
}
return rs;
}
|
我觉得你是不是没有关闭连接啊!你只建立连接但是没关掉,所以就没有释放连接,造成线程不足!
|
把你的 javabean打包试试。
还有我记得javabean得实现java.io.Serializable接口
还有我记得javabean得实现java.io.Serializable接口