java儲存讀取文字oracle

不設限發表於2011-12-11

儲存:


import java.sql.*;
import java.io.*;


public class SaveClob{
public static void main(String[] args){
Connection conn = null;
   PreparedStatement stmt = null;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:dandan";
conn = DriverManager.getConnection(url, "root","dada");
String sql = "insert into book_list values(?,?,?)";
stmt=conn.prepareStatement(sql);
stmt.setString(1,"b1");
stmt.setString(2,"沉思錄");
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
StringBuffer sb = new StringBuffer();
String s;
while((s=br.readLine()) != null){
sb.append(s + "\n"); 
}
br.close();
String content = sb.toString();          
StringReader sr = new StringReader(content);
stmt.setCharacterStream(3, sr, content.length());
stmt.executeUpdate();
sr.close();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(conn != null){
conn.close();
}
}catch(Exception e){
    e.printStackTrace();
}
}
}
}


讀取:


import java.sql.*;
import java.io.*;


public class GetClob{
public static void main(String[] args){
Connection conn = null;
   PreparedStatement stmt = null;
   ResultSet rs =  null;
   FileOutputStream fos = null;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
String url=   "jdbc:oracle:thin:@localhost:1521:ora9";
conn = DriverManager.getConnection(url,"scott","tiger");
String sql="select * from book_list where bid='b1'";
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
            rs.next();            

StringBuffer sb = new StringBuffer();
            Reader rd = rs.getCharacterStream(3);
            BufferedReader br = new BufferedReader(rd);
String s;
            while((s=br.readLine())!=null)
            {
                sb.append(s + "\n");
            }
            System.out.println(sb.toString());
            
            rs.close();
            br.close();
stmt.close();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(conn != null){
conn.close();
}
}catch(Exception e){
    e.printStackTrace();
}
}
}
}

相關文章