struts两种数据库连接

1:我看精通struts   邮电出版社的那本书的时候,讲的是使用struts-config.xml中的datasource配置数据源。
然后在每个action都写一个获得数据源   DataSource   dataSource=(DataSource)this.getDataSource(request, "datasource ");然后在写数据库的读取或者增加或者删除操作。最后相应的每个数据库资源的释放。代码贴出:
stuts-config.xml:
<struts-config>
    <data-sources>
    <data-source   key= "datasource "   type= "org.apache.commons.dbcp.BasicDataSource ">
    <set-property   property= "driverClassName "
      value= "com.microsoft.jdbc.sqlserver.SQLServerDriver "   />
    <set-property   property= "url "  
      value= "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=shopping "/>
    <set-property   property= "maxActive "  
      value= "5 "   />
    <set-property   property= "username "  
      value= "sa "   />
    <set-property   property= "password "
      value= " "   />
    <set-property   property= "autoCommit "  
      value= "true "   />
    </data-source>    
</data-sources>
.....
</struts-config>
相关的action类:
/*
*   Generated   by   MyEclipse   Struts
*   Template   path:   templates/java/JavaClass.vtl
*/
package   com.wnkj.struts.action.corporation;

import   java.sql.Connection;
import   java.sql.ResultSet;
import   java.sql.Statement;

import   javax.servlet.http.HttpServletRequest;
import   javax.servlet.http.HttpServletResponse;
import   javax.servlet.http.HttpSession;
import   javax.sql.DataSource;

import   org.apache.struts.action.Action;
import   org.apache.struts.action.ActionForm;
import   org.apache.struts.action.ActionForward;
import   org.apache.struts.action.ActionMapping;
import   com.wnkj.struts.form.NewloginForm;

/**  
*   MyEclipse   Struts
*   Creation   date:   07-07-2007
*  
*   XDoclet   definition:
*   @struts.action   path= "/newlogin "   name= "newloginForm "   input= "/form/newlogin.jsp "   scope= "request "   validate= "true "
*   @struts.action-forward   name= "success "   path= "/struts/ok1.jsp "
*/
public   class   NewloginAction   extends   Action   {
        String   howemail= "@ny114.cn ";
        Connection   con=null;
        Statement   stat=null;
        public   ActionForward   execute(ActionMapping   mapping,   ActionForm   form,
                        HttpServletRequest   request,   HttpServletResponse   response)   {
                NewloginForm   newloginForm   =   (NewloginForm)   form;//   TODO   Auto-generated   method   stub
                String   username=newloginForm.getUsername();
                String   psd=newloginForm.getPassword1();
                String   email=newloginForm.getEmail();
                String   standemail=username+howemail;
                try{
                        DataSource   dataSource=(DataSource)this.getDataSource(request, "datasource ");
                        con=dataSource.getConnection();
                        stat=con.createStatement();
                        String   sql= "insert   into   t_user(f_nickName,f_password,f_email,f_regtime,f_standemail)   values   ( ' "+username+ " ', ' "+psd+ " ', ' "+email+ " ',getdate(), ' "+standemail+ " ') ";
                        int   i=stat.executeUpdate(sql);
                        if(i> 0){
                                request.setAttribute( "insert ",   sql);
                                HttpSession   session=request.getSession();
                                session.setAttribute( "username ",   username);
                               
                        }
                        else{
                                request.setAttribute( "error ",   sql);
                        }
                }
                catch(Exception   ex){
                        ex.printStackTrace();
                }
                finally{
                        if(con!=null){
                                try{
                                        con.close();
                                }
                                catch(Exception   ex){
                                        ex.printStackTrace();
                                }
                        }
                        close();
                }
                return   mapping.findForward( "success ");
        }
       
        public   void   close(){
                try{
                        if(stat!=null){
                                try{
                                        stat.close();
                                }
                                catch(Exception   ex){
                                        ex.printStackTrace();
                                }
                        }
                }
                catch(Exception   ex){
                        ex.printStackTrace();
                }
               
        }
}
以上是第一种方法,下面是第二种方法。


