当前位置: 技术问答>java相关
怎样用oracle保存长字符串(4、5千字左右),并用jsp取出?在线给分
来源: 互联网 发布时间:2015-09-21
本文导语: 怎样用oracle保存长字符串(4、5千字左右),并用jsp取出? | 用blob可以实现,大致如下: public synchronized boolean insertBlob(Connection v_connection, String arg_blob...
怎样用oracle保存长字符串(4、5千字左右),并用jsp取出?
|
用blob可以实现,大致如下:
public synchronized boolean insertBlob(Connection v_connection,
String arg_blobTableName,String arg_blobIdColName,String arg_blobColName,
int v_rowid,byte[] v_bindata)
{
String sql = "INSERT INTO " + arg_blobTableName + " ( " + arg_blobIdColName +","+ arg_blobColName +") VALUES ( ?, ? )";
try {
ByteArrayInputStream bais = new ByteArrayInputStream(v_bindata);
PreparedStatement ps = v_connection.prepareStatement(sql);
int paramindex = 1;
ps.setInt(paramindex++, v_rowid);
ps.setBinaryStream(paramindex++, bais, v_bindata.length);
statement:
ps.executeUpdate();
ps.close();
return true;
}
catch ( SQLException se )
{
System.err.println("Couldn't insert binary data: "+se);
return false;
}
}
public byte[] selectBlob(Connection v_connection,String v_blobsql)
{
byte[] returndata = null;
try {
Statement sment = v_connection.createStatement();
ResultSet rs = sment.executeQuery(v_blobsql);
if ( rs.next() )
{
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
BufferedInputStream bis = new BufferedInputStream( rs.getBinaryStream(1) );
byte[] bindata = new byte[1024];
int bytesread = 0;
if ( !rs.wasNull() )
{
if ( (bytesread = bis.read(bindata,0,bindata.length)) != -1 )
{
baos.write(bindata,0,bytesread);
}
else
{
returndata = baos.toByteArray();
}
}
bis.close();
}
catch ( IOException ioe )
{
System.err.println("Problem retrieving binary data: " + ioe);
}
}
rs.close();
sment.close();
}
catch ( SQLException se )
{
System.err.println("Couldn't retrieve binary data: " + se);
}
finally
{
System.err.println("Couldn't retrieve binary data: ");
}
return returndata;
}
}
public synchronized boolean insertBlob(Connection v_connection,
String arg_blobTableName,String arg_blobIdColName,String arg_blobColName,
int v_rowid,byte[] v_bindata)
{
String sql = "INSERT INTO " + arg_blobTableName + " ( " + arg_blobIdColName +","+ arg_blobColName +") VALUES ( ?, ? )";
try {
ByteArrayInputStream bais = new ByteArrayInputStream(v_bindata);
PreparedStatement ps = v_connection.prepareStatement(sql);
int paramindex = 1;
ps.setInt(paramindex++, v_rowid);
ps.setBinaryStream(paramindex++, bais, v_bindata.length);
statement:
ps.executeUpdate();
ps.close();
return true;
}
catch ( SQLException se )
{
System.err.println("Couldn't insert binary data: "+se);
return false;
}
}
public byte[] selectBlob(Connection v_connection,String v_blobsql)
{
byte[] returndata = null;
try {
Statement sment = v_connection.createStatement();
ResultSet rs = sment.executeQuery(v_blobsql);
if ( rs.next() )
{
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
BufferedInputStream bis = new BufferedInputStream( rs.getBinaryStream(1) );
byte[] bindata = new byte[1024];
int bytesread = 0;
if ( !rs.wasNull() )
{
if ( (bytesread = bis.read(bindata,0,bindata.length)) != -1 )
{
baos.write(bindata,0,bytesread);
}
else
{
returndata = baos.toByteArray();
}
}
bis.close();
}
catch ( IOException ioe )
{
System.err.println("Problem retrieving binary data: " + ioe);
}
}
rs.close();
sment.close();
}
catch ( SQLException se )
{
System.err.println("Couldn't retrieve binary data: " + se);
}
finally
{
System.err.println("Couldn't retrieve binary data: ");
}
return returndata;
}
}