20110623

将数据库里的BLOB值查出来,生成文件:
byte[] byteArray = rs.getBytes(1);
File file = new File(...);
file.createNewFile();
OutputStream out = new FileOutputStream(file);
out.write(byteArray);
out.close();

====================
String.format(...)方法
String str= String.format("%02d", 5);
输出为“05”,左面不足补零
====================
压缩文件:
public void compressFiles(List<File> list, File zipFile) throws Exception {
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(zipFile));
ZipOutputStream zos = new ZipOutputStream(bos);
BufferedInputStream bis = null;
try {
for (File file : list) {
bis = new BufferedInputStream(new FileInputStream(file));
zos.putNextEntry(new ZipEntry(file.getName()));
int len;
while ((len = bis.read()) > 0) {
zos.write(len);
}
zos.closeEntry();
bis.close();
}
} catch (IOException e) {
throw new Exception("Compress file failed!");
} finally {
try {
if (bis != null) {
bis.close();
}
if (zos != null) {
zos.close();
}
} catch (IOException e) {
// do some log for the I/O not correct close.
}
}
}
==================
JDBC 存储Blob
File fileIn = new File(...);
bodyIn = new FileInputStream(fileIn);
ps.setBinaryStream(3, bodyIn, (int) fileIn.length());
=================
shell 运行java:
#!/bin/sh
JAVA_HOME=/opt/jdk1.6.0_16
test_home=/a/b
lib=$test_home/lib
cp=$test_home
cd $lib
for jar in *.jar
do
cp=$cp:$lib/$jar
done
echo $cp
cd $test_home
#need three parameters to java program
$JAVA_HOME/bin/java -cp $cp  a.b.Runner $1 $2 $3

============
public class TryFinnally {
public static void main(String[] args)  {
int i=0;
try{
++i;
System.out.println("In the try,the i value is :"+i);
throw new RuntimeException();
}catch(Exception e){
++i;
System.out.println("In the catch,the i value is :"+i);
}finally{
++i;
System.out.println("In the finally,the i value is :"+i);
}
++i;
System.out.println("after the finally,the i value is :"+i);


}
}
The result is:
In the try,the i value is :1
In the catch,the i value is :2
In the finally,the i value is :3
after the finally,the i value is :4

你可能感兴趣的:(java,jdbc)