操作Oracle資料庫的clob類型

來源:互聯網
上載者:User
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<%@ page import="java.util.*,java.sql.*,oracle.sql.*,java.io.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Oracle Operation</title>
<style type="text/css">
<!--
td {
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
color: #000000;
text-decoration: none;
}
-->
</style>
</head>
<body>
<%
java.sql.Connection conn; //資料庫連接對象\r
java.lang.String strCon; //資料庫連接字串
strCon ="jdbc:oracle:thin:@localhost:1521:via";
java.lang.String strSQL; //SQL語句\r
//try {

Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
conn = java.sql.DriverManager.getConnection(strCon,"kevin","ddd");
Statement stmt=conn.createStatement();
java.sql.PreparedStatement pstmt = null;
boolean defaultCommit = conn.getAutoCommit();
conn.setAutoCommit(false);
String query;
query ="select * from tblproduct ";
pstmt = conn.prepareStatement(query);
ResultSet rs = pstmt.executeQuery();

%>
<table width="770" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#e7e7e7">
<%
String content = "";
String description = "";
while (rs.next()){
oracle.jdbc.driver.OracleResultSet ors = 
       (oracle.jdbc.driver.OracleResultSet)rs;
  Clob clobtmp = (Clob)ors.getClob(8);
  if(clobtmp==null || clobtmp.length()==0){
    //out.println("======clob對象為空白 ");
    description = "";
  }else{
    description=clobtmp.getSubString((long)1,(int)clobtmp.length());
    //out.println("======字串形式 "+description);
  }
//clob.empty_lob();
/* 以字元形式輸出 */
/*BufferedReader in = new BufferedReader(clob.getCharacterStream());
BufferedWriter out2 = new BufferedWriter(new FileWriter("test.txt"));
int c;
*/
String str="";
if (clobtmp != null){
Reader is = clobtmp.getCharacterStream();
BufferedReader br = new BufferedReader(is);
str = br.readLine();
}

%>
<tr bgcolor="#FFFFFF">
  <td height="19"><%out.println(rs.getString(1));%> </td>
  <td><%out.println(rs.getString(2));%> </td>
  <td><%out.println(rs.getString(4));%> </td>
  <td><%= str %> </td>
  <td><%=description%> </td>
</tr>
<%
}
%> 
</table>
<%
rs.close();
stmt.close();
conn.close();
/*
}
catch(Exception e){
out.println("Error"+e);
}
*/
%>
</body>
</html><%@ page contentType="text/html; charset=gb2312" language="java" %>
<%@ page import="java.util.*,java.lang.*,java.sql.*,java.io.*"%>
<%@ page import="oracle.sql.*,webutil.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Oracle Operation</title>
<style type="text/css">
<!--
td {
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
color: #000000;
text-decoration: none;
}
-->
</style>
</head>
<body>
<%
java.sql.Connection conn; //資料庫連接對象\r
java.lang.String strCon; //資料庫連接字串
StringBuffer sbf;
sbf= new StringBuffer();

strCon ="jdbc:oracle:thin:@localhost:1521:via";
java.lang.String strSQL,strtmp; //SQL語句\r
strtmp="testestsetse";
//try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
conn = java.sql.DriverManager.getConnection(strCon,"kevin","ddd");
Statement stmt=conn.createStatement();
java.sql.PreparedStatement pstmt = null;
ResultSet rs = null;
String query = "",description="";
conn.setAutoCommit(false);

File f=null;
f = new File("I://JSP//Exercise//Oracle//humor.txt");
FileInputStream fi = new FileInputStream(f);
int size = fi.available();
for (int i=0;i<size; i++ )
{
  //System.out.print((char)fi.read());
  sbf.append((char)fi.read());
}
fi.close();

query = "insert into tblproduct(proid,prodetail,productname) values(?,empty_clob(),?)";
pstmt = conn.prepareStatement(query);
pstmt.setInt(1,68);
pstmt.setString(2,"gg");
pstmt.executeUpdate();
pstmt = null;
query = "select prodetail from tblproduct where proid = 68 for update";
pstmt = conn.prepareStatement(query);
rs= pstmt.executeQuery();

CLOB clobtt = null;
if(rs.next()){
oracle.jdbc.driver.OracleResultSet ors = (oracle.jdbc.driver.OracleResultSet)rs;
  clobtt = (CLOB)ors.getClob(1);

}

/* //向CLOB對象中寫入資料
BufferedWriter bw = new BufferedWriter(clobtt.getCharacterOutputStream());
BufferedReader in = new BufferedReader(new FileReader("I://JSP//Exercise//Oracle//humor.txt"));
int c;
while ((c=in.read())!=-1) {
bw.write(c);
}
in.close();
bw.close();
*/

Writer wr = clobtt.getCharacterOutputStream();
String strSbf = new String(sbf.toString().getBytes("ISO8859_1"),"gb2312");

wr.write(WebUtil.htmlEncode(strSbf));
wr.flush();
wr.close();

rs.close();
conn.commit();
//out.print(WebUtil.htmlEncode(sbf.toString()));
%>
<table width="770" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#e7e7e7">

<tr bgcolor="#FFFFFF">
  <td height="19"> </td>
  <td> </td>
  <td> </td>
  <td> </td>
  <td> </td>
</tr>

</table>
<%
/*
}
catch(Exception e){
out.println("Error"+e);
}
*/
%>
</body>
</html>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.