见下帖:

 
 
88 
id="Topic_Zone" style="HEIGHT: 4px" marginwidth="0" marginheight="0" src="/u/AD/Topic_Zone.aspx" frameborder="0" width="100%" scrolling="no" height="0">
发表于:2007-08-06 09:34:331楼 得分:0
下贴呢?
怎么没了
 
发表于:2007-08-06 09:35:172楼 得分:0
2:我看别人写的还有一种方法:(代码贴出)
package   com.wnkj.dbutil;

import   java.util.*;
import   java.sql.*;

import   com.wnkj.propertites.*;
import   com.wnkj.pubclass.*;

public   class   Connect   extends   WriteLog
{
        public   Connection   con=null;
        public   PreparedStatement   pst=null;
        public   ResultSet   rs=null;
       
        private   ArrayList   celList=null;
        private   ArrayList   rowList=null;
        private   int   count=0;
        public   Connect()
        {
                try
                {
                    String   strCon= "com.microsoft.jdbc.sqlserver.SQLServerDriver ";
                    String   strUrl= "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=NPK2007 ";
                    /*
                    GetConnect.GetConnection();
                    Class.forName(GetConnect.ClassConnection);
                    con=java.sql.DriverManager.getConnection(GetConnect.ConnectionUrl,GetConnect.uid,GetConnect.pwd);
                    */
                    Class.forName(strCon);
                    con=java.sql.DriverManager.getConnection(strUrl, "npk2007 ", "npk2007 ");
                }catch(Exception   ex)
                {
                    ex.printStackTrace();
                    Write(ex,this.toString());
                }
        }
        /**
          *   查询信息
          */
        public   ArrayList   select()
        {
                try
                {
                        rowList=new   ArrayList();
                        rs=pst.executeQuery();
                        while(rs.next())
                        {
                                celList=new   ArrayList();
                                for(int   i=1;i <=rs.getMetaData().getColumnCount();i++)
                                {
                                        celList.add(rs.getString(i)+ " ");
                                }
                                rowList.add(celList);
                        }

                }catch(Exception   ex)
                {
                        ex.printStackTrace();
                        Write(ex,   this.toString());
                }   finally   {
                        try   {
                                if   (rs   !=   null)
                                        rs.close();
                                if   (pst   !=   null)
                                        pst.close();
                                close();
                        }   catch   (SQLException   e)   {
                                //   TODO   Auto-generated   catch   block
                                e.printStackTrace();
                                Write(e,   this.toString());
                        }
                }
                return   rowList;
        }
        /**
          *   查询信息记录数
          *   @return
          */
        public   int   selectCount()
        {
                try
                {
                        rowList=new   ArrayList();
                        rs=pst.executeQuery();
                        if(rs.next())
                        {
                                count=rs.getInt(1);
                        }
                }catch(Exception   ex)
                {
                        ex.printStackTrace();
                        Write(ex,   this.toString());
                }   finally   {
                        try   {
                                if   (rs   !=   null)
                                        rs.close();
                                if   (pst   !=   null)
                                        pst.close();
                                close();
                        }   catch   (SQLException   e)   {
                                //   TODO   Auto-generated   catch   block
                                e.printStackTrace();
                                Write(e,   this.toString());
                        }
                }
                return   count;
        }
        /**
          *   添加一条新的信息
          *   @return
          */
        public   int   insert()
        {
                try
                {
                        count=this.pst.executeUpdate();
                }catch(Exception   ex)
                {
                        ex.printStackTrace();
                        Write(ex,this.toString());
                }   finally   {
                        try   {
                                if   (pst   !=   null)
                                        pst.close();
                                close();
                        }   catch   (SQLException   e)   {
                                //   TODO   Auto-generated   catch   block
                                e.printStackTrace();
                                Write(e,   this.toString());
                        }
                }
                return   count;
        }
        /**
          *   更新表中的数据
          *   @return
          */
        public   int   update()
        {
                try
                {
                        count=this.pst.executeUpdate();
                }catch(Exception   ex)
                {
                        ex.printStackTrace();
                        Write(ex,this.toString());
                }   finally   {
                        try   {
                                if   (pst   !=   null)
                                        pst.close();
                                close();
                        }   catch   (SQLException   e)   {
                                //   TODO   Auto-generated   catch   block
                                e.printStackTrace();
                                Write(e,   this.toString());
                        }
                }
                return   count;
        }
        /**
          *   删除表中的数据
          *   @return
          */
        public   int   delete()
        {
                try
                {
                        count=this.pst.executeUpdate();
                }catch(Exception   ex)
                {
                        ex.printStackTrace();
                        Write(ex,this.toString());
                }   finally   {
                        try   {
                                if   (pst   !=   null)
                                        pst.close();
                                close();
                        }   catch   (SQLException   e)   {
                                //   TODO   Auto-generated   catch   block
                                e.printStackTrace();
                                Write(e,   this.toString());
                        }
                }
                return   count;
        }
        /**
          *   关闭数据库连接
          *
          */
        public   void   close()
        {
              try
              {
                  if(this.con!=null)
                  {
                          con.close();
                  }
              }catch(Exception   ex)
              {
                    ex.printStackTrace();
                    Write(ex,this.toString());
              }
        }
}

