A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 黑马刘杰 中级黑马   /  2012-6-28 10:55  /  2545 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

这两天做个人网站,以前写Java程序用数据源连接数据库蛮好用的,但是现在在网站上不好用了,错误提示是“不能初始化连接的这个类”,是不是还得配置服务器什么的?下面是我用数据源连接mysql数据库的连接类代码,那位大侠可以帮助看一下?谢谢了!
  1. private static DataSource myDataSource=null;
  2.             
  3.         static
  4.         {
  5.                 try
  6.                 {
  7.                         Properties prop=new Properties();
  8.                         InputStream inStream=ConnectDB.class.getClassLoader().getResourceAsStream("dbconfig.properties");
  9.                         prop.load(inStream);
  10.                         myDataSource = BasicDataSourceFactory.createDataSource(prop);
  11.                 } catch (Exception e)
  12.                 {
  13.                         // TODO Auto-generated catch block
  14.                         throw new ExceptionInInitializerError();
  15.                 }
  16.         }
  17.         public ConnectDB(){}
  18.        
  19.         public static Connection getConnection() throws SQLException
  20.         {
  21.                 return myDataSource.getConnection();
  22.         }
  23.        
  24.        
  25.        
  26.         public static void free(ResultSet rs,PreparedStatement pst,Connection con) throws Exception
  27.         {
  28.                 try
  29.                 {
  30.                         if(rs!=null)
  31.                         {
  32.                                 rs.close();
  33.                         }
  34.                 } catch (SQLException e)
  35.                 {
  36.                         // TODO Auto-generated catch block
  37.                         throw e;
  38.                 }finally
  39.                 {
  40.                         try
  41.                         {
  42.                                 if(pst!=null)
  43.                                 {
  44.                                         pst.close();
  45.                                 }
  46.                         }catch(SQLException e)
  47.                         {
  48.                                 throw e;
  49.                         }finally
  50.                         {
  51.                                 try
  52.                                 {
  53.                                         if(con!=null)
  54.                                         {
  55.                                                 con.close();
  56.                                         }
  57.                                 }catch(SQLException e)
  58.                                 {
  59.                                         throw e;
  60.                                 }
  61.                         }
  62.                 }
  63.         }
复制代码

3 个回复

倒序浏览
你贴出来的代码,free这个方法占了一大半,可能引起异常的原因比如
dbconfig.properties的内容,路径没有给出,createDataSource方法也没给出,让人家怎么帮你看,你给出的代码至少让别人在eclipse下面不产生编译错误吧?
回复 使用道具 举报
闾丘日月 发表于 2012-6-28 11:18
你贴出来的代码,free这个方法占了一大半,可能引起异常的原因比如
dbconfig.properties的内容,路径没有给 ...

哦,dbconfig.properties配置文件的内容如下
  1. #连接设置
  2. driverClassName=com.mysql.jdbc.Driver
  3. url=jdbc\:mysql\://localhost\:3306/myDB
  4. username=root
  5. password=1001

  6. #<!-- 初始化连接 -->
  7. initialSize=10

  8. #最大连接数量
  9. maxActive=50

  10. #<!-- 最大空闲连接 -->
  11. maxIdle=20

  12. #<!-- 最小空闲连接 -->
  13. minIdle=5

  14. #<!-- 超时等待时间以毫秒为单位60000毫秒/1000等于60秒 -->
  15. maxWait=60000
复制代码
createDataSource方法是数据源里的,第三方连接工具包。
回复 使用道具 举报
我在发一下我的程序吧
  1. package jike091.Jackey.connect;

  2. import java.io.InputStream;
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.util.Properties;

  8. import javax.sql.DataSource;
  9. import org.apache.commons.dbcp.BasicDataSourceFactory;

  10. public class ConnectDB
  11. {
  12.         private static DataSource myDataSource=null;
  13.             
  14.         static
  15.         {
  16.                 try
  17.                 {
  18.                         Properties prop=new Properties();
  19.                         InputStream inStream=ConnectDB.class.getClassLoader().getResourceAsStream("dbconfig.properties");
  20.                         prop.load(inStream);
  21.                         myDataSource = BasicDataSourceFactory.createDataSource(prop);
  22.                 } catch (Exception e)
  23.                 {
  24.                         // TODO Auto-generated catch block
  25.                         throw new ExceptionInInitializerError();
  26.                 }
  27.         }
  28.         public ConnectDB(){}
  29.        
  30.         public static Connection getConnection() throws SQLException
  31.         {
  32.                 return myDataSource.getConnection();
  33.         }
  34.        
  35.        
  36.        
  37.         public static void free(ResultSet rs,PreparedStatement pst,Connection con) throws Exception
  38.         {
  39.                 try
  40.                 {
  41.                         if(rs!=null)
  42.                         {
  43.                                 rs.close();
  44.                         }
  45.                 } catch (SQLException e)
  46.                 {
  47.                         // TODO Auto-generated catch block
  48.                         throw e;
  49.                 }finally
  50.                 {
  51.                         try
  52.                         {
  53.                                 if(pst!=null)
  54.                                 {
  55.                                         pst.close();
  56.                                 }
  57.                         }catch(SQLException e)
  58.                         {
  59.                                 throw e;
  60.                         }finally
  61.                         {
  62.                                 try
  63.                                 {
  64.                                         if(con!=null)
  65.                                         {
  66.                                                 con.close();
  67.                                         }
  68.                                 }catch(SQLException e)
  69.                                 {
  70.                                         throw e;
  71.                                 }
  72.                         }
  73.                 }
  74.         }
  75. }
复制代码
我导入了三个包commons-dbcp-1.4.jar, commons-collections-3.2.1.jar, commons-pool-1.6.jar用以连接,这是我在李勇老师的JDBC视频上学到的,大家可以看一下。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马