1 package com.test; 2 3 import java.io.BufferedReader; 4 import java.io.FileReader; 5 import java.io.IOException; 6 import java.sql.Connection; 7 import java.sql.DriverManager; 8 import java.sql.PreparedStatement; 9 import java.sql.ResultSet; 10 import java.sql.ResultSetMetaData; 11 import java.sql.SQLException; 12 import java.util.ArrayList; 13 import java.util.List; 14 15 /** 16 * 17 * @author 测试java7的新特性 18 */ 19 public class TestJava7 20 { 21 // 1. 支持二进制字面值 22 public static void binaryLiteral() 23 { 24 int a = 0b100; 25 int b = 0B1100; 26 int c = a & b; 27 System.out.println(c); 28 } 29 30 // 2. 数字字面值中添加下划线"_"分割 31 public static void underlineInNumber() 32 { 33 /* 34 * <注意: 数字的开头结尾不能添加; 小数点前后不能添加; F或L前面不能添加> 35 */ 36 int a = 1_2_3; 37 long b = 92_3L; 38 double c = 3.14_15926; 39 double d = 3.1_415F; 40 System.out.println(a); 41 System.out.println(b); 42 System.out.println(c); 43 System.out.println(d); 44 } 45 46 // 3. switch表达式支持string常量 47 public static void switchSupportString() 48 { 49 String str = "ccc"; 50 switch (str) 51 { 52 case "aaa": 53 System.out.println("I‘m first"); 54 break; 55 case "bbb": 56 System.out.println("I‘m second"); 57 break; 58 case "ccc": 59 System.out.println("I‘m third"); 60 break; 61 default: 62 System.out.println("not matched"); 63 break; 64 } 65 } 66 67 //4. try-with-resources特性支持自动关闭实现了java.lang.AutoCloseable和java.io.Closeable接口的对象资源 68 // 一个 try-with-resources 语句可以像普通的 try 语句那样有 catch 和 finally 块。 69 // 在try-with-resources 语句中, 任意的 catch 或者 finally 块都是在声明的资源被关闭以后才运行 70 public static void tryWithResources() 71 { 72 try 73 { 74 Class.forName("com.mysql.jdbc.Driver"); 75 // 将对象声明在try()中 76 try (Connection connection = DriverManager.getConnection( 77 "jdbc:mysql://172.17.13.38:3306/g2mdev", "root", ""); 78 79 PreparedStatement preparedStatement = connection 80 .prepareStatement("select SQL_CALC_FOUND_ROWS * from g2m_sec_account where id = ? or id = ? or id = ?");) 81 { 82 preparedStatement.setString(1, "1"); 83 preparedStatement.setString(2, "2"); 84 preparedStatement.setString(3, "3"); 85 86 try (ResultSet resultSet = preparedStatement.executeQuery()) 87 { 88 ResultSetMetaData meta = resultSet.getMetaData(); 89 for (int i = 0; i < meta.getColumnCount(); ++i) 90 { 91 System.out.printf("%20s ", meta.getColumnName(i + 1)); 92 } 93 System.out.println(); 94 while (resultSet.next()) 95 { 96 for (int i = 0; i < meta.getColumnCount(); ++i) 97 { 98 System.out.printf("%20s ", resultSet 99 .getString(meta.getColumnName(i + 1))); 100 } 101 System.out.println(); 102 } 103 } 104 } 105 catch (SQLException e) 106 { 107 // TODO Auto-generated catch block 108 e.printStackTrace(); 109 } 110 } 111 catch (ClassNotFoundException e) 112 { 113 e.printStackTrace(); 114 } 115 } 116 117 //5. 一个catch块捕获多个异常 118 public static void multiCatch() 119 { 120 BufferedReader bufferedReader; 121 try 122 { 123 bufferedReader = new BufferedReader(new FileReader( 124 "d:/note.txt")); 125 String buffer = null; 126 while (null != (buffer = bufferedReader.readLine())) 127 { 128 System.out.println(buffer); 129 } 130 bufferedReader.close(); 131 throw new MyException("-------ERROR-------"); 132 } 133 catch (IOException | MyException e) 134 { 135 e.printStackTrace(); 136 } 137 } 138 139 //6. 创建泛型对象时类型推断 140 public static void genericTypeInfer() 141 { 142 List<String> list = new ArrayList<>(); 143 list.add("aaa"); 144 list.add("bbb"); 145 } 146 public static void main(String[] args) 147 { 148 binaryLiteral(); 149 System.out.println("-------------"); 150 underlineInNumber(); 151 System.out.println("-------------"); 152 switchSupportString(); 153 System.out.println("-------------"); 154 tryWithResources(); 155 System.out.println("-------------"); 156 } 157 } 158 class MyException extends Exception 159 { 160 private static final long serialVersionUID = 884387500985353045L; 161 162 public MyException(String string) 163 { 164 super(string); 165 } 166 }
时间: 2024-11-15 10:14:16