zoukankan      html  css  js  c++  java
  • tomcat mysql 数据源

    新文章移至 
    http://cffile.sinaapp.com/?p=30
     

    一、mysql

    1.拷相应的driver.jar到Tomcat5\common\lib下
    2.更改Tomcat5\conf下的context.xml
    <Context>节点下加
        <Resource name="jdbc/MysqlConnectionPoolTest" auth="Czh"
          type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver"
          url="jdbc:mysql://127.0.0.1:3306/test"
          username="root" password="000000" maxActive="20" maxIdle="10"
          maxWait="-1"/>
    3.更改工程下的web.xml
      <web-app>节点下加
        <resource-ref>
          <description>DB Connection</description>
          <res-ref-name>jdbc/MysqlConnectionPoolTest</res-ref-name>
          <res-type>javax.sql.DataSource</res-type>
          <res-auth>Czh</res-auth>
      </resource-ref>
    4.代码如下
    Context context = null;
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        
        public void DoQuery(String sql) {
            try {
                if(context==null)
                {
                    context = new InitialContext();
                }
                // get ds
                DataSource ds = (DataSource) context
                        .lookup("java:comp/env/jdbc/MysqlConnectionPoolTest");
                // get conn
                if(conn==null){
                    conn = ds.getConnection();
                }
                if(stmt==null){
                    stmt = conn.createStatement();
                }
                rs = stmt.executeQuery(sql);
                while (rs.next()) {
                    String a = rs.getString("a");
                    String b = rs.getString("b");
                }
            } catch (Exception e) {

                e.printStackTrace();
            }

        }
        
        注意有comp/env/    
    context
                        .lookup("java:comp/env/jdbc/MysqlConnectionPoolTest");
                    
  • 相关阅读:
    D365: 笔记(VS无法打开表浏览器)
    D365: 笔记(跟踪调试批处理代码)
    D365: 笔记(非批处理执行class中弹出交互式窗体或报表)
    D365: 笔记(现有量增加批号表字段查询条件)
    D365: 新功能(二)采购发票自动化流程
    D365: 新功能(一)按日期设置编号规则
    ES6 入门教程(未完)
    深入理解TypeScript(未完)
    项目
    vue + threejs 项目踩坑总结
  • 原文地址:https://www.cnblogs.com/chenzhihong/p/1560705.html
Copyright © 2011-2022 走看看