Java笔记

  • JDBC
    // Oracle
    String driver = "com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://地址:端口(默认3306)/数据库名?useUnicode=true&characterEncoding=utf8";
    
    //MySql
    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@地址:端口(默认1521):数据库名";
    
    //Sql Server 2000
    String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
    String url = "jdbc:microsoft:sqlserver://地址:端口(默认1433);DatabaseName=数据库名;";
    
    // Sql Server 2005
    String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    String url = "jdbc:sqlserver://地址:端口(默认1433);databaseName=数据库名";
    
    
    String user = "登录名";
    String password = "密码";
    Class.forName(driver);
    Connection conn = DriverManager.getConnection(url, user, password);
     
  • 取值范围
        public static void main(String[] args) {
    
            byte maxByte = 0x7f;
            byte minByte = (byte)0x80;
            System.out.println("maxByte = " + maxByte + ", minByte = " + minByte);
    
            short maxShort = 0x7fff;
            short minShort = (short) 0x8000;
            System.out.println("maxShort = " + maxShort + ", minShort = " + minShort);
    
            int maxInt = 0x7fffffff;
            int minInt = 0x80000000;
            System.out.println("maxInt = " + maxInt + ", minInt = " + minInt);
    
            long maxLong = Long.decode("0x7fffffffffffffff");
            long minLong = Long.decode("-0x8000000000000000"); // bug?! 应该不需要再添加-号的
            System.out.println("maxLong = " + maxLong + ", minLong = " + minLong);
    
            // float表示方式(32bit): 1bit(符号位, S) + 8bit(指数位, 无符号, E) + 23bit(尾数位, M)
            // 指数位为127的偏移值. 如00000001 = 1 - 127 = -126; 11111110 = 254 - 127 = 127;
            // 当指数位为11111111(255)时, 用于表示NaN,正负无穷数.具体见下列相关代码
            // float计算: 符号位 * (2^指数位) * 尾数位
            // 最大正单精度浮点数 0 11111110 1111111111111111111111111
            float maxPositiveNumber = Float.intBitsToFloat(0x7f7fffff);
            // 最小正单精度浮点数 0 00000001 0000000000000000000000000
            float minPositiveNumber = Float.intBitsToFloat(0x08000000);
            System.out.println("maxPositiveNumber = " + maxPositiveNumber + ", minPositiveNumber = " + minPositiveNumber);
            // 0. IEEE754规定 E=0, M=0, 则值为0.
            // 正0: 0 00000000 00000000000000000000000
            float positiveZero = Float.intBitsToFloat(0x00000000);
            // 负0: 1 00000000 00000000000000000000000
            float negativeZero = Float.intBitsToFloat(0x80000000);
            System.out.println("positiveZero = " + positiveZero + ", negativeZero = " + negativeZero);
            // 最大负单精度浮点数 1 00000001 0000000000000000000000000
            float maxNegativeNumber = Float.intBitsToFloat(0x88000000);
            // 最小负单精度浮点数 1 11111110 1111111111111111111111111
            float minNegativeNumber = Float.intBitsToFloat(0xff7fffff);
            System.out.println("maxNegativeNumber = " + maxNegativeNumber + ", minNegativeNumber = " + minNegativeNumber);
            // NaN, 非数值. E=255 M<>0
            // 例子: 0 11111111 00000000000000000000001
            float NaN = Float.intBitsToFloat(0x7f800001);
            System.out.println("nan = " + NaN);
            // 正无穷 S=0, E=255, M=0. 0 11111111 00000000000000000000000
            float POSITIVE_INFINITY = Float.intBitsToFloat(0x7f800000);
            // 负无穷 S=1, E=255, M=0. 1 11111111 00000000000000000000000
            float NEGATIVE_INFINITY = Float.intBitsToFloat(0xff800000);
            System.out.println("POSITIVE_INFINITY = " + POSITIVE_INFINITY + ", NEGATIVE_INFINITY = " + NEGATIVE_INFINITY);
    
            /*
            Consider the bit representation of an IEEE-754 binary64 format:
                    s_eee_eeee_eeee_mmmm_mmmm_mmmm_mmmm_mmmm_mmmm_mmmm_mmmm_mmmm_mmmm_mmmm_mmmm_mmmm
              IEEE-754 binary64 values are derived as such:
                If e is more than 0 and less than 2047 (2047 is 111_1111_1111 in binary),
                  then value is equal to (-1)s × 2e-1023 × (1 + m × 2-52). (These are the normal numbers.)
                If e equals 0,
                  then value equals (-1)s × 2(e+1)-1023 × (0 + m × 2-52). (Besides zero, these are the subnormal numbers.)
                If e equals 2047 and m equals 0,
                  then value equals (-1)s × infinity.
                If e equals 2047 and m not equals 0,
                  then value equals NaN. (Unrelated fact: thus there is 2 × (252 - 1) different bit representations for NaN; cf. doubleToRawLongBits.)
                Therefore, the smallest positive IEEE-754 binary64 normal number is equal to:
                         (-1)0 × 21-1023 × (1 + 0 × 2-52)
                      = 2-1022
                And the smallest positive IEEE-754 binary64 subnormal number is equal to:
                         (-1)0 × 2(0+1)-1023 × (0 + 1 × 2-52)
                      = 2-1022 × 2-52
                      = 2-1074
             */
        }
     
  • url参数处理
    URL特殊符号及对应的十六进制值编码:   
     
    1. +  URL 中+号表示空格 %2B   
    2. 空格 URL中的空格可以用+号或者编码 %20   
    3. /  分隔目录和子目录 %2F    
    4. ?  分隔实际的 URL 和参数 %3F    
    5. % 指定特殊字符 %25    
    6. # 表示书签 %23    
    7. & URL 中指定的参数间的分隔符 %26    
    8. = URL 中指定参数的值 %3D  
    
    将获取参数的方法从request.getParameter()改为request.getQueryString(), 然后对获取的字符串进行另外解析.
     
  • java文件路径空格及中文处理
        TestURL().class.getResource("").getPath()或TestURL().class.getResource("").getFile()获得的路径,不能被FileReader()和FileWriter()直接应用。
        原因是URL对空格,特殊字符(%,#,[]等)和中文进行了编码处理。
        例如:空格变为%20。
        方法(1),使用repaceAll("%20",' ')替换后,只能解决空格问题。但是路径中包含%和中文就不行了。
        方法(2),使用URLDecoder.decode(str,"UTF-8")解码,但是只能解决一部分,若路径中含有+,也是不能解决的,原因是URL并不是完全用URLEncoder.encode(str,"UTF-8")编码的,+号被解码后,却变成了空格。
        方法(3),可以解决所有的问题,用TestURL().class.getResource("").toURI().getPath(),但是需要处理URISyntaxException异常,比较麻烦点。
     
  • joda time
		// 计算时间差
		long ms = (long)(3.33 * 12 * 30 * 24 * 60 * 60 * 1000) ;
		System.out.println(ms); // 103576319999
		DateTime dt1 = DateTime.now();
		DateTime dt2 = dt1.plus(ms);
		log(Years.yearsBetween(dt1, dt2).getYears(),  // {int} 3
				Months.monthsBetween(dt1, dt2), // {Months} P39M
				Days.daysBetween(dt2, dt1), // {Days} P-1198D
				Seconds.secondsBetween(dt2, dt1).getSeconds()); // {int} -103576319
		
		Period p = new Period(ms); // 根据一段时长创建Period
		log(p.getYears() , p.getMonths(), p.getDays(), // 0 0 0
				p.getHours(), p.getMinutes(), p.getSeconds()); // 28771 11 59
		
		p = new Period(dt1, dt2); // 根据两个时间创建Period
		log(p.getYears() , p.getMonths(), p.getDays(), // 3 3 3
				p.getHours(), p.getMinutes(), p.getSeconds()); // 19 11 59

 

你可能感兴趣的:(java)