本人在java SE开发的使用到JNDI进行开发,特此分享一下
1.配置数据源(找到tomcat安装目录->conf->context.xml)
<Resource
name="students"
type="javax.sql.DataSource" (固定)
auth="Container" (固定)
maxActive="100"
maxIdle="30"
maxWait="10000"
username="root"
password="123"
url="jdbc:mysql://localhost:3306/students"
driverClassName="com.mysql.jdbc.Driver"
/>
2.在项目中引用数据源(项目的web.xml)
<resource-ref>
<res-ref-name>students</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
3.获得连接
Context ct = new InitialContext();
DataSource ds= (DataSource) ct.lookup("java:comp/env/students");
Connection conn=ds.getConnection();