关闭

连接数据库方法汇总

521人阅读 评论(0) 收藏 举报
---------------------------直连---------------------
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Connection connection=DriverManager.getConnection 
  ("jdbc:mysql://localhost:3306/test", "root", "880428");
return connection;
-------------------------tomcat5.0.28连接词写法----------------------
1.项目的配置文件,把他复制到jakarta-tomcat-5.0.28confCatalinalocalhost下
< xml version='1.0' encoding='utf-8' >
<Context docBase="D:MyProjectseclipseairbookingairweb" path="/airweb" workDir="workCatalinalocalhostairweb" reloadable="true">
<Resource name="jdbc/database" type="javax.sql.DataSource"/>
  <ResourceParams name="jdbc/database">
  <parameter>
  <name>factory</name>
  <value>
   org.apache.commons.dbcp.BasicDataSourceFactory
  </value>
 </parameter>
    <parameter>
      <name>maxWait</name>
      <value>1</value>
    </parameter>
    <parameter>
      <name>maxActive</name>
      <value>20</value>
    </parameter>
 <parameter>
      <name>username</name>
      <value>store</value>
    </parameter>
    <parameter>
      <name>password</name>
      <value>store</value>
    </parameter>
    <parameter>
      <name>url</name>
      <value>jdbc:oracle:thin:@localhost:1521:mydb</value>
    </parameter>
    <parameter>
      <name>driverClassName</name>
      <value>oracle.jdbc.OracleDriver</value>
    </parameter>
    <parameter>
      <name>maxIdle</name>
      <value>10</value>
    </parameter>  
  </ResourceParams>
</Context>
2.得到dataSource
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class SqlConnect
{
  private static DataSource datasource = null;
  static 
  {
   try
   {
     Context context = new InitialContext();
     datasource = (DataSource)context.lookup("java:comp/env/jdbc/database");
   }
   catch(Exception e)
   {
     e.printStackTrace();
   }
 }
 
 public static DataSource getDataSource()
 {
  return datasource;
 }
}
---------------------------tomcat6.0的数据库连接词写法----------------------
1、在Tomcat6的conf文件夹下,打开server.xml,在<GlobalNamingResources>标签下加入
<Environment
      name="simpleValue"
      type="java.lang.Integer"
      value="30"/>
    <Resource
      name="jdbc/oracle"
      type="javax.sql.DataSource"
      maxActive="20"
      maxIdle="10"
      username="scott"
      maxWait="5000"
      driverClassName="oracle.jdbc.driver.OracleDriver"
      password="tiger"
      url="jdbc:oracle:thin:@localhost:1521:mydb"/>
    <Resource
      name="jdbc/mysql"
      type="javax.sql.DataSource"
      maxActive="20"
      maxIdle="10"
      username="root"
      maxWait="5000"
      driverClassName="com.mysql.jdbc.Driver"
      password="yong1028"
      url="jdbc:mysql://localhost:3306/test"/>
2、在META-INF下新建一个文件,命名为context.xml,里面的内容如下
< xml version="1.0" encoding="UTF-8" >
<Context path="/stdemo">
  <ResourceLink global="jdbc/mysql" name="jdbc/mysql" type="javax.sql.DataSource"/>
  <ResourceLink global="jdbc/oracle" name="jdbc/oracle" type="javax.sql.DataSource"/>
</Context>
3、在web.xml中加入
<resource-ref>
    <res-ref-name>jdbc/datasource</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
<resource-ref>
    <res-ref-name>jdbc/datasource</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
放在<welcome-file-list>标签之后。

===========================测试===========================================
index.jsp
<%@page pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>Java Web应用教学演示</title>
    </head>
    <body>
        <h3 align="center"><a href="dbmysql.jsp">数据库连接测试(MySQL)-需更新web.xml中数据源的配置</a></h3><br>
        <h3 align="center"><a href="dboracle.jsp">数据库连接测试(Oracle)-需更新web.xml中数据源的配置</a></h3><br>
    </body>
</html>
dboracle.jsp
<%@page pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<sql:query var="result" dataSource="jdbc/oracle">
    select sysdate from dual
</sql:query>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>数据库测试</title>
    </head>
    <body>
        <table align="center" border="1">
            <tr align="center">
                <td align="center">系统日期</td>
            </tr>
            <c:forEach var="name" items="${result.rows}">
                <tr align="center">
                    <td align="center">${name.sysdate}</td>
                </tr>
            </c:forEach>
        </table>
    </body>
</html>
dbmysql.jsp
<%@page pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<sql:query var="result" dataSource="jdbc/mysql">
    select current_date() as current
</sql:query>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>数据库测试</title>
    </head>
    <body>
        <table align="center" border="1">
            <tr align="center">
                <td align="center">当前日期</td>
            </tr>
            <c:forEach var="name" items="${result.rows}">
                <tr align="center">
                    <td align="center">${name.current}</td>
                </tr>
            </c:forEach>
        </table>
    </body>
</html> 
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:84128次
    • 积分:1506
    • 等级:
    • 排名:千里之外
    • 原创:64篇
    • 转载:8篇
    • 译文:0篇
    • 评论:15条
    文章分类
    最新评论