最新更新 sitemap 网站制作设计本站搜索
网页设计
国外网站 韩国网站 个人主页 手提袋设计 CSS 网页特效 平面设计 网站设计 Flash CMS技巧 服装网站 php教程 photoshop 画册 服务器选用 数据库 Office
虚拟主机 域名注册 云主机 网页设计 客服QQ:8208442
当前位置:首页 > 编程开发 > jsp教程

jsp编程:Tomcat6 下 MySQL数据源配制

日期:10-15    来源:中国设计秀    作者:cnwebshow.com

数据源配制中有一些参数,不太明白,不明白的咱不说.在这说两种我用过的.82U中国设计秀

 首先在apache-tomcat-6.0.16lib  路径下的  MySQL JDBC 5.1  驱动不能少.82U中国设计秀

 第一种是在TomCat 里配制apache-tomcat-6.0.16confcontext.xml 如下:82U中国设计秀

Xml代码 82U中国设计秀
<?xml version='1.0' encoding='utf-8'?>  82U中国设计秀
<Context>  82U中国设计秀
    <Resource name="jdbc/myTest" auth="Container"  82U中国设计秀
        type="javax.sql.DataSource" username="root" password=""  82U中国设计秀
        driverClassName="com.mysql.jdbc.Driver"  82U中国设计秀
        url="jdbc:mysql://192.168.0.68:3306/points" />  82U中国设计秀
    <WatchedResource>WEB-INF/web.xml</WatchedResource>  82U中国设计秀
</Context> 82U中国设计秀

<?xml version='1.0' encoding='utf-8'?>82U中国设计秀
<Context>82U中国设计秀
 <Resource name="jdbc/myTest" auth="Container"82U中国设计秀
  type="javax.sql.DataSource" username="root" password=""82U中国设计秀
  driverClassName="com.mysql.jdbc.Driver"82U中国设计秀
  url="jdbc:mysql://192.168.0.68:3306/points" />82U中国设计秀
 <WatchedResource>WEB-INF/web.xml</WatchedResource>82U中国设计秀
</Context>82U中国设计秀
 name 是数据源名称.格式是:"jdbc/数据源名称"82U中国设计秀

username,password 不用说了.这里password 密码为空.82U中国设计秀

url 和 JDBC 驱动配相同.82U中国设计秀
http://bizhi.knowsky.com/82U中国设计秀
第二种是在项目里面配.WebRoot/META-INF/context.xml  中配:82U中国设计秀

Xml代码 82U中国设计秀
<?xml version="1.0" encoding="UTF-8"?>  82U中国设计秀
<Context>  82U中国设计秀
    <Resource name="jdbc/test" auth="Container"  82U中国设计秀
        type="javax.sql.DataSource" username="root" password=""  82U中国设计秀
        driverClassName="com.mysql.jdbc.Driver"  82U中国设计秀
        url="jdbc:mysql://192.168.0.68:3306/points" />  82U中国设计秀
</Context> 82U中国设计秀

<?xml version="1.0" encoding="UTF-8"?>82U中国设计秀
<Context>82U中国设计秀
 <Resource name="jdbc/test" auth="Container"82U中国设计秀
  type="javax.sql.DataSource" username="root" password=""82U中国设计秀
  driverClassName="com.mysql.jdbc.Driver"82U中国设计秀
  url="jdbc:mysql://192.168.0.68:3306/points" />82U中国设计秀
</Context>和在 Tomcat 里面几乎一样. context.xml  要注意是在 META-INF 下新建 是自己新建的.82U中国设计秀

 82U中国设计秀

 82U中国设计秀

数据源的使用:82U中国设计秀

