zhangyanfengzi
zhangyanfengzi
2012-09-18 15:55

哥们纠结一个问题为什么静态引用类型变量可以调用非静态的方法

  • it技术
  • 互联网问答
  • IT行业问题
  • 编程语言问答
  • 计算机技术
public class OracleUtil {
    
    private static OracleUtil oracleUtil = new OracleUtil(); 
    
    private BasicDataSource bds=null;
    
    private OracleUtil(){
        try {
            Properties props = new Properties();
            props.load(OracleUtil.class.getClassLoader().getResourceAsStream("connection.properties")); // 得到当前类的类加载器,以流的方式读取配置文件
            
            bds=new BasicDataSource();
            bds.setDriverClassName(props.getProperty("orcl.driver"));
            bds.setUrl( props.getProperty("orcl.connection"));
            
            bds.setUsername( props.getProperty("orcl.name"));
            bds.setPassword(props.getProperty("orcl.password"));
            
            bds.setInitialSize(5);
            bds.setMaxActive(9);
            bds.setMaxIdle(7);
            
            bds.setMinIdle(4);
            bds.setMaxWait(5000);
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public static OracleUtil getOracleUtil(){
        return oracleUtil;
    }

    public Connection getConnection() {
        Connection conn = null;
        try {
            conn = bds.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
public class SearchOracleIP {
    
    OracleUtil oracleUtil;
    Connection conn;
    PreparedStatement pstmt;
    ResultSet rs;
    Map map;
    
    public Map SearchByIP() {
        oracleUtil=OracleUtil.getOracleUtil();
        conn = oracleUtil.getConnection();

 conn = oracleUtil.getConnection();oracleUtil不是静态的引用类型变量吗?为什么可以调用非静态的getConnection();方法呢?

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

5条回答

为你推荐