读取数据源配置,获取连接

1、配置数据源

D:\apache-tomcat-7.0.70\conf\context.xml

 <Resource name="jdbc/news" 
              auth="Container"  type="javax.sql.DataSource"  maxActive="100" 
              maxIdle="30" maxWait="10000" username="root"  password="123456" 
              driverClassName="com.mysql.jdbc.Driver" 
              url="jdbc:mysql://127.0.0.1:3306/news?useUnicode=true&amp;characterEncoding=GBK"/>
    

 

2、获取连接

// 获取数据库连接
        public Connection getConnection2() {
            try {
                //初始化上下文
                Context cxt=new InitialContext();
                //获取与逻辑名相关联的数据源对象
                DataSource ds=(DataSource)cxt.lookup("java:comp/env/jdbc/news");
                conn=ds.getConnection();
            } catch (NamingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return conn;
        }

 

相关文章:

  • 2022-12-23
  • 2022-02-13
  • 2021-10-04
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-06-30
  • 2022-12-23
  • 2021-05-01
  • 2022-12-23
  • 2021-05-07
  • 2022-01-19
  • 2022-01-30
相关资源
相似解决方案