struts 中连接数据库连接池

struts-config.xml添加

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
  <!-- ==================== form-beans Configuration ========================== -->
 <form-beans>

        <form-bean name="MovieForm" type="com.coraise.web.stb.MovieForm"/>

 </form-beans>
  <!-- ==================== action-mappings Configuration ========================== -->
 <action-mappings>

  <action path="/movie" type="com.coraise.web.stb.MovieAction" parameter="action" name="MovieForm" scope="request">
         <forward name="listMovieInfo" path="/stb/movie_list.jsp"/>
         <forward name="listMusicInfo" path="/stb/music_list.jsp"/>
         <forward name="listEnMovieInfo" path="/stb/en_movie_list.jsp"/>
         <forward name="listEnMusicInfo" path="/stb/en_music_list.jsp"/>
         <forward name="viewMediaInfo" path="/stb/mediaInfo.jsp"/>
         <forward name="viewEnMediaInfo" path="/stb/en_mediaInfo.jsp"/>
   
   <forward name="confirm2" path="/stb/confirm2.jsp"/>
   <forward name="en_confirm2" path="/stb/en_confirm2.jsp"/>
  </action>
  
 </action-mappings>
    <!-- ==================== Data Source Configuration ========================== -->
    <data-sources>
        <data-source key="DATA_SOURCE" type="org.apache.struts.util.GenericDataSource">
            <set-property property="user" value="root" />
            <set-property property="password" value="1234" />           
            <set-property property="maxCount" value="20" />
            <set-property property="minCount" value="2" />
            <set-property property="driverClass" value="com.mysql.jdbc.Driver" />
            <set-property property="description" value="" />
            <set-property property="url" value="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&amp;characterEncoding=GBK" />
            <set-property property="readOnly" value="false" />
            <set-property property="autoCommit" value="false" />
            <set-property property="loginTimeout" value="200" />
        </data-source>
    </data-sources>

</struts-config>

 

在你的Action类的excute方法中即可执行以下连接数据库代码

import javax.servlet.ServletContext;
import javax.sql.*;  

Connection conn = null;
   Statement stmt = null;
   ResultSet rs = null;
   String tmpName = "currentName";
   ServletContext context = this.servlet.getServletContext();
   DataSource dataSource = (DataSource)context.getAttribute("DATA_SOURCE");
 
   try {
    conn = dataSource.getConnection();
    stmt = conn.createStatement();
    rs = stmt.executeQuery("select * from user where id = 6");
    if( rs.next() ){
     tmpName = rs.getString("name");
     System.out.print(tmpName);
    }
   }catch (SQLException e) {
    System.err.println(e.getMessage());
   }
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值