未完,下帖:相应的action类
 
发表于:2007-08-06 09:36:013楼 得分:0
可以看出,这个属于数据库相关操作的类。另外相应的具体操作数据库还有一个类,贴了:

package   com.wnkj.dbutil;

import   java.util.*;

public   class   DB_GuanLiMember   extends   Connect   {
        private   String   sql   =   " ";

        /**
          *   添加一个新的职员
          *  
          *   @return
          */
        public   int   insertinfor(String   guanlinum,   String   memnum,   String   memname,
                        String   deptnum,   String   guantime,   String   note)   {
                try   {
                        sql   =   "insert   into   guanlimember(guanlinum,memnum,memname,deptnum,   guantime,note)   values(?,?,?,?,?,?) ";
                        this.pst   =   this.con.prepareStatement(sql);
                        this.pst.setString(1,   guanlinum);
                        this.pst.setString(2,   memnum);
                        this.pst.setString(3,   memname);
                        this.pst.setString(4,   deptnum);
                        this.pst.setString(5,   guantime);
                        this.pst.setString(6,   note);
                }   catch   (Exception   ex)   {
                        ex.printStackTrace();
                        Write(ex,   this.toString());
                }
                return   this.insert();
        }

        public   int   updateById(String   guanlinum,   String   memnum,   String   memname,
                        String   deptnum,String   guantime,   String   note,
                        int   id)   {
                try   {
                        sql   =   "update   guanlimember   set   guanlinum=?,memnum=?,memname=?,deptnum=?,guantime=?,note=?   where   id=? ";
                        this.pst   =   this.con.prepareStatement(sql);
                        this.pst.setString(1,   guanlinum);
                        this.pst.setString(2,   memnum);
                        this.pst.setString(3,   memname);
                        this.pst.setString(4,   deptnum);
                        this.pst.setString(5,   guantime);
                        this.pst.setString(6,   note);
                        this.pst.setInt(7,   id);
                }   catch   (Exception   ex)   {
                        ex.printStackTrace();
                        Write(ex,   this.toString());
                }
                return   this.update();
        }
        /**
          *   根据id删除某条记录信息
          *   @param   id
          *   @return
          */
        public   int   deleteinfor(int   id)
        {
                try
                {
                        sql= "delete   from   guanlimember   where   id=? ";
                        this.pst=this.con.prepareStatement(sql);
                        this.pst.setInt(1,   id);
                }catch(Exception   ex)
                {
                        ex.printStackTrace();
                        Write(ex,this.toString());
                }
                return   this.delete();
        }
        /**
          *   查询某一个会员的id信息
          *  
          *   @param   deptnum
          *   @return
          */
        public   ArrayList   selectById(int   id)   {
                try   {
                        sql   =   "select   *   from   guanlimember   where   id=? ";
                        this.pst   =   this.con.prepareStatement(sql);
                        this.pst.setInt(1,   id);
                }   catch   (Exception   ex)   {
                        ex.printStackTrace();
                        Write(ex,   this.toString());
                }
                return   this.select();
        }

