struts本身的连接池使用方法就很适合你的这个要求啦。找它的例子看一下你就明白了。

解决方案 »

  1.   

    struts一般是在每个action类里面进行连接的,我就是想把它独立出来package employees;import java.io.IOException;
    import javax.servlet.ServletException;
    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 org.apache.struts.action.ActionErrors;
    import org.apache.struts.action.ActionError;import javax.sql.DataSource;
    import java.sql.Connection;
    import java.sql.Statement;
    import java.sql.ResultSet;
    import java.sql.SQLException;public class LoginAction extends Action {        public ActionForward execute(ActionMapping mapping,
                    ActionForm form,
                    HttpServletRequest request,
                    HttpServletResponse response)
                    throws IOException, ServletException {    String user = null;                // Default target to success
        String target = new String("success");    // Use the LoginForm to get the request parameters
              String username = ((LoginForm)form).getUsername();
              String password = ((LoginForm)form).getPassword();          DataSource dataSource = (DataSource)
                servlet.getServletContext().getAttribute("evocdb");    user = getUser(username, password);    // Set the target to failure
        if ( user == null ) {      target = new String("login");
          ActionErrors errors = new ActionErrors();      errors.add(ActionErrors.GLOBAL_ERROR,
            new ActionError("errors.login.unknown",
                            username));      // Report any errors we have discovered back to the original form
          if (!errors.isEmpty()) {        saveErrors(request, errors);
          }
        }
        else {      HttpSession session = request.getSession();
          session.setAttribute("USER", user);
        }
              // Forward to the appropriate View
              return (mapping.findForward(target));
            }        protected String getUser(String username, String password) {          String user = null;
              Connection conn = null;
              Statement stmt = null;
              ResultSet rs = null;          DataSource dataSource = (DataSource)
                servlet.getServletContext().getAttribute("evocdb");          try {            conn = dataSource.getConnection();
                stmt = conn.createStatement();
                String sql = "select * from employees where username=\'"
                  + username + "' " + "and password='" + password + "'";
                rs = stmt.executeQuery(sql);
    //            DbLink dl = new DbLink();
    //            rs = dl.DbQuery(sql);            if ( rs.next() ) {              user = rs.getString("username");
                  // Iterate over the results
                  System.err.println("Username : "
                    + rs.getString("username")
                    + " Password : " + rs.getString("password"));
                }
                else {              System.err.println("---->User not found<----");
                }
              }
              catch (SQLException e) {            System.err.println(e.getMessage());
              }
              finally {            if (rs != null) {              try {                rs.close();
                  }
                  catch (SQLException sqle) {                System.err.println(sqle.getMessage());
                  }
                  rs = null;
                }
                if (stmt != null) {              try {                stmt.close();
                  }
                  catch (SQLException sqle) {                System.err.println(sqle.getMessage());
                  }
                  stmt = null;
                }
                if (conn != null) {              try {                conn.close();
                  }
                  catch (SQLException sqle) {                System.err.println(sqle.getMessage());
                  }
                  conn = null;
                }
              }
              return user;
            }
    }
      

  2.   

    struts数据库连接池,不建议使用,不知道原因。
    apahe有个DBCP就是干这个的
      

  3.   

    那用什么方式连接数据库效率最好呢
    我现在是直接用JDBC连接的,可能每次连接花费的时间比较长点了~