jdbc通过JNDI获取tomcat6连接池的连接
程序员文章站
2022-05-30 13:22:08
...
[color=darkblue][size=large]1.tomcat的server.xml中配置连接池[/size][/color]
[color=darkblue][size=large]2.将jdbc.driver的jar包拷到tomcat/lib文件夹下[/size][/color]
[color=darkblue][size=large]3.获取数据库连接[/size][/color]
<Context docBase="HelloWeb" path="/HelloWeb" reloadable="true" source="org.eclipse.jst.jee.server:HelloWeb">
<Resource name="jdbc/msg"
auth="Container"
type="javax.sql.DataSource"
username="root"
password="root"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/test"
maxActive="8"
maxIdle="4"/>
</Context>
[color=darkblue][size=large]2.将jdbc.driver的jar包拷到tomcat/lib文件夹下[/size][/color]
[color=darkblue][size=large]3.获取数据库连接[/size][/color]
Connection conn = null;
try {
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource)
envCtx.lookup("jdbc/test");
conn = ds.getConnection();
//conn.close();
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
}
上一篇: MySQL6——外键约束
下一篇: Mysql8连接信息变动