        /**
          *   查询某一个部门的会员信息
          *  
          *   @param   deptnum
          *   @return
          */
        public   ArrayList   selectByDeptNum(String   deptnum)   {
                try   {
                        sql   =   "select   *   from   guanlimember   where   deptnum=? ";
                        this.pst   =   this.con.prepareStatement(sql);
                        this.pst.setString(1,   deptnum);
                }   catch   (Exception   ex)   {
                        ex.printStackTrace();
                        Write(ex,   this.toString());
                }
                return   this.select();
        }

        /**
          *   查询管理表中的所有数据信息
          *  
          *   @return
          */
        public   ArrayList   selectAll()   {
                try   {
                        sql   =   "select   guanlimember.id,guanlinum,memnum,memname,jigou.deptnum,jigou.deptname,guantime,guanlimember.note     from   guanlimember,jigou   where   guanlimember.deptnum=jigou.deptnum ";
                        this.pst   =   this.con.prepareStatement(sql);
                }   catch   (Exception   ex)   {
                        ex.printStackTrace();
                        Write(ex,   this.toString());
                }
                return   this.select();
        }
}

最后是相应的action类:
/*
*   Generated   by   MyEclipse   Struts
*   Template   path:   templates/java/JavaClass.vtl
*/
package   com.wnkj.action;

import   javax.servlet.http.HttpServletRequest;
import   javax.servlet.http.HttpServletResponse;
import   javax.servlet.http.HttpSession;

import   org.apache.struts.action.Action;
import   org.apache.struts.action.ActionForm;
import   org.apache.struts.action.ActionForward;
import   org.apache.struts.action.ActionMapping;

import   com.wnkj.dbutil.DB_GuanLiMember;
import   com.wnkj.dbutil.DB_ZhangQi;
import   com.wnkj.formbean.AddZhiYuanForm;

/**
*   MyEclipse   Struts   Creation   date:   05-14-2007
*  
*   XDoclet   definition:
*  
*   @struts.action   path= "/addZhiyuan "   name= "addZhiyuanForm "
*                                 input= "/form/addZhiyuan.jsp "   scope= "request "   validate= "true "
*/
public   class   AddZhiYuanAction   extends   Action   {
        /*
          *   Generated   Methods
          */
        public   static   int   flag   =   0;

        /**
          *   Method   execute
          *  
          *   @param   mapping
          *   @param   form
          *   @param   request
          *   @param   response
          *   @return   ActionForward
          */
        public   ActionForward   execute(ActionMapping   mapping,   ActionForm   form,
                        HttpServletRequest   request,   HttpServletResponse   response)
                        throws   Exception   {
                AddZhiYuanForm   addZhiYuanForm   =   (AddZhiYuanForm)   form;
                String   guanlinum   =   addZhiYuanForm.getGuanlinum();
                String   memnum   =   addZhiYuanForm.getMemnum();
                String   memname   =   addZhiYuanForm.getMemname();
                String   deptnum   =   addZhiYuanForm.getDeptnum();
                String   guantime   =   addZhiYuanForm.getGuantime();
                String   note   =   addZhiYuanForm.getNote();

                int   row   =   new   DB_GuanLiMember().insertinfor(guanlinum,   memnum,   memname,
                                deptnum,   guantime,   note);
                if   (row   ==   1)   {
                        flag   =   1;
                }   else   {
                        flag   =   -1;
                }
                response.sendRedirect(request.getContextPath()+   "/nongzi/addZhiYuan.jsp ");
                return   null;
        }
}
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值