以下的文章主要向大家描述的是tomcat MySQL数据源的实际操作流程以及在其实际操作中所要用到的代码的描述,如果你对实现tomcat MySQL数据源的实际操作感兴趣的话,以下的文章将会满足你这一兴趣。
1.拷相应的driver.jar到Tomcat5commonlib下
2.更改Tomcat5conf下的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.tomcat MySQL数据源代码如下 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");
上面说了这么多内容,是关于对tomcat MySQL数据源的介绍,不知道各位对MySQL的认识是不是更上一层楼了,时时关注ITJS,学习最新Mysql技术。