黑马程序员技术交流社区
标题:
数据源连接数据库连不上了
[打印本页]
作者:
黑马刘杰
时间:
2012-6-28 10:55
标题:
数据源连接数据库连不上了
这两天做个人网站,以前写Java程序用数据源连接数据库蛮好用的,但是现在在网站上不好用了,错误提示是“不能初始化连接的这个类”,是不是还得配置服务器什么的?下面是我用数据源连接mysql数据库的连接类代码,那位大侠可以帮助看一下?谢谢了!
private static DataSource myDataSource=null;
static
{
try
{
Properties prop=new Properties();
InputStream inStream=ConnectDB.class.getClassLoader().getResourceAsStream("dbconfig.properties");
prop.load(inStream);
myDataSource = BasicDataSourceFactory.createDataSource(prop);
} catch (Exception e)
{
// TODO Auto-generated catch block
throw new ExceptionInInitializerError();
}
}
public ConnectDB(){}
public static Connection getConnection() throws SQLException
{
return myDataSource.getConnection();
}
public static void free(ResultSet rs,PreparedStatement pst,Connection con) throws Exception
{
try
{
if(rs!=null)
{
rs.close();
}
} catch (SQLException e)
{
// TODO Auto-generated catch block
throw e;
}finally
{
try
{
if(pst!=null)
{
pst.close();
}
}catch(SQLException e)
{
throw e;
}finally
{
try
{
if(con!=null)
{
con.close();
}
}catch(SQLException e)
{
throw e;
}
}
}
}
复制代码
作者:
闾丘日月
时间:
2012-6-28 11:18
你贴出来的代码,free这个方法占了一大半,可能引起异常的原因比如
dbconfig.properties的内容,路径没有给出,createDataSource方法也没给出,让人家怎么帮你看,你给出的代码至少让别人在eclipse下面不产生编译错误吧?
作者:
黑马刘杰
时间:
2012-6-28 11:27
闾丘日月 发表于 2012-6-28 11:18
你贴出来的代码,free这个方法占了一大半,可能引起异常的原因比如
dbconfig.properties的内容,路径没有给 ...
哦,dbconfig.properties配置文件的内容如下
#连接设置
driverClassName=com.mysql.jdbc.Driver
url=jdbc\:mysql\://localhost\:3306/myDB
username=root
password=1001
#<!-- 初始化连接 -->
initialSize=10
#最大连接数量
maxActive=50
#<!-- 最大空闲连接 -->
maxIdle=20
#<!-- 最小空闲连接 -->
minIdle=5
#<!-- 超时等待时间以毫秒为单位60000毫秒/1000等于60秒 -->
maxWait=60000
复制代码
createDataSource方法是数据源里的,第三方连接工具包。
作者:
黑马刘杰
时间:
2012-6-28 11:31
我在发一下我的程序吧
package jike091.Jackey.connect;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;
public class ConnectDB
{
private static DataSource myDataSource=null;
static
{
try
{
Properties prop=new Properties();
InputStream inStream=ConnectDB.class.getClassLoader().getResourceAsStream("dbconfig.properties");
prop.load(inStream);
myDataSource = BasicDataSourceFactory.createDataSource(prop);
} catch (Exception e)
{
// TODO Auto-generated catch block
throw new ExceptionInInitializerError();
}
}
public ConnectDB(){}
public static Connection getConnection() throws SQLException
{
return myDataSource.getConnection();
}
public static void free(ResultSet rs,PreparedStatement pst,Connection con) throws Exception
{
try
{
if(rs!=null)
{
rs.close();
}
} catch (SQLException e)
{
// TODO Auto-generated catch block
throw e;
}finally
{
try
{
if(pst!=null)
{
pst.close();
}
}catch(SQLException e)
{
throw e;
}finally
{
try
{
if(con!=null)
{
con.close();
}
}catch(SQLException e)
{
throw e;
}
}
}
}
}
复制代码
我导入了三个包commons-dbcp-1.4.jar, commons-collections-3.2.1.jar, commons-pool-1.6.jar用以连接,这是我在李勇老师的JDBC视频上学到的,大家可以看一下。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2