Java代码 82U中国设计秀
list = new ArrayList();   82U中国设计秀
Connection conn = null;   82U中国设计秀
DataSource ds = null;   82U中国设计秀
PReparedStatement pst = null;   82U中国设计秀
ResultSet rst = null;   82U中国设计秀
String sqlStr = "insert into `user`(`userNo`,`userName`) values (?,?)";   82U中国设计秀
try {   82U中国设计秀
    InitialContext ctx = new InitialContext();   82U中国设计秀
    ds = (DataSource) ctx.lookup("java:comp/env/jdbc/test");   82U中国设计秀
    conn = ds.getConnection();   82U中国设计秀
    pst = conn.prepareStatement(sqlStr);   82U中国设计秀
    pst.clearBatch();   82U中国设计秀
    for (int i = 0; i < 4; i++) {   82U中国设计秀
        pst.setString(1, "good " + i);   82U中国设计秀
        pst.setString(2, "isw " + i);   82U中国设计秀
        // 使用batch,将多个sql操作作为一个单元传输给数据库,   82U中国设计秀
        // Hibernate 中 jdbc.batch_size 网上推荐 30   82U中国设计秀
        pst.addBatch();   82U中国设计秀
    }   82U中国设计秀
    pst.executeBatch();   82U中国设计秀
    rst = pst.executeQuery("select * from user");   82U中国设计秀
    // Fetch Size 是设定JDBC的prepareStatement读取数据的时候每次从数据库中取出的记录条数,   82U中国设计秀
    // Hibernate 中 jdbc.fetch_size 网上推荐 50   82U中国设计秀
    rst.setFetchSize(50);   82U中国设计秀
    while (rst.next()) {   82U中国设计秀
        System.out.println(rst.getString(3));   82U中国设计秀
        list.add(rst.getString(3));   82U中国设计秀
    }   82U中国设计秀
} catch (Exception e) {   82U中国设计秀
    e.printStackTrace();   82U中国设计秀
    return ERROR;   82U中国设计秀
} finally {   82U中国设计秀
    try {   82U中国设计秀
        rst.close();   82U中国设计秀
        pst.close();   82U中国设计秀
        conn.close();   82U中国设计秀
    } catch (SQLException e) {   82U中国设计秀
        e.printStackTrace();   82U中国设计秀
    }   82U中国设计秀
82U中国设计秀

list = new ArrayList();82U中国设计秀
Connection conn = null;82U中国设计秀
DataSource ds = null;82U中国设计秀
PreparedStatement pst = null;82U中国设计秀
ResultSet rst = null;82U中国设计秀
String sqlStr = "insert into `user`(`userNo`,`userName`) values (?,?)";82U中国设计秀
try {82U中国设计秀
 InitialContext ctx = new InitialContext();82U中国设计秀
 ds = (DataSource) ctx.lookup("java:comp/env/jdbc/test");82U中国设计秀
 conn = ds.getConnection();82U中国设计秀
 pst = conn.prepareStatement(sqlStr);82U中国设计秀
 pst.clearBatch();82U中国设计秀
 for (int i = 0; i < 4; i++) {82U中国设计秀
  pst.setString(1, "good " + i);82U中国设计秀
  pst.setString(2, "isw " + i);82U中国设计秀
  // 使用batch,将多个sql操作作为一个单元传输给数据库,82U中国设计秀
  // Hibernate 中 jdbc.batch_size 网上推荐 3082U中国设计秀
  pst.addBatch();82U中国设计秀
 }82U中国设计秀
 pst.executeBatch();82U中国设计秀
 rst = pst.executeQuery("select * from user");82U中国设计秀
 // Fetch Size 是设定JDBC的prepareStatement读取数据的时候每次从数据库中取出的记录条数,82U中国设计秀
 // Hibernate 中 jdbc.fetch_size 网上推荐 5082U中国设计秀
 rst.setFetchSize(50);82U中国设计秀
 while (rst.next()) {82U中国设计秀
  System.out.println(rst.getString(3));82U中国设计秀
  list.add(rst.getString(3));82U中国设计秀
 }82U中国设计秀
} catch (Exception e) {82U中国设计秀
 e.printStackTrace();82U中国设计秀
 return ERROR;82U中国设计秀
} finally {82U中国设计秀
 try {82U中国设计秀
  rst.close();82U中国设计秀
  pst.close();82U中国设计秀
  conn.close();82U中国设计秀
 } catch (SQLException e) {82U中国设计秀
  e.printStackTrace();82U中国设计秀
 }82U中国设计秀
}    上面说一下,大家都说Hibernate 执行数据库操作要比手动JDBC 效率要高,这是因为它对查询进行了一些优化,Fetch Size,Batch 也许只是冰山一角.说到这里就提了一下它们的用法.82U中国设计秀

 82U中国设计秀

关于 Fectch Size,Batch 下面的博客给出较详细的说明.82U中国设计秀

http://xuganggogo.javaeye.com/blog/44051682U中国设计秀

 82U中国设计秀

数据源在Hibernate 中的使用  hibernate.cfg.xml82U中国设计秀

Xml代码 82U中国设计秀
<?xml version='1.0' encoding='UTF-8'?>  82U中国设计秀
<!DOCTYPE hibernate-configuration PUBLIC   82U中国设计秀
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"   82U中国设计秀
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  82U中国设计秀
  82U中国设计秀
<!-- Generated by MyEclipse Hibernate Tools.                   -->  82U中国设计秀
<hibernate-configuration>  82U中国设计秀
    <session-factory>  82U中国设计秀
                <!-- 下面是数据源的配制  -->  82U中国设计秀
        <property name="connection.datasource">  82U中国设计秀
            java:comp/env/jdbc/test   82U中国设计秀
        </property>  82U中国设计秀
                <!-- 数据库方言 -->  82U中国设计秀
        <property name="dialect">  82U中国设计秀
            org.hibernate.dialect.MySQLDialect   82U中国设计秀
        </property>  82U中国设计秀
        <property name="jdbc.batch_size">25</property>  82U中国设计秀
        <property name="jdbc.fetch_size">50</property>  82U中国设计秀
        <mapping resource="com/isw2/entity/UserBean.hbm.xml" />  82U中国设计秀
    </session-factory>  82U中国设计秀
</hibernate-configuration> 82U中国设计秀

<?xml version='1.0' encoding='UTF-8'?>82U中国设计秀
<!DOCTYPE hibernate-configuration PUBLIC82U中国设计秀
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"82U中国设计秀
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">82U中国设计秀

<!-- Generated by MyEclipse Hibernate Tools.                   -->82U中国设计秀
<hibernate-configuration>82U中国设计秀
 <session-factory>82U中国设计秀
                <!-- 下面是数据源的配制  -->82U中国设计秀
  <property name="connection.datasource">82U中国设计秀
   java:comp/env/jdbc/test82U中国设计秀
  </property>82U中国设计秀
                <!-- 数据库方言 -->82U中国设计秀
  <property name="dialect">82U中国设计秀
   org.hibernate.dialect.MySQLDialect82U中国设计秀
  </property>82U中国设计秀
  <property name="jdbc.batch_size">25</property>82U中国设计秀
  <property name="jdbc.fetch_size">50</property>82U中国设计秀
  <mapping resource="com/isw2/entity/UserBean.hbm.xml" />82U中国设计秀
 </session-factory>82U中国设计秀
</hibernate-configuration>  82U中国设计秀
 82U中国设计秀

人生之所以的许许多多的半途而废及心有不甘,不过因为我们没有坚持自己的理想罢了,只要我们不放弃,理想终会实现.82U中国设计秀

本文引用地址:/bc/article_46563.html
网站地图 | 关于我们 | 联系我们 | 网站建设 | 广告服务 | 版权声明 | 免责声明