上述类DbManager中我只给出了关键代码

解决方案 »

  1.   

    抽象类不能生成实例,也就不成被执行了,只有具体的类才能生成实例,比如DataSourceConnector,它实现了抽象类DbConnector,所以它才能实际地执行DbConnector中地方法。
      

  2.   

    这些我知道,在上面的代码中,如何判断调用DataSourceConnector或者DirectConnector的(getDbConnector)?
      

  3.   

    你可以加一句System.out.println语句,就知道执行的是哪个调用了呀
      

  4.   

    在DbConnector 打印this 看看
      

  5.   

    DbManager的源代码如下:
    package frame.db;import java.sql.*;
    import java.util.ArrayList;
    import frame.util.FastHashMap;public class DbManager {
    static String config = null; 
    static DbConnector dbConnector;
    static FastHashMap tableMap = new FastHashMap();
    static FastHashMap initTable = new FastHashMap();
    public static void setConfig(String conf){
    if(config != null){
    return;
    }
    config = conf;
    dbConnector = DbConnector.getDbConnector(config);
    }
    public static Connection getConnection() throws SQLException {
    Connection conn = null;
    conn = dbConnector.getConnection();
    return conn;
    } public static ArrayList search(Connection conn, String sql) throws SQLException{
    System.err.println("[SEARCH:]" + sql);
    return search(conn, sql, 1, -1);
    } public static ArrayList search(Connection conn, String sql, int count)
    throws SQLException{
    System.err.println("[SEARCH with count:]" + sql + "-------" + count);
    return search(conn, sql, 1, count);
    } public static ArrayList search(Connection conn, String sql,
       int startIndex, int count) throws SQLException {
    System.err.println("[SEARCH with index and count:]" + sql + "-------" + startIndex + "-------" + count);
    ResultSet   rs = null;
    ArrayList   list = new ArrayList();
    Statement   stmt = null;
    try{
    stmt = conn.createStatement();
    rs = stmt.executeQuery(sql);
    if(rs != null){
    ResultSetMetaData metaData = rs.getMetaData();
    int cols = metaData.getColumnCount(); int index = 0;
    while(index<startIndex-1 && rs.next()) {
    index++;
    }
    while(rs.next()){
    index++;
    if(count>=0 && index-startIndex>=count) break;
    String[] row = new String[cols];
    for(int i = 0; i < cols; i++){
    row[i] = rs.getString(i + 1);
    }
    list.add(row);
    }
    }
    }catch(SQLException e) {
    System.err.println("### SQLException catched in DbManager ###:"+ e + " Message:" + e.getMessage() + " Errorcode:" + e.getErrorCode()) ;
    e.printStackTrace();
    throw e;
    }catch(Exception e2) {
    System.err.println("### Exception catched in DbManager ###:"+ e2 + " Message:" + e2.getMessage()) ;
    e2.printStackTrace();
    // throw e2;
    }
    finally{
    if(rs != null){
    rs.close();
    }
    if(stmt != null){
    stmt.close();
    }
    }
    return list;
    }
    public static int searchCount(Connection conn, String sql) throws SQLException{
    System.err.println("[SEARCH COUNT:]" + sql);
    ResultSet   rs = null;
    ArrayList   list = new ArrayList();
    Statement   stmt = null;
    int count = 0;
    try{
    stmt = conn.createStatement();
    rs = stmt.executeQuery(sql);
    if(rs != null){
    while(rs.next()){
    count++;
    }
    }
    }catch(SQLException e) {
    System.err.println("### SQLException catched in DbManager ###:"+ e + " Message:" + e.getMessage() + " Errorcode:" + e.getErrorCode()) ;
    e.printStackTrace();
    throw e;
    }catch(Exception e2) {
    System.err.println("### Exception catched in DbManager ### "+ e2 + " Message:" + e2.getMessage()) ;
    e2.printStackTrace();
    // throw e2;
    }finally{
    if(rs != null){
    rs.close();
    }
    if(stmt != null){
    stmt.close();
    }
    }
    return count;
    } public static void closeConnection(Connection conn) throws SQLException {
    if(conn==null) return;
    conn.close();
    } public static int executeUpdate(Connection conn, String sql) throws SQLException{
    System.err.println("[EXECUTE UPDATE:]" + sql);
    Statement stmt = null;
    int count = 0; try{
    stmt = conn.createStatement();
    count = stmt.executeUpdate(sql);
    }catch(SQLException e) {
    throw e;
    } finally {
    if (stmt != null){
    stmt.close();
    }
    }
    return count;
    } public static int insert(Connection conn, String sql) throws SQLException {
    System.err.println("[INSERT:]" + sql);
    return executeUpdate(conn, sql);
    } public static int update(Connection conn, String sql) throws SQLException {
    System.err.println("[UPDATE:]" + sql);
    return executeUpdate(conn, sql);
    } public static int delete(Connection conn, String sql) throws SQLException {
    System.err.println("[DELETE:]" + sql);
    return executeUpdate(conn, sql);
    } public static ArrayList getFields(String tblname,Connection conn){
    String table = tblname.toUpperCase();
    if(initTable.containsKey(table)){
    return (ArrayList)tableMap.get(table);
    } initTable.put(table,(new Boolean(true)));
    String sql = "select column_name from cols where table_name='" + table + "' order by column_id";//to be add
    ArrayList fields = new ArrayList();
    ArrayList list = null;
    try{
    list = search(conn,sql);
    }catch(Exception e){
    return null;
    }
    for(int i=0;i<list.size();i++){
    String[] row = (String[])list.get(i);
    fields.add(row[0]);
    }
    if(fields.size()>0){
    tableMap.put(table,fields);
    return fields;
    }else{
    return null;
    }
    } public static ArrayList getFields(String tblname){
    String table = tblname.toUpperCase();
    if(initTable.containsKey(table)){
    return (ArrayList)tableMap.get(table);
    } Connection conn = null;
    try{
    conn = getConnection();
    }catch(Exception e){
    return null;
    } ArrayList fields = getFields(table,conn); try{
    closeConnection(conn);
    }catch(Exception e){
    ;
    }
    return fields;
    } public static void beginTransaction(Connection conn) throws SQLException {
    System.err.println("[BEGIN TRANSACTION]");
    //stop autocommit
    conn.setAutoCommit(false);
    } public static void endTransaction(Connection conn, boolean success) throws SQLException {
    System.err.println("[END TRANSACTION]" + success);
    try {
    if(success)
    conn.commit();
    else
    conn.rollback();
    }
    finally {
    conn.setAutoCommit(true);
    }
    } public static int callProcedure(Connection conn, String procedure, String[] params) throws SQLException {
    System.err.println("[CALL PROCEDURE]: " + procedure + ": size = " + params.length);
    if(procedure == null)
    return 0; CallableStatement stmt = null;
    int count = (params==null)?0:params.length; try{
    stmt = conn.prepareCall("{call " + procedure + "}");
    for(int i=0; i<count; i++) {
    stmt.setString(i + 1, params[i]);
    }
    count = stmt.execute()?1:0;
    stmt.close();
    }catch(SQLException e) {
    throw e;
    } finally {
    if (stmt != null){
    stmt.close();
    }
    }
    return count;
    }
    public static int prepareUpdate(Connection conn, String sql, ArrayList params) throws SQLException {
    System.err.println("[PREPARE UPDATE]: " + sql + ": size = " + params.size());
    if(params == null)
    return 0; PreparedStatement stmt = null;
    int count = 0; try{
    String[] row;
    stmt = conn.prepareStatement(sql);
    for(int i=0; i<params.size(); i++) {
    // a row
    row = (String[])params.get(i);
    for(int j=0; j<row.length; j++) {
    stmt.setString(j + 1, row[j]);
    }
    count = stmt.executeUpdate();
    }
    stmt.close();
    }catch(SQLException e) {
    throw e;
    } finally {
    if (stmt != null){
    stmt.close();
    }
    }
    return count;
    }
    }
      

  6.   

    Too long!把问题抽象出来说说,别动不动就贴代码