1.方法把ArrayList里面的字符串拼成一个大字符串,并且每行后面加一个“\r”,也就是oracle数据库中的chr(13).
public static void WriteClob() throws Exception {
ArrayList content = new ArrayList();
String back_string = "";
String flag = "\r";
content.add("line1");
content.add("line2");
content.add("line3");
content.add("line4");
content.add("line5");
content.add("line6");
for (int i = 0; i < content.size(); i++) {
back_string += content.get(i) + flag;
}
PreparedStatement pst = null;
ResultSet rs = null;
String sql = null;
Connection conn = DBUtil.getConnection();
conn.setAutoCommit(false);
try {
//update clob field to empty_clob();
sql = "update clob_test set c = empty_clob()";
pst = conn.prepareStatement(sql);
pst.executeUpdate();
pst = null;
sql = " select c from clob_test for update";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
if (rs.next()) {
Clob clob = rs.getClob("C");
Writer outstream = clob.setCharacterStream(0);
outstream.write(back_string);
outstream.flush();
outstream.close();
conn.commit();
}
} catch (Exception e) {
throw e;
} finally {
DBUtil.close(rs, pst, conn);
}
}
2。get clob from oracle:
public static void getClob() throws
Exception {
String sql = null;
Connection conn = DBUtil.getConnection();
PreparedStatement pst = null;
ResultSet rs = null;
StringBuffer buf = new StringBuffer();
try {
sql = "select c from clob_test"
;
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
if (rs.next()) {
Clob clob = rs.getClob(1);
Reader instream = clob.getCharacterStream();
long length = clob.length();
for (long l = 1; l < length; l += 1024) {
buf.append( (clob.getSubString(l, 1024)));
}
String ss = buf.toString();
String tos;
byte b[] = ss.getBytes("UTF-8");
for (int j = 0; j < b.length; j++) {
tos = Integer.toHexString(b[j]);
System.out.println("Integer.toHexString(b[" + j +
"])================>" + tos);
}
System.out.println("buf.toString.length()====:>" +
buf.toString().length());
System.out.println("buf.toString====:>" + buf.toString());
System.out.println("clob length ===>" + length);
}
} catch (Exception e) {
System.err.println(sql);
e.printStackTrace();
} finally {
DBUtil.close(rs, pst, conn);
}
}
3.
/**
* 得到上载文本的内容,以ArrayList返回,其中每个元素是文本文件的一行,为字符串
* @param request MultipartRequest
* @return ArrayList
*/
public ArrayList getUploadedFileContent(MultipartRequest request) {
ArrayList fileContent = new ArrayList();
try {
System.out.println(
"getUploadedFileContent functional is processing========begin================>");
File file = request.getFile( (String) request.getFileNames().nextElement());
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
boolean eof = false;
while (!eof) {
String line = bufferedReader.readLine();
if (line == null) {
eof = true;
} else {
fileContent.add(line);
}
}
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(
"getUploadedFileContent functional is processing========begin================>");
return fileContent;
}
4。自定义异常类:
public class MyException extends Exception {
public MyException () {
super();
}
public MyException (String errorMsg){
super(errorMsg);
}
}
5.
从数据库中取道clob内容,以\r为分隔符,取到一个ArrayList里面去:
public static void StringTokenSplit() throws Exception {
String ss;
ss = getClob();
ArrayList content = new ArrayList();
System.out.println("ss========================>:\n"+ss);
StringTokenizer st = new StringTokenizer(ss,"\r",false);
while(st.hasMoreTokens()){
content.add(st.nextToken());
}
for( int i = 0; i< content.size(); i++) {
System.out.println(" content.size()======================>:\n"+ content.size());
System.out.println(" content.get("+i+")======================>:\n"+ content.get(i));
}
}