17,086
社区成员
发帖
与我相关
我的任务
分享
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import oracle.jdbc.driver.OracleDriver;
import oracle.jdbc.driver.OraclePreparedStatement;
/**
* @author ibm
*
*/
public class TestClob {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
String url = "jdbc:oracle:thin:@localhost:1521:orclu";
String username = "xxx_name";
String passwd = "xxxxx_passwd";
// -----------------------------------ORCLU_TEST_TBS
Properties props = new Properties();
props.put("user", username);
props.put("password", passwd);
props.put("setBigStringTryClob", "true");
DriverManager.registerDriver(new OracleDriver());
Connection conn = DriverManager.getConnection(url, props);
// PreparedStatement pstmt =
// conn.prepareStatement("insert into clob_tab values(?)");
String str = readFile("c:\\sqltest.sql");
OraclePreparedStatement opstmt = (OraclePreparedStatement) conn
.prepareStatement("insert into clob_tab values(?)");
opstmt.setStringForClob(1, str);
opstmt.executeUpdate();
ResultSet rset = null;
opstmt = (OraclePreparedStatement) conn.prepareStatement("select clob_col from clob_tab");
rset = opstmt.executeQuery();
String clobVal=null;
while (rset.next()){
clobVal = rset.getString(1);
System.out.println("CLOB LENGTH: "+clobVal.length());
}
}
public static String readFile(String path) throws Exception {
BufferedReader reader = new BufferedReader(new FileReader(path));
String tt = "";
String content;
content = reader.readLine();
while (null != content) {
tt += content;
content = reader.readLine();
}
return tt;
}
}