我有以下几个java程序:
以下是dbcpconfig.properties程序:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8
username=root
password=123
initialSize=10
maxIdle=20
minIdle=5
maxActive=50
maxWait=60000
cooectionProperties=useUnicode=true;characterEncoding=utf8
以下是jdbcutils.java程序:
package utils;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp2.BasicDataSourceFactory;
public class jdbcutils {
private static DataSource ds = null;
static
{
InputStream in = (InputStream) jdbcutils.class.getClassLoader().getResourceAsStream("dbcpconfig.properties");
Properties conf = new Properties();
try
{
conf.load(in);
BasicDataSourceFactory bdsf = new BasicDataSourceFactory();
ds = bdsf.createDataSource(conf);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static Connection getconn() throws SQLException
{
return ds.getConnection();
}
public static void del(Connection conn,Statement st,ResultSet rs)
{
if(rs!=null)
{
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(st!=null)
{
try {
st.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn!=null)
{
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
以下是customerdaoimpl.java程序(这只是我javaee项目其中一个程序):
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import utils.jdbcutils;
import bean.customer;
import bean.queryresult;
public class customerdaoimpl implements customerdao {
@Override
public void add(customer c) {
// TODO Auto-generated method stub
Connection conn=null;
PreparedStatement st=null;
ResultSet rs=null;
String sql;
try {
conn=jdbcutils.getconn();
sql="insert into customer(name,gender,age,cellphone,live,type,descr) values(?,?,?,?,?,?,?)";
//System.out.println(sql);
st=conn.prepareStatement(sql);
st.setString(1,c.getName());
st.setString(2, c.getGender());
st.setInt(3,c.getAge());
st.setString(4,c.getCellphone());
st.setString(5, c.getLive());
st.setString(6, c.getType());
st.setString(7, c.getDescr());
int i=st.executeUpdate();
if(i<1)
{
System.out.println("插入客户失败");
}
else
{
System.out.println("插入客户成功");
}
}
catch (Exception e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
finally
{
jdbcutils.del(conn, st, rs);
}
}
@Override
public void updata(customer c) {
// TODO Auto-generated method stub
Connection conn=null;
PreparedStatement st=null;
ResultSet rs=null;
String sql;
try {
conn=jdbcutils.getconn();
sql="update customer set name=?,gender=?,age=?,cellphone=?,live=?,type=?,descr=? where id=?";
st=conn.prepareStatement(sql);
st.setString(1,c.getName());
st.setString(2,c.getGender());
st.setInt(3,c.getAge());
st.setString(4,c.getCellphone());
st.setString(5,c.getLive());
st.setString(6,c.getType());
st.setString(7,c.getDescr());
st.setInt(8,c.getId());
int i=st.executeUpdate();
if(i<1)
{
System.out.println("修改客户失败");
}
else
{
System.out.println("修改客户成功");
}
}
catch (Exception e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
finally
{
jdbcutils.del(conn, st, rs);
}
}
@Override
public void del(int id) {
// TODO Auto-generated method stub
Connection conn=null;
PreparedStatement st=null;
ResultSet rs=null;
String sql;
try {
conn=jdbcutils.getconn();
conn=jdbcutils.getconn();
sql="delete from customer where id=?";
st=conn.prepareStatement(sql);
st.setInt(1, id);
int i=st.executeUpdate();
if(i<1)
{
System.out.println("删除客户失败");
}
else
{
System.out.println("删除客户成功");
}
}
catch (Exception e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
finally
{
jdbcutils.del(conn, st, rs);
}
}
@Override
public customer find(int id) {
// TODO Auto-generated method stub
Connection conn=null;
PreparedStatement st=null;
ResultSet rs=null;
String sql;
customer c = new customer();
try {
conn=jdbcutils.getconn();
sql="select * from customer where id=?";
st=conn.prepareStatement(sql);
st.setInt(1,id);
rs=st.executeQuery();
if(!rs.next())
{
throw new RuntimeException("没有"+id+"号客户");
}
else
{
c.setId(rs.getInt("id"));
c.setName(rs.getString("name"));
c.setGender(rs.getString("gender"));
c.setAge(rs.getInt("age"));
c.setCellphone(rs.getString("cellphone"));
c.setLive(rs.getString("live"));
c.setType(rs.getString("type"));
c.setDescr(rs.getString("descr"));
return c;
}
}
catch (Exception e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
finally
{
jdbcutils.del(conn, st, rs);
}
}
@Override
public List<customer> getall() {
// TODO Auto-generated method stub
Connection conn=null;
PreparedStatement st=null;
ResultSet rs=null;
String sql;
List<customer> lc = new ArrayList<customer>();
try {
conn=jdbcutils.getconn();
sql="select * from customer";
st=conn.prepareStatement(sql);
rs=st.executeQuery();
while(rs.next())
{
customer c = new customer();
c.setId(rs.getInt("id"));
c.setName(rs.getString("name"));
c.setGender(rs.getString("gender"));
c.setAge(rs.getInt("age"));
c.setCellphone(rs.getString("cellphone"));
c.setLive(rs.getString("live"));
c.setType(rs.getString("type"));
c.setDescr(rs.getString("descr"));
lc.add(c);
}
return lc;
}
catch (Exception e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
finally
{
jdbcutils.del(conn, st, rs);
}
}
public queryresult pagequery(int startindex,int pagesize)
{
Connection conn=null;
PreparedStatement st=null;
ResultSet rs=null;
String sql;
List<customer> lc = new ArrayList<customer>();
queryresult qr = new queryresult();
try {
conn=jdbcutils.getconn();
sql="select * from customer limit ?,?";
st=conn.prepareStatement(sql);
st.setInt(1, startindex);
st.setInt(2, pagesize);
rs=st.executeQuery();
while(rs.next())
{
customer c = new customer();
c.setId(rs.getInt("id"));
c.setName(rs.getString("name"));
c.setGender(rs.getString("gender"));
c.setAge(rs.getInt("age"));
c.setCellphone(rs.getString("cellphone"));
c.setLive(rs.getString("live"));
c.setType(rs.getString("type"));
c.setDescr(rs.getString("descr"));
lc.add(c);
}
qr.setList(lc);
sql="select count(*) from customer";
st=conn.prepareStatement(sql);
rs=st.executeQuery();
if(rs.next())
{
qr.setTotalrecord(rs.getInt(1));
}
return qr;
}
catch (Exception e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
finally
{
jdbcutils.del(conn, st, rs);
}
}
}
以下是test1.java程序:
package unit;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import utils.jdbcutils;
import dao.customerdao;
import dao.customerdaoimpl;
import bean.customer;
public class test1 extends common {
/**
* @param args
*/
public static void main(String[] args) {
Connection conn=null;
PreparedStatement st=null;
ResultSet rs=null;
try {
//System.out.println(jdbcutils.getconn());
conn = jdbcutils.getconn();
st=conn.prepareStatement("select * from customer");
rs=st.executeQuery();
while(rs.next())
{
System.out.println(rs.getString("name"));
}
//System.out.println(conn);
//System.out.println(st);
//System.out.println(rs);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
jdbcutils.del(conn, st, rs);
}
}
}
运行test1.java程序时,程序能够运行正常,把数据库的数据正确输出,没有问题,但在firefox浏览器运行javeee项目(即customerdaoimpl .java程序所在的javeee项目)时,就出现以下错误:
ava.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8'
at org.apache.commons.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2161)
at org.apache.commons.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:2033)
at org.apache.commons.dbcp2.BasicDataSource.getLogWriter(BasicDataSource.java:1588)
at org.apache.commons.dbcp2.BasicDataSourceFactory.createDataSource(BasicDataSourceFactory.java:595)
at utils.jdbcutils.<clinit>(jdbcutils.java:30)
at dao.customerdaoimpl.pagequery(customerdaoimpl.java:217)
at service.customerserimpl.pagequery(customerserimpl.java:50)
at ccon.getallservlet.doGet(getallservlet.java:29)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(Unknown Source)
at org.apache.commons.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2145)
... 24 more
java.lang.RuntimeException: java.lang.NullPointerException
at dao.customerdaoimpl.pagequery(customerdaoimpl.java:248)
at service.customerserimpl.pagequery(customerserimpl.java:50)
at ccon.getallservlet.doGet(getallservlet.java:29)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at utils.jdbcutils.getconn(jdbcutils.java:40)
at dao.customerdaoimpl.pagequery(customerdaoimpl.java:217)
... 19 more
为什么都是同一个jdbcutils.java在普通java程序就很正常,但在javaee项目中就运行出错了?那位高手能够解决掉这个问题? |
|