在 private DatabaseCon() {   }中设置不可以吗 

解决方案 »

  1.   

    用静态块
    static
    {
    //do more
    }
      

  2.   

    static DatabaseCon(){  //这里提示出错,不能使用static关键字
        ...
        //想在这里对静态成员变量ThisDatabase和UserNames作一些设置
        ...
      }
    构造函数是不能用static来修饰的!  你把要设置的代码写到private DatabaseCon() {   }即可!这样初始化肯定会执行!
     xiaohuasz() 说的用静态块,在块中也行!
      

  3.   

    你已经在
    static private Database ThisDatabase = new Database();
    初试化了呀,初试话的时候是调用
    Database(){}所以你没有必要在写这么一个static Database(){}(况且更本没这个写法)
      

  4.   

    没有什么可以再说的了
    希望你把class写完以后贴出来看看
    下面是我的数据库扩展类,sql语法是mysql的
    package ecom;
    import java.sql.*;
    import java.util.*;class db_sql {
       private final static String config_file = "ecom.db_conf";
       private final static String driver = "DB_DRIVER";
       private final static String user = "DB_USER";
       private final static String passwd = "DB_USERPASSWORD";
       private final static String url = "DB_URL";
       private static ResourceBundle bundle = null;
       private String table = null;
       private Connection conn = null;   public void setTable(String table) {
          if (conn != null) { this.table = table; System.out.println("setTable(" + table + ") finished"); }
          else System.out.println("setTable(" + table + ") missed");
       }   private void Initiation() {
          if (bundle == null) bundle = ResourceBundle.getBundle(config_file);
          String Url = bundle.getString(url);
          String User = bundle.getString(user);
          String Passwd = bundle.getString(passwd);
          try { Class.forName(bundle.getString(driver)); }
          catch (Exception e) { System.out.println("failed to find the driver"); }
          try { conn = DriverManager.getConnection(Url,User,Passwd); }
          catch (Exception e) { System.out.println("failed to create a connection"); }
       }   public db_sql() { Initiation(); }
       public db_sql(String table) { Initiation(); setTable(table); }   public ResultSet Query(String qry) {
          ResultSet rs = null;
          Statement stmt = null;      System.out.println("Method : query(" + qry + ")");
          try {
             stmt = conn.createStatement();
             rs = stmt.executeQuery(qry);
          } catch (Exception e) { System.out.println("failed to perform the query"); }
          return rs;
       }   public int Update(String upd) {
          int rs = 0;
          Statement stmt = null;      System.out.println("Method : update(" + upd + ")");
          try {
             stmt = conn.createStatement();
             rs = stmt.executeUpdate(upd);
          } catch (Exception e) { Update("ROLLBACK"); System.out.println("failed to perform the update"); }
          return rs;
       }   public void AddbyFields(String Names[], String Values[]) {
          int i, len = Names.length;
          Statement stmt = null;      System.out.println("Method : add()");
          if (table == null) return;
          StringBuffer buf = new StringBuffer("INSERT INTO " + table + " (");
          for (i = 0;i < len;i ++)
             if (i == 0) buf.append(Names[0]);
             else buf.append("," + Names[i]);
          buf.append(") VALUES (");
          for (i = 0;i < len;i ++)
             if (i == 0) buf.append("'" + Values[0] + "'");
             else buf.append(",'" + Values[i] + "'");
          buf.append(")");
          System.out.println(buf);
          try {
             stmt = conn.createStatement();
             stmt.executeUpdate(buf.toString());
          } catch (Exception e) { Update("ROLLBACK"); System.out.println("failed to perform the update"); }
       }   public void UpdateFieldsbyId(int Id, String Names[], String Values[]) {
          int i, len = Names.length;
          Statement stmt = null;      System.out.println("Method : update(ID)");
          if (table == null) return;
          StringBuffer buf = new StringBuffer("UPDATE " + table + " SET ");
          for (i = 0;i < len;i ++)
             if (i == 0) buf.append(Names[0] + "=" + "'" + Values[0] + "'");
             else buf.append("," + Names[i] + "=" + "'" + Values[i] + "'");
          buf.append(" WHERE ID=" + Id);
          System.out.println(buf);
          try {
             stmt = conn.createStatement();
             stmt.executeUpdate(buf.toString());
          } catch (Exception e) { Update("ROLLBACK"); System.out.println("failed to perform the update"); }
       }   public void DeletebyId(int Id) {
          Statement stmt = null;      System.out.println("Method : delete()");
          if (table == null) return;
          String buf = "DELETE FROM " + table + " WHERE ID=" + Id;
          System.out.println(buf);
          try {
             stmt = conn.createStatement();
             stmt.executeUpdate(buf);
          } catch (Exception e) { Update("ROLLBACK"); System.out.println("failed to perform the update"); }
       }   public ResultSet SearchbyId(int Id) {
          Statement stmt = null;
          ResultSet rs = null;      System.out.println("Method : search(id)");
          if (table == null) return null;
          String buf = "SELECT * FROM " + table + " WHERE ID=" + Id;
          System.out.println(buf);
          try {
             stmt = conn.createStatement();
             rs = stmt.executeQuery(buf);
          } catch (Exception e) { System.out.println("failed to perform the query"); }
          return rs;
       }   public ResultSet SearchbyFields(String Preps[], String Names[], String Values[], String Methods[]) {
          Statement stmt = null;
          ResultSet rs = null;
          int i;
          
          System.out.println("Method : search(String)");
          if (table == null) return rs;
          StringBuffer buf = new StringBuffer("SELECT ");
          for (i = 0;i < Preps.length;i ++)
             if (i == 0) buf.append(Preps[0]);
             else buf.append("," + Preps[i]);
          buf.append(" FROM " + table);
          if (Names.length > 0) buf.append(" WHERE ");
          for (i = 0;i < Names.length;i ++) {
             if (i != 0) buf.append(" AND ");
             if (Methods[i].equals("=")) buf.append(Names[i] + "='" + Values[i] + "'");
             if (Methods[i].equals(">")) buf.append(Names[i] + ">'" + Values[i] + "'");
             if (Methods[i].equals("<")) buf.append(Names[i] + "<'" + Values[i] + "'");
             if (Methods[i].equals("|")) buf.append(Names[i] + " LIKE '%" + Values[i] + "%'");
          }
          System.out.println(buf);
          try {
             stmt = conn.createStatement();
             rs = stmt.executeQuery(buf.toString());
          } catch (Exception e) { System.out.println("failed to perform the query"); }
          return rs;
       }    public ResultSet SearchbyFields(Vector Names, Vector Values, Vector Methods, String Complement) {
          Statement stmt = null;
          Enumeration CN, CV, CM;
          ResultSet rs = null;
          String method;
          
          System.out.println("Method : search(Vector)");
          if (table == null) return rs;
          StringBuffer buf = new StringBuffer("SELECT * FROM " + table);
          if (Names.size() > 0) {
             buf.append(" WHERE ");
             for (CN = Names.elements(), CV = Values.elements(), CM = Methods.elements();CN.hasMoreElements();) {
                method = (String)CM.nextElement();
                if (method.equals("=")) buf.append(CN.nextElement() + "='" + CV.nextElement() + "'");
                if (method.equals(">")) buf.append(CN.nextElement() + ">'" + CV.nextElement() + "'");
                if (method.equals("<")) buf.append(CN.nextElement() + "<'" + CV.nextElement() + "'");
                if (method.equals("|")) buf.append(CN.nextElement() + " LIKE '%" + CV.nextElement() + "%'");
                if (CN.hasMoreElements()) buf.append(" AND ");
             }
          }
          if (Complement != null) buf.append(" " + Complement);
          System.out.println(buf);
          try {
             stmt = conn.createStatement();
             rs = stmt.executeQuery(buf.toString());
          } catch (Exception e) { System.out.println("failed to perform the query"); }
          return rs;
       }   public void Close() {
          try { conn.close(); }
          catch (Exception e) { System.out.println("failed to close the connection"); }
          table = null; conn = null;
       }
    }我想要个管理类
      

  5.   

    单态模式?public class DatabaseCon {static private Database ThisDatabase = new Database();
    static private HashMap UserNames = new HashMap();private DatabaseCon() { //不允许从外部实例化
    }static isThisLoaded = false;
    static private loadDatabaseCon(){ 
        if(isThisLoaded) return;
        //如果已经加载过就不用再做什么了。
        //如果没有加载过就去加载,在下面对静态成员初始化
    ...
    //想在这里对静态成员变量ThisDatabase和UserNames作一些设置
    //就在这里设置吧
    ...
        isThisLoaded = true;
    }static public Database getDatabase(){ //返回静态成员变量
    loadDatabaseCon();
    //每次返回实例之前都要加载一次。实际上加载函数自己保证加载函数自己只加载一次。
    return(ThisDatabase);
    }static public HashMap getUserNames(){ //返回静态成员变量
    loadDatabaseCon();
    //每次返回实例之前都要加载一次。实际上加载函数自己保证加载函数自己只加载一次。
    return(UserNames);
    }
    }建议:这个成员最好不要用static 方式的。过多的static成员会让你的代码看着很乱。