数据源与连接池

数据源和连接池(以MySql为例)
1.创建数据库
create database dbtest;
use dbtest;
create table student
(
 studentid char(20) not null,
 name char(30) not null,
 address varchar(200),
 constraint primary key(studentid)
);
insert into student(studentid,name,address)values('10','张三','北京');
insert into student(studentid,name,address)values('11','李四','河北');
2.tomcat中配置server.xml
在最后一个</host>前添加以下代码
<Context path="/test1" docBase="D:/java projects/test1/WebRoot"
        debug="5" reloadable="true" crossContext="true">
  <Resource name="jdbc/dbtest" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="root" password="root" driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://localhost/dbtest"/>

</Context>
3.配置webapp中web.xml
<resource-ref>
 <description>DataSource</description>
 <res-ref-name>jdbc/dbtest</res-ref-name>
 <res-type>javax.sql.DataSource</res-type>
 <res-auth>Container</res-auth>
</resource-ref>
4.测试程序
新建工程:test1
创建2个.java文件
/DBStudent .java
package db;
import java.io.*;
import java.util.*;
import java.sql.*;
public class DBStudent {
 private String studentid;
 private String name;
 private String address;
 public static Vector getAllStudent()
 {
  Vector stus=new Vector();
  Connection conn=null;
  Statement stmt=null;
  try{
   conn=DbTestDS.getConnection();
   if(conn!=null)
   {
    stmt=conn.createStatement();
    ResultSet rs=stmt.executeQuery("select * from student");
    while(rs.next())
    {
     String studentid1=rs.getString(1);

     String name1=new String(rs.getString(2));
     String address1=new String(rs.getString(3));
     DBStudent DBstu=new DBStudent();
     DBstu.setStudentid(studentid1);
     DBstu.setName(name1);
     DBstu.setAddress(address1);
     stus.add(DBstu);
    }
    rs.close();
    stmt.close();
   }
   
  }catch(SQLException e){
   e.printStackTrace();
  }
  return stus;
 }
 public void setStudentid(String studentid)
 {
  this.studentid=studentid;
 }
 public void setName(String name)
 {
  this.name=name;
 }
 public void setAddress(String address)
 {
  this.address=address;
 }
 public String getStudentid()
 {
  return studentid;
 }
 public String getName()
 {
  return name;
 }
 public String getAddress()
 {
  return address;
 }
}
//DbTestDS .java
package db;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
public class DbTestDS {
 private static DataSource ds=null;
 static
 {
  init();
 }
 private static void init()
 {
  try{
   Context ctx=new InitialContext();
   if(ctx==null)
   {
    throw new Exception("No Context");
   }
   Context envContext=(Context)ctx.lookup("java:comp/env");
   ds=(DataSource)envContext.lookup("jdbc/dbtest");
  }catch(Exception e)
  {
   e.printStackTrace();
  }
  
 }
 private DbTestDS(){}
 public static Connection getConnection()throws SQLException{
  if(ds==null)
  {
   throw new SQLException("找不到数据源");
  }
  else
  {
   return ds.getConnection();
  }
 }
//index.jsp///
<%@ page language="java" import="java.util.*,java.sql.*,db.*" pageEncoding="GB2312"%>
<%@ page contentType="text/html;charset=gb2312" errorPage=""%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=GB2312">
<title>使用配置数据源</title>
</head>
<body>
<% Vector stus=DBStudent.getAllStudent();
 int num=stus.size();
%>

<h2>信息表:</h2>
<table border="2">
<tr bgcolor="99FFCC">
<th width="50" align="center"  class="ProductList">学号</th>
<th width="100" align="center" class="ProductList">姓名</th>
<th width="200" align="center" class="ProductList">住址</th>
</tr>
<%for(int i=0;i<num;i++){
 DBStudent stu=(DBStudent)stus.get(i);
%>
<tr bgcolor="FFFF00">
<td width="50" align="center" class="ProductList"><%=stu.getStudentid()%></td>
<td width="100" align="center" class="ProductList"><%=stu.getName()%></td>
<td width="100" align="center" class="ProductList"><%=stu.getAddress()%></td>
</tr>
<%} %>
</table>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值