|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
你通过从书的数量和开发周期及运行速度来证明:net和ruby要比java简单。oracle/**
*
*操纵oracle数据库的CLOB字段,包含读和写
*令少爷
**/
packagecom.nes.common.sql.lob;
importjava.sql.*;
importjava.io.*;
importoracle.jdbc.OracleResultSet;
importoracle.sql.*;
publicclassJClob{
StringtableName=null;//表名
StringprimaryKey=null;//表的主键名
StringprimaryValue=null;//表的主键值
StringfieldName=null;//表的CLOB字段名
StringclobValue=null;//表的CLOB字段值
Connectionconn=null;//与oracle的毗连
/**
*
*用于测试用
*
**/
publicstaticvoidmain(String[]args){
try{
JClobjc=newJClob(getConnection(),"aa","a","aaaa","c","ccc");
jc.write();
jc.read();
}
catch(Exceptione){
System.out.println(e);
e.printStackTrace();
}
}
/**
*
*机关办法
*
**/
publicJClob(Connectionconnection,StringtableName,StringprimaryKey,StringprimaryValue,StringfieldName,StringclobValue){
this.conn=connection;
this.tableName=tableName;
this.primaryKey=primaryKey;
this.primaryValue=primaryValue;
this.fieldName=fieldName;
this.clobValue=clobValue;
}
/**
*
*机关办法,但不用传clobValue值
*一样平常机关出的实例用来读Clob字段
*
**/
publicJClob(Connectionconnection,StringtableName,StringprimaryKey,StringprimaryValue,StringfieldName){
this.conn=connection;
this.tableName=tableName;
this.primaryKey=primaryKey;
this.primaryValue=primaryValue;
this.fieldName=fieldName;
}
/**
*
*用于测试
*
**/
publicstaticConnectiongetConnection()throwsSQLException,ClassNotFoundException{
Class.forName("oracle.jdbc.OracleDriver");
Connectionconn=DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.18:1521:portal","portal","portal");
returnconn;
}
/**
*
*读数据库中clob字段的内容
*@returnclob字段值
*
**/
publicStringread()throwsSQLException,IOException{
Stringrtn=null;
try{
Stringsql="select"+fieldName+"from"+tableName+"where"+primaryKey+"="+primaryValue;
//Connectionconn=getConnection();
PreparedStatementpstmt=conn.prepareStatement(sql);
//intv=Integer.parseInt(primaryValue);
//pstmt.setInt(1,v);
ResultSetrs=pstmt.executeQuery();
java.sql.Clobclob=null;
if(rs.next()){
clob=rs.getCLOB(fieldName);
//clob=((OracleResultSet)rs).getCLOB(fieldName);
//clob=((org.apache.commons.dbcp.DelegatingResultSet)rs).getClob(fieldName);
//Readerin=clob.getCharacterStream();
InputStreaminput=clob.getAsciiStream();
intlen=(int)clob.length();
byte[]by=newbyte[len];
inti;//=input.read(by,0,len);
while(-1!=(i=input.read(by,0,by.length))){
input.read(by,0,i);
}
rtn=newString(by);
}
}
catch(SQLExceptione){
throwe;
}
catch(Exceptionee){
ee.printStackTrace();
}
returnrtn;
}
/**
*
*葱数据库中clob字段的内容
*
**/
publicvoidwrite()throwsSQLException,IOException{
Stringsql="update"+tableName+"set"+fieldName+"=empty_clob()where"+primaryKey+"="+primaryValue;
//Connectionconn=getConnection();
conn.setAutoCommit(false);
PreparedStatementpstmt=conn.prepareStatement(sql);
pstmt.executeUpdate();
sql="select"+fieldName+"from"+tableName+"where"+primaryKey+"="+primaryValue;
Statementst=conn.createStatement();
ResultSetrs=st.executeQuery(sql);
java.sql.Clobclob;
if(rs.next()){
clob=((oracle.jdbc.OracleResultSet)rs).getClob(fieldName);
//clob=((org.apache.commons.dbcp.DelegatingResultSet)rs).getClob(fieldName);
oracle.sql.CLOBmy_clob=(oracle.sql.CLOB)clob;
OutputStreamwriter=my_clob.getAsciiOutputStream();
byte[]contentStr=this.getContent().getBytes();
writer.write(contentStr);
writer.flush();
writer.close();
}
conn.commit();
rs.close();
st.close();
pstmt.close();
conn.setAutoCommit(true);
}
/**
*
*
**/
privateStringgetContent(){
returnthis.clobValue;
}
/**
*
*
**/
publicvoidsetClobValue(StringclobValue){
this.clobValue=clobValue;
}
}
最后我再次声明,我并没有说不看好java,实际上我对java很乐观的,毕竟她正在不断改进中,我相信她总有一天会和.net并驾齐驱的 |
|