【MySQL】JavaWeb项目中配置数据库的连接池
生活随笔
收集整理的這篇文章主要介紹了
【MySQL】JavaWeb项目中配置数据库的连接池
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在META-INF目錄下新建context.xml
<?xml version="1.0" encoding="UTF-8"?> <Context><Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"maxActive="10" maxIdle="4"username="root" password="" driverClassName="com.mysql.jdbc.Driver"url="jdbc:mysql://localhost:3306/庫名" /> </Context>在DAO中獲取連接實例
public static Connection getConnect(){Context initContext;Context envContext;Connection con = null;try {initContext = new InitialContext();envContext = (Context) initContext.lookup("java:/comp/env");DataSource ds = (DataSource) envContext.lookup("jdbc/PriceDB");con = ds.getConnection();System.out.println("數據庫連接成功");} catch (NamingException e) {// TODO Auto-generated catch block e.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch block e.printStackTrace();}return con;}?
轉載于:https://www.cnblogs.com/to-red/p/11191025.html
總結
以上是生活随笔為你收集整理的【MySQL】JavaWeb项目中配置数据库的连接池的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Clob类型转换为String
- 下一篇: c#中struct和class的区别