第 21章
Java SE6新功能简介
– Java SE6基本新功能
– Apache Derby,JDBC 4.0
java.lang套件
在 Java SE 6中,String类别上新增了
isEmpty()方法
String str = "";
if(str.isEmpty()) {

}
java.util套件
在 Java SE 6中,Arrays类别新增了 copyOf()
方法
int[] arr1 = {1,2,3,4,5};
int[] arr2 = Arrays.copyOf(arr1,arr1.length);
for(int i = 0; i < arr2.length; i++)
System.out.print(arr2[i] + " ");
System.out.println();
int[] arr1 = {1,2,3,4,5};
int[] arr2 = Arrays.copyOf(arr1,10);
for(int i = 0; i < arr2.length; i++)
System.out.print(arr2[i] + " ");
System.out.println();
java.util套件
copyOfRange()方法
binarySearch()方法
int[] arr1 = {1,2,3,4,5};
int[] arr2 = Arrays.copyOf(arr1,1,4);
int[] arr1 = {10,20,30,40,50,60,70,80,90};
int result = Arrays.binarySearch(arr1,6,9,85);
if(result > -1) {
System.out.printf("索引 %d处找到数据 %n",result);
}
else {
System.out.printf("插入点 %d %n",(result + 1) * -1);
}
java.util套件
在 Java SE 6中,您可以直接使用
getDisplayNames()或 getDisplayName()方法取得区域化的日期格式显示
Calendar rightNow = Calendar.getInstance();
Locale locale = Locale.getDefault();
System.out.println("现在时间是,");
System.out.printf("%s,%d %n",
rightNow.getDisplayName(ERA,LONG,locale),
rightNow.get(YEAR));
System.out.println(
rightNow.getDisplayName(MONTH,LONG,locale));
System.out.printf("%d日 %n",
rightNow.get(DAY_OF_MONTH));
System.out.println(
rightNow.getDisplayName(DAY_OF_WEEK,LONG,locale));
java.io套件
使用 System类别上新增的 console()方法
使用 Console物件的 readLine()方法
System.out.print("输入名称,");
String name = System.console().readLine();
System.out.print("输入密码,");
char[] passwd = System.console().readPassword();
String password = new String(passwd);
if("caterpillar".equals(name) &&
"123456".equals(password)) {
System.out.println("欢迎 caterpillar ");
break;
}
else {
System.out.printf("%s,名称或密码错误,请重新输入! %n",name);
}
java.io套件
String name = console.readLine("[%s] ","输入名称 …");
char[] passwd = console.readPassword("[%s]","输入密码 …");
String password = new String(passwd);
if("caterpillar".equals(name) &&
"123456".equals(password)) {
System.out.println("欢迎 caterpillar ");
break;
}
else {
System.out.printf("%s,名称或密码错误,请重新输入! %n",name);
}
java.io套件
在 Java SE6中,对于 File类别新增了几个方法
File[] roots = File.listRoots();
for(File root,roots) {
System.out.printf("%s总容量 %d,可用容量 %d %n",
root.getPath(),root.getTotalSpace(),
root.getUsableSpace());
}
java.awt套件
指定启动屏幕的图片
manifest档案的写法
java-splash:caterpillar.jpg -jar JNotePad.jar
Manifest-Version,1.0
Main-Class,onlyfun.caterpillar.JNotePad
SplashScreen-Image,caterpillar.jpg
java.awt套件
系统工具栏图标的支持
if(SystemTray.isSupported()) {
SystemTray tray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit()
.getImage("musical_note_smile.gif");
TrayIcon trayIcon = new TrayIcon(image,"JNotePad 1.0");
try {
tray.add(trayIcon);
} catch (AWTException e) {
System.err.println("无法加入系统工具栏图标 ");
e.printStackTrace();
}
} else {
System.err.println("无法取得系统工具栏 ");
}
java.awt套件
系统工具栏图标的支持
if(SystemTray.isSupported()) {
SystemTray tray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit()
.getImage("musical_note_smile.gif");
PopupMenu popup = new PopupMenu();
MenuItem item = new MenuItem("开启 JNotePad 1.0");
popup.add(item);
TrayIcon trayIcon =
new TrayIcon(image,"JNotePad 1.0",popup);
try {
tray.add(trayIcon);
} catch (AWTException e) {
System.err.println("无法加入系统工具栏图标 ");
e.printStackTrace();
}
} else {
System.err.println("无法取得系统工具栏 ");
}
java.awt套件
系统工具栏图标上主动显示讯息
移除系统工具栏中的图标
trayIcon.displayMessage("哈囉 ","该休息了吗? ",
TrayIcon.MessageType.WARNING);
tray.remove(trayIcon);
Classpath简化设定
在 Java SE 6中,您可以使用 '*'来指定某个目录下的所有,jar档案
java –cp,;c:\jars\* onlyfun.caterpillar.JNotePad
使用 Apache Derby
在 JDK6中捆绑了 ApacheDerby数据库
( http://db.apache.org/derby/)
纯 Java撰写的数据库,支援 JDBC 4.0
可以在 JDK6包装目录的 db目录下找到
ApacheDerby的相关档案加载驱动程序
JDBC 4.0之中,不需要再呼叫 Class.forName()并指定驱动程序
JVM会自动在 Classpath中寻找适当的驱动程序
在包装有 JDBC驱动程序的 JAR档案中,必须有一个 "META-INF/services/java.sql.Driver"档案,当中撰写驱动程序类别名称
Connection conn = DriverManager.getConnection(
url,username,password);
改进的例外处理
在 JDBC 4.0之中,SQLException新增了几个建构函式,可以接受 Throwable实例进行
SQLException的建构
SQLException并实作了 Iterable<T>界面改进的例外处理
try {

}
catch(SQLException ex) {
for(Throwable t,ex) {
System.err.println(t);
Throwable cause = t.getCause();
while(cause != null) {
System.err.println("Cause," + cause);
cause = cause.getCause();
}
}
}
BLOB,CLOB的改进
在 JDBC 4.0中,PreparedStatement新增了接受 InputStream实例的 setBlob()等方法
会将资料以 BLOB的方式送至数据库
对于 CLOB,PreparedStatement也增加有接受 Reader实例的 setClob()等方法
Connection上,也增加有 createBlob()与
createClob()等方法