注意:在做之前,不管用什么数据库,一定要先将驱动包复制到Tomcat下common的lib中,不然会出现找不到数据库的驱动类错误 在WEB文件中增加: <resource-ref> <res-ref-name>jdbc/test</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> <res-sharing-scope>Shareable</res-sharing-scope> </resource-ref>
第一种方法: 在tomcat_home/webapps/myapps/META-INF/context.xml中增加: 没有这个文件,自己建一个,完整的文件:
<?xml version="1.0" encoding="UTF-8"?> <Context> <Resource name="jdbc/myderby" type="javax.sql.DataSource" password="tarena" driverClassName="org.apache.derby.jdbc.ClientDriver" maxIdle="2" maxWait="50" username="tarena" url="jdbc:derby://localhost:1527/tarena" maxActive="4"/> </Context>
name="jdbc/myderby" --jndi名称 driverClassName="org.apache.derby.jdbc.ClientDriver" –驱动类名 url="jdbc:derby://localhost:1527/tarena" 连接的URL字符串 username="tarena" 用户名 password="tarena" 密码 type="javax.sql.DataSource" 类型 maxIdle="2" maxWait="50" maxActive="4"
第二种方法: 直接在tomcat_home/conf/Catalina/localhost/下建立一个xml文件,文件名是:应用名.xml
<?xml version="1.0" encoding="UTF-8"?> <Context> <Resource name="jdbc/myderby" type="javax.sql.DataSource" password="tarena" driverClassName="org.apache.derby.jdbc.ClientDriver" maxIdle="2" maxWait="50" username="tarena" url="jdbc:derby://localhost:1527/tarena" maxActive="4"/> </Context>
第三种方法: (1)打开tomcat_home/conf/server.xml在<GlobalNamingResources>标记之间加入 <Resource name="jdbc/myderby" type="javax.sql.DataSource" password="tarena" driverClassName="org.apache.derby.jdbc.ClientDriver" maxIdle="2" maxWait="50" username="tarena" url="jdbc:derby://localhost:1527/tarena" maxActive="4"/>
(2)直接在tomcat_home/conf/Catalina/localhost/下建立一个xml文件,文件名是<应用名>.xml
<?xml version="1.0" encoding="UTF-8"?> <Context> <ResourceLink global="jdbc/test" name="jdbc/test" type="javax.sql.DataSource"/> </Context> |