Tomcat 配置 JNDI数据源
???????????????? maxActive="100" maxIdle="30" maxWait="10000"
???????????????? username="root" password="" driverClassName="org.gjt.mm.mysql.Driver"
???????????????? url="jdbc:mysql://127.0.0.1:3306/test????????????????
??????????????? characterEncoding=GBK&useUnicode=TRUE"/>
??????????????? 注意的是:如果有&字符,需要转移成&(XML文件规范)
3,加上资源引用:Resource-Def,如下:
?????????????? <resource-ref>
????????????????? <description>DB Connection</description>
????????????????? <res-ref-name>jdbc/TestDB</res-ref-name>
???????????? ???? <res-type>javax.sql.DataSource</res-type>
???????????? ???? <res-auth>Container</res-auth>
????????????? </resource-ref>
使用数据源和数据库连接:
?
Context initCtx = new InitialContext(); javax.sql.DataSource ds=(javax.sql.DataSource)initCtx.lookup("java:comp/env/jdbc/TestDB"); Connection conn = ds.getConnection();?<Context debug="0" reloadable="false" crossContext="true" verbosity="debug">???
?<Resource name="jdbc/evaluate_dbcp_connect"???
?????? auth="Container"??
?????? type="javax.sql.DataSource"???
?????? driverClassName="com.mysql.jdbc.Driver"???
?????? url="jdbc:mysql://localhost:3306/test01?useUnicode=true&characterEncoding=UTF-8"???
?????? username="root"???
?????? password="123456"??
?????? testOnBorrow="true"
?????? testOnReturn="true"
?????? testWhileIdle="true"
?????? maxActive="100"???
?????? maxIdle="30"???
?????? maxWait="10000" >
?</Resource>
</Context>
?
?
?
Problem And Answer:
1。Question:如果出现下列提示:
????? javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:? java.naming.factory.initial
??????? at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:640),怎么办?
????? Answer:检查在Tomcat中conf/server.xml文件,检查<context>是否设置useNaming="false",如果是,去掉。
2。Question:如果无法找到数据库驱动程序怎么办?
????? Answer:检查数据库驱动JDBC程序是否放在了Tomcat\common\lib目录下面
?
?
?
?
?