驱动准备 你需要下载Java Database Connector for MySQL,本文提供一个来自于菜鸟教程的jar包,版本号为5.1.39。点击此处 下载。将此文件放入tomcat目录的lib文件夹中。
配置Tomcat的连接池 不配置连接池,在使用驱动包的时候tomcat会返回500 Internal Server Error,提示ClassNotFoundException(异常:找不到类),可以通过配置它的连接池来解决问题。
修改server.xml 修改位于tomcat目录下的conf/server.xml,找到<GlobalNamingResources>
标签,在此标签内我们需要添加内容。
<GlobalNamingResources > <Resource name ="UserDatabase" auth ="Container" type ="org.apache.catalina.UserDatabase" description ="User database that can be updated and saved" factory ="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname ="conf/tomcat-users.xml" /> <Resource name ="jdbc/mysqlds" auth ="Container" type ="javax.sql.DataSource" username ="$UNAME" password ="$PWD" maxIdle ="30" maxWait ="10000" maxActive ="100" driverClassName ="com.mysql.jdbc.Driver" url ="jdbc:mysql://localhost:3306/$DBNAME" /> </GlobalNamingResources >
其中 $DBNAME
位置放你的数据库名,$UNAME
位置放置你的数据库用户名,$PWD
位置放置你的数据库密码
修改context.xml 修改位于tomcat目录下的conf/context.xml,在<Context>
标签内我们需要添加内容。
<Context > <WatchedResource > WEB-INF/web.xml</WatchedResource > <WatchedResource > WEB-INF/tomcat-web.xml</WatchedResource > <WatchedResource > ${catalina.base}/conf/web.xml</WatchedResource > <ResourceLink name ="jdbc/mysqlds" global ="jdbc/mysqlds" type ="javax.sql.DataSource" /> </Context >
修改web.xml 修改位于tomcat目录下的网页项目文件夹下的WEB-INF/web.xml文件,在
<web-app>
内我们需要添加内容。
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns ="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version ="4.0" > <resource-ref > <description > mysql数据库连接池</description > <res-ref-name > jdbc/mysqlds</res-ref-name > <res-type > javax.sql.DataSource</res-type > <res-auth > Container</res-auth > <res-sharing-scope > Shareable</res-sharing-scope > </resource-ref > </web-app >
配置之后,需重启tomcat 。之后连接数据库的时候就不会出现ClassNotFoundException了。
JSP连接数据库 给出一段DEMO:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <%@ page import ="java.sql.*" %> <html> <head> <title>通过JSP打开数据表</title> </head> <body> <% Class.forName("com.mysql.jdbc.Driver" ); String url = "jdbc:mysql://localhost:3306/$DBNAME" ; String username = "$UNAME" ; String password = "$PWD" ; Connection conn = DriverManager.getConnection(url, username, password); if (conn != null ){ out.print("数据库连接成功!" ); out.print("<br />" ); Statement stmt = null ; ResultSet rs = null ; String sql = "SELECT * FROM $TABLENAME;" ; stmt = conn.createStatement(); rs = stmt.executeQuery(sql); out.print("查询结果:" ); out.print("<br />" ); out.println("id" +" " +"uname" +" " +"pwd" ); out.print("<br />" ); while (rs.next()) { out.println(rs.getString("id" )+" " +rs.getString("uname" )+" " +rs.getString("passwd" )+"<br />" ); } } else { out.print("连接失败!" ); } %> </body> </html>
其中 $DBNAME
位置放你的数据库名,$UNAME
位置放置你的数据库用户名,$PWD
位置放置你的数据库密码,$TABLENAME
位置放置你操作的数据表表名。
ResultSet.getString()函数接受字段名作为参数返回对应一条记录的该字段的值,可以以此读取信息。
上方Demo实现了读取一个数据表中id、uname、pwd字段的所有数据的JSP代码。
通过更改SQL语句可以实现更多的操作,此处不再赘述。