咱废话不多说,http://localhost:8080 , 开始...
这是全局配法
1、TomCat Administration
|_
Resources
|_
Data sources
|_
Data Source Actions (右上角处)
|_
Create New Data Source(下拉菜单中选择此项)
2、在新建数据源中,设置Vaule
Property Value
JNDI Name: jdbc/mysql(名字随便)
Data Source URL: jdbc:mysql://localhost:3306/<databasename>
JDBC Driver Class: org.gjt.mm.mysql.Driver
User Name: root(根据自己设置MySql的用户名来定)
Password: ××××××
。
。
。
。
剩下的视应用的情况来定,一般默认就可。
3、配置XML
来到TomCat的安装目录
再到
OK!重启tomcat,写个TestJsp.jsp
这是全局配法
1、TomCat Administration
|_
Resources
|_
Data sources
|_
Data Source Actions (右上角处)
|_
Create New Data Source(下拉菜单中选择此项)
2、在新建数据源中,设置Vaule
Property Value
JNDI Name: jdbc/mysql(名字随便)
Data Source URL: jdbc:mysql://localhost:3306/<databasename>
JDBC Driver Class: org.gjt.mm.mysql.Driver
User Name: root(根据自己设置MySql的用户名来定)
Password: ××××××
。
。
。
。
剩下的视应用的情况来定,一般默认就可。
3、配置XML
来到TomCat的安装目录
./conf/server.xml
<Context path="/DBTest" docBase="DBTest" |
再到
自己的WEB-INF/web.xml
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" |
OK!重启tomcat,写个TestJsp.jsp
<%@page contentType="text/html"%> <%@page pageEncoding="UTF-8"%> <%@ page import="java.sql.*"%> <%@ page import="javax.sql.*"%> <%@ page import="javax.naming.*"%> <%-- The taglib directive below imports the JSTL library. If you uncomment it, you must also add the JSTL library to the project. The Add Library... action on Libraries node in Projects view can be used to add the JSTL 1.1 library. --%> <%-- <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> --%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <h1>JSP Page</h1> <%-- This example uses JSTL, uncomment the taglib directive above. To test, display the page like this: index.jsp?sayHello=true&name=Murphy --%> <%-- <c:if test="${param.sayHello}"> <!-- Let's welcome the user ${param.name} --> Hello ${param.name}! </c:if> --%> <% out.println("开始>>>"); try{ InitialContext ct = new InitialContext(); DataSource ds = (DataSource)ct.lookup("java:comp/env/jdbc/mysql"); Connection conn = ds.getConnection(); Statement stm = conn.createStatement(); String sql = "select * from help_category"; ResultSet rs = stm.executeQuery(sql); while(rs.next()){ out.println("<br>"); for(int i=0;i<rs.getMetaData().getColumnCount();i++){ out.print(rs.getString(i+1)); } } out.print("经过'rs'循环"); }catch(Exception ex){ out.print("Error: "+ex.getMessage()); ex.printStackTrace(); } %> </body> </html> |