我觉得这程序比较乱,重复罗嗦,好多函数重复做事情!
看看其它人的连db的bean 的结构和函数写法。网上多。
个人意见!
拧外,我要分>=20(first up!)

解决方案 »

  1.   

    package dataaccess;/**
     * <p>Title: Users类</p>
     * <p>Description: 连接数据库类DataAccess类,执行对数据库的一些操作</p>
     * <p>Copyright: echo Copyright (c) 2003</p>
     * <p>Company: Time</p>
     * @echo
     * @version 1.0
     */
    import java.sql.*;public class Users {
                                                              //---------------属性:
      private String userID = null;                           //用户id
      private String userPassWord = null;                     //用户密码
      private String userName = null;                         //用户名称  DataAccess userAccess = null;                           //数据库通用连接对象
      ResultSet allRs = null;                                 //所有用户记录集
      ResultSet oneRs = null;                                 //指定用户的纪录集 /*
      String databaseType = "SQLServer";                      //定义SQLServer连接参数
      String url = "192.168.0.14";
      String database = "northwind";
      String user = "sa";
      String pwd = "";
      String table = "Users";
    */
    //  /*
      String databaseType = "Oracle";                         //定义Oracle连接参数
      String url = "192.168.0.36";
      String database = "yq";
      String user = "SCOTT";
      String pwd = "TIGER";
      String table = "YQUSERS1";
    //  */
      public Users() {                                        //---------------构造函数:  }                                                          //---------------方法:
      public boolean addUser(String userID, String userPassWord, String userName) { //1.增加用户    this.userID = userID;
        this.userPassWord = userPassWord;
        this.userName = userName;    userAccess = new DataAccess(databaseType, url, database, user, pwd);    String selectSql = "SELECT UserID FROM "+table+" WHERE UserID='" + userID + "'";
        ResultSet rs = userAccess.sqlSelect(selectSql);    boolean a = false;                             //是否记录集为空,给布尔变量a
        try {
          a = rs.next();
        }
        catch (SQLException ex) {
        }    if (!a) {                                     //如果无此用户,增加用户,返回结果
          String insertSql =
              "INSERT INTO "+table+"(UserID,UserPassWord,UserName) VALUES('" + userID +
              "','" + userPassWord + "','" + userName + "')";
          userAccess.sqlOther(insertSql);
          userAccess.closeAll();
          return true;
        }
        else {
          userAccess.closeAll();
          return false;                                        //否则,返回结果
        }  }
      public boolean updateUser(String userID, String userPassWord, String userName) { //2.修改用户    this.userID = userID;
        this.userPassWord = userPassWord;
        this.userName = userName;    userAccess = new DataAccess(databaseType, url, database, user, pwd);    String selectSql = "SELECT UserID FROM "+table+" WHERE UserID='" + userID + "'";
        ResultSet rs = userAccess.sqlSelect(selectSql);    boolean a = false;                             //是否记录集为空,给布尔变量a
        try {
          a = rs.next();
        }
        catch (SQLException ex) {
        }
        if (a) {                                      //如果有此用户,修改用户,返回结果
          String updateSql =
              "UPDATE "+table+" SET UserPassWord='" + userPassWord + "',UserName='" +
              userName + "' WHERE UserID='" + userID + "'";
          userAccess.sqlOther(updateSql);
          userAccess.closeAll();
          return true;
        }
        else {
          userAccess.closeAll();
          return false;                                         //否则,返回结果
        }  }
      public boolean deleteUser(String userID) {                 //3.删除用户    this.userID = userID;    userAccess = new DataAccess(databaseType, url, database, user, pwd);    String selectSql = "SELECT UserID FROM "+table+" WHERE UserID='" + userID + "'";
        ResultSet rs = userAccess.sqlSelect(selectSql);    boolean a = false;                             //是否记录集为空,给布尔变量a
        try {
          a = rs.next();
        }
        catch (SQLException ex) {
        }
        if (a) {                                       //如果有此用户,删除用户,返回结果
          String deleteSql = "DELETE FROM "+table+" WHERE UserID='" + userID + "'";
          userAccess.sqlOther(deleteSql);
          userAccess.closeAll();
          return true;
        }
        else {
          userAccess.closeAll();
          return false;                                          //否则,返回结果
        }  }
      public boolean checkUser(String userID, String userPassWord) { //4.用户是否合法    this.userID = userID;
        this.userPassWord = userPassWord;    userAccess = new DataAccess(databaseType, url, database, user, pwd);    String selectSql = "SELECT UserID FROM "+table+" WHERE UserID='" + userID + "' AND UserPassWord='"+userPassWord+"'";
        ResultSet rs = userAccess.sqlSelect(selectSql);    boolean a = false;                                        //是否记录集为空,给布尔变量a
        try {
          a = rs.next();
        }
        catch (SQLException ex) {
        }    if (a) {
          userAccess.closeAll();                                  //如果有此用户 并且 密码正确,返回true
          return true;
        }    else {                                                   //否则,返回false
          userAccess.closeAll();
          return false;
        }  }//判断用户是否合法,方法结束
      public String getUserName(String userID) {                  //5.取得用户名    this.userID = userID;    userAccess = new DataAccess(databaseType, url, database, user, pwd);    oneRs = userAccess.sqlSelect("SELECT * FROM "+table+" WHERE UserID='" + userID +
                                     "'");
        try {
          oneRs.next();
          userName = oneRs.getString("UserName");
        }
        catch (SQLException ex) {
        }    userAccess.closeAll();
        return userName;  }
      public ResultSet getAllUserInfo() {                          //6.取得所有用户列表    userAccess = new DataAccess(databaseType, url, database, user, pwd);    allRs = userAccess.sqlSelect("SELECT * FROM "+table);    userAccess.closeAll();
        return allRs;  }
      public ResultSet getOneUserInfo(String userID) {             //7.取得指定用户记录    this.userID = userID;    userAccess = new DataAccess(databaseType, url, database, user, pwd);    oneRs = userAccess.sqlSelect("SELECT * FROM "+table+" WHERE UserID='" + userID +
                                     "'");    userAccess.closeAll();
        return oneRs;  }
      public String getUserPassWord(String userID) {               //8.取得用户密码    this.userID = userID;    userAccess = new DataAccess(databaseType, url, database, user, pwd);    oneRs = userAccess.sqlSelect("SELECT * FROM "+table+" WHERE UserID='" + userID +
                                     "'");
        try {
          oneRs.next();
          userPassWord = oneRs.getString("UserPassWord");
        }
        catch (SQLException ex) {
        }    userAccess.closeAll();
        return userPassWord;  }
      public boolean setUserPassWord(String userID, String userPassWord) { //9.修改用户密码    this.userID = userID;
        this.userPassWord = userPassWord;    userAccess = new DataAccess(databaseType, url, database, user, pwd);    String updateSql =
            "UPDATE "+table+" SET UserPassWord='" + userPassWord + "' WHERE UserID='" +
            userID + "'";
        userAccess.sqlOther(updateSql);    if(userAccess.getRsCount() > 0)
        {                                                    //如果影响记录大于1,说明修改成功
         userAccess.closeRsCn();
          return true;
        }
        else
        {
          userAccess.closeRsCn();
          return false;                                      //否则,说明没有成功
        }
      }
    }
    对照这个看下吧
      

  2.   

    请给点比较容易实现的建议好吗?因为时间紧迫,所以最好能在原程序的基础上做改动,像连接池,JNDI什么的一时半会弄不好的
      

  3.   

    to sunyongxu(上帝的土豆):谢谢,不过好像关于数据库的操作都被封装在DataAccess类中了啊?能不能把DataAccess类的代码贴一帖啊?
      

  4.   

    请给点比较容易实现的建议好吗?因为时间紧迫,所以最好能在原程序的基础上做改动,像连接池,JNDI什么的一时半会弄不好的
      

  5.   

    我现在是这样的情况,就我一个人在某一个页面上操作时,也会发生数据库连接超限的问题,我操作的页面上有几个对数据库的操作,比如查询和插入,删除然后我用ps -ef查看系统进程时(linux)发现操作一次数据库,就多一个oracle进程,如果像您说的,只产生一个dba对象的话,按照上面给出的代码,conn也只有一个,而且不为空,那么根本就不可能产生别的连接啊问题是:这么多连接肯定是从操作数据库中产生出的,那么怎么产生了这么多连接,我只想要产生一个连接,怎么解决?
      

  6.   

    改造后的类(大减肥) [未测试]
    ====================
    package d;import java.sql.*;
    import oracle.jdbc.driver.OracleDriver;class DBconn {  private Connection conn;
      private Statement stmt;
      private ResultSet rs;  public DBconn() throws Exception {    String driver = "oracle.jdbc.driver.OracleDriver";
        String url    = "jdbc:oracle:thin:@localhost:1521:O817DB";
        String user   = "LMY";
        String paswd  =  "LMY";    this(s_DBdriver,s_DBurl,s_DBuser,s_DBpwd); // 调用下面的带参数的构造方法  }  public DBconn(String driver, String url, String user, String password) throws Exception {    try {
          Class.forName(DBdriver);      //将连接数据库的工作迁移到这里,因此,整个类仅建立一次数据库连接
          conn = DriverManager.getConnection(DBurl, DBuser, DBpwd);    } catch (ClassNotFoundException e) {
          System.err.println("DBconn (): " + e.getMessage());
        } catch (Exception e) { // 捕捉其他异常(此处是为了捕捉生成连接数据库而可能产生的异常)
          System.err.println("DBconn (): " + e.getMessage());
        }  }  public ResultSet ExeQuery(String s) {    try{
          // 数据库连接可以复用,但Statement和ResultSet最好不复用,
          // 否则会出现让人不太好理解的错误,虽然还是可以理解.      // *** 同一个Statement只能同时存在一个ResultSet ***
          stmt = conn.createStatement(); // 新生成Statement,其中隐含着消毁先前生成的Statement
          rs   = stmt.executeQuery(s);
        }catch (SQLException e){
          System.err.println("executeQuery Exception:" + e.getMessage());
          return null;
        }
        return rs;  }  public ResultSet ExeQuery(String s, int type_scroll_insensitive, int concur_updatable) {    try{      // *** 同一个Statement只能同时存在一个ResultSet ***
          // 新生成Statement,其中隐含着消毁先前生成的Statement
          stmt = conn.createStatement(type_scroll_insensitive,concur_updatable);
          rs   = stmt.executeQuery(s);
        }catch (SQLException e){
          System.err.println("executeQuery Exception:" + e.getMessage());
          return null;
        }
        return rs;  }  public int ExeUpdate(String s) {    try {
          stmt = conn.createStatement(); // 新生成Statement,其中隐含着消毁先前生成的Statement
          return stmt.executeUpdate(s);
        }
        catch (SQLException e) {
          System.err.println("executeUpdate Exception:" + e.getMessage());
          return -1;
        }  }  /**
       * 使用完这个类的实例后,应该主动调用此方法
       */
      public void Close() {    try {
          if (rs != null)
            rs.close();
          if (stmt != null)
            stmt.close();
          if (conn != null)
            conn.close();
        } catch (Exception e) {
            System.err.println("close Exception:" + e.getMessage());
        }    rs = null;
        stmt = null;
        conn = null;    System.gc(); // 主动回收资源
      }}///:~