在持久化过程中我们往往要用到存储一些大数据的操作,比如说用户的靓照,还有用户的整个数据文件什么的啦!在oracle中提供了blog,clog等字段好象可以提供此功能呢(另外当然也可以直接放到如硬盘等其它存储设备上)。
就我个的人一些实践记下:
1:存放photo,比如gif的二进制数据:
//在一个字节数据中存放此gif文件的二进制数据
InputStream in = new InputStream("photo.gif");//locate你的photho啦!
byte[] buffer_in = new byte[in.available()];
in.read(buffer_in);
in.close();
//将buffer_in持久化到DB中去
//再将其写到内存来
byte[] buffer_out = getPhoto();//get the buufer_in from DB ^_^
String path = context.getRealPath("/");
FileOutputStream fout = new FileOutputStream(path+"photo.gif");//locate your out path for your photo
fout.write(buffer_out);
fout.close(); 2:对如比较大的文件可以存在CLOG中:
oracle.jdbc.OraclePreparedStatement pstmt =
(oracle.jdbc.OraclePreparedStatement)OCon.prepareCall(updatesql);
pstmt.setCLOB(1,tempclob);//tempclob就是存信DB中clog的内容,clog的得到可以由以下function。
//得到存于oracle表中clob的function
private CLOB getCLOB(String xmlData, Connection conn)
throws SQLException
{
CLOB tempClob = null;
try
{
//tempClob = CLOB.getEmptyCLOB();
tempClob = CLOB.createTemporary(conn,true,CLOB.DURATION_SESSION);
// If the temporary CLOB has not yet been created, create one
// tempClob = CLOB.createTemporary(conn, true, CLOB.DURATION_SESSION);
// Open the temporary CLOB in readwrite mode, to enable writing
tempClob.open(CLOB.MODE_READWRITE);
// Get the output stream to write
Writer tempClobWriter = tempClob.getCharacterOutputStream();
// Write the data into the temporary CLOB
tempClobWriter.write(xmlData);
// Flush and close the stream
tempClobWriter.flush();
tempClobWriter.close();
// Close the temporary CLOB
tempClob.close();
}
catch(SQLException sqlexp)
{
tempClob.freeTemporary();
sqlexp.printStackTrace();
}
catch(Exception exp)
{
tempClob.freeTemporary();
exp.printStackTrace();
}
return tempClob;
}
/** *//**
*在此多说几句呢,在运行到
*tempClob = CLOB.createTemporary(conn,true,CLOB.DURATION_SESSION);
*时若是出现:ClassCastException 不太受欢迎的信息,看一下你的DB连接是不是连接池什么的,
*我以前的用的连接(会出现此异常)如下:
*<Resource name="jdbc/ttt" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="3000"
username="LMSOwner" password="password" driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@appserver:1521:ttt"/>
try
{
if ( _Debug )
{
System.out.println(" ::--> Connecting to the DB");
}
Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:/comp/env");
DataSource ds = (DataSource) envContext.lookup("jdbc/ttt");
conn = ds.getConnection();
}
改成:
try{
// Load the database details into the variables.
String url="jdbc:oracle:thin:@appserver:1521:ttt";
String user = "LMSOwner";
String password = "ttt";
// Create the properties object that holds all database details
java.util.Properties props = new java.util.Properties();
props.put("user", user );
props.put("password", password);
props.put("SetBigStringTryClob", "true");
// Load the Oracle JDBC driver class.
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
// Get the database connection
conn = DriverManager.getConnection( url, props );
}
就OK了(可能是连接池的一个缺陷吧,呵呵不是太懂)
*/
将其从DB中写入到内存
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//to connect DB
import com.studyez.lms.util.LMSDatabaseHandler;
import java.sql.*;
import oracle.sql.*;
import oracle.xdb.XMLType;
/** *//**
* Servlet implementation class for Servlet: UserCourseData
*
*/
public class UserCourseData extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet
{
/** *//**
*
*/
private static final long serialVersionUID = 1L;
/**//*
* (non-Java-doc)
*
* @see javax.servlet.http.HttpServlet#HttpServlet()
*/
public UserCourseData()
{
super();
}
/**//*
* (non-Java-doc)
*
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
* HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
// TODO Auto-generated method stub
String tlearnerID = (String) request.getParameter("learnerID");
int lID = Integer.parseInt(tlearnerID);
String courseID = (String) request.getParameter("courseID");
int cID = Integer.parseInt(courseID);
String sql = "select * from usercoursedata where userID = " + lID + " and courseID = " + cID;
String tempDocStr = null;
try
{
Connection conn = LMSDatabaseHandler.getTempConn();
Statement stmt = conn.createStatement();
ResultSet rst = stmt.executeQuery(sql);
OPAQUE xml;
if(rst.next())
{
// rst.first();
oracle.jdbc.OracleResultSet temprst = (oracle.jdbc.OracleResultSet) rst;
xml = temprst.getOPAQUE("xmlDoc");
XMLType xt = XMLType.createXML(xml);
// doc = xt.getDOM();
tempDocStr = xt.getStringVal();
// System.out.println("Testing getDOM() ");
}
else
{
System.out.println("shit he is go now ");
tempDocStr = "<root></root>";
}
// Write XML to response.
response.setContentType("application/xml");
response.getWriter().write(tempDocStr);
if(rst != null)
rst.close();
if(conn != null)
conn.close();
// System.out.println(tempDocStr);
}
catch(SQLException e)
{
e.printStackTrace();
tempDocStr = "<root></root>";
// Write XML to response.
response.setContentType("application/xml");
response.getWriter().write(tempDocStr);
}
}
/**//*
* (non-Java-doc)
*
* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
* HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
// TODO Auto-generated method stub
doGet(request, response);
}
}
.
对于clob我也是在摸索中写进去的(运行时通过),不足之处请指正。谢谢!