使用JNDI+连接池
生活随笔
收集整理的這篇文章主要介紹了
使用JNDI+连接池
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
配置context.xml,在META-INF下新建context.xml,內容如下
1 <?xml version="1.0" encoding="UTF-8"?> 2 <Context reloadable="true"> 3 <Resource name="jdbc/BookDB" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" 4 username="root" password="cwx2009" driverClassName="com.mysql.jdbc.Driver" 5 url="jdbc:mysql://127.0.0.1:3306/BookDB?autoReconnect=true"> 6 </Resource> 7 </Context>?
?
配置web.xml
1 <resource-ref> 2 <description>DB Connection</description> 3 <res-ref-name>jdbc/BookDB</res-ref-name> 4 <res-type>javax.sql.DataSource</res-type> 5 <res-auth>Container</res-auth> 6 </resource-ref>利用jsp頁面輸出數據庫查詢結果,查詢的主頁步驟如下
1 Connection conn; 2 Statement stmt; 3 ResultSet rs; 4 //從數據源中獲得數據庫連接 5 Context ctx = new InitialContext(); 6 DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/BookDB"); 7 conn = ds.getConnection(); 8 9 //創建一個SQL聲明 10 stmt = conn.createStatement(); 11 //查詢記錄 12 rs = stmt.executeQuery("select id,username,password from schema.admin"); 13 //輸出查詢結果省略具體的輸出和關閉流的操作
?
轉載于:https://www.cnblogs.com/zhangqiuchi/p/6790521.html
總結
以上是生活随笔為你收集整理的使用JNDI+连接池的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 20162316刘诚昊 第九周学习总结
- 下一篇: 使用reactjs做一个CRUD功能