- 论坛徽章:
- 0
|
最近学习jsp,现将心得给大家一起分享,水平有限,高手勿见笑.
如题,开始吧
第一步: 创建数据库并在tomcat下配置链接池
1.创建数据库
在mysql下创建名为testdb的数据库,在testdb下创建数据表test_table.字段id,name
2.tomcat配置
我们在C:\Program Files\Tomcat 5.5.7\conf\content.xml文件中添加如下数据库链接池配置,如果没有就新建一个content.xml,注意tomcat版本要5.5以上,5.5以下不支持content.xml。
- <Context>
- <Resource name="jdbc/testdb"
- auth="Container"
- type="javax.sql.DataSource"
- driverClassName="com.mysql.jdbc.Driver"
- url="jdbc:mysql://localhost/mytestdb"
- username="root"
- password="******"
- maxActive="100"
- maxIdle="30"
- maxWait="10000"
- ></Resource>
- </Context>
复制代码
第二步:编写javaBean读取tomcat下的content.xml
javaBean 文件DataBase.java代码
第三步: 编写test.jsp测试
- <%@ page language="java" contentType="text/html; charset=gbk"
- pageEncoding="gbk"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
- "http://www.w3.org/TR/html4/loose.dtd">
- <%@page import="withouttears.bean.Database"%>
- <%@page import="java.sql.*"%>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=gbk">
- <title>Insert title here</title>
- </head>
- <body>
- <%
- Database db=new Database();
- db.setjndiName("jdbc/testdb");//初始化JNDI名称
- ResultSet rs=db.executeQuery("select * from test_table");
- while(rs.next()){
- out.println("id:"+rs.getInt("id")+"<br>");
- }
- rs.close();
- %>
- </body>
- </html>
复制代码
好了,打开浏览器输入:http://localhost:8080/test.jsp
[ 本帖最后由 wiThouTTears 于 2006-12-19 15:56 编辑 ] |
|