有点问题请教大家(100分)

  • 主题发起人 主题发起人 ncyqhhf
  • 开始时间 开始时间
N

ncyqhhf

Unregistered / Unconfirmed
GUEST, unregistred user!
[:)]我修改后的代码如下,就是没有把大文本保存到数据库,帮我看看,在下多谢了:
<font face="Arial, Helvetica, sans-serif"><%@ page language="java" %>
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*,java.util.*,java.text.*" errorPage="" %>
<%@page import="java.io.*"%>
<%@page import="oracle.sql.*"%>
<%@page import="oracle.jdbc.driver.*"%>
<jsp:useBean id = "dbtool" scope = "session" class = "com.bms.pub.db.tools" />
<%
Connection conn = null;
Statement stmt = null;
String sql;
ResultSet resultSet = null;
%>
<html>
<head>
<title>大文本的保存</title>
<meta http-equiv="Content-Type" content="text/html;
charset=gb2312">
<style type="text/css">
<!--
.style1 {
font-size: x-large;
font-weight: bold;
}
a:link {
color: #D4D0C8;
}
.style2 {font-size: large}
body {
background-color: #FFFFFF;
}
-->
</style>
</head>
<body>
<form name="form1" method="post" action="">
<font size="2"> </font>
<table width="100%" height="20" border="0" cellpadding="0" cellspacing="0">
<tr bgcolor="#CCCCCC">

<input name="find" type="submit" id="find" value="统计(T)">
</font></div></th>
</tr>
</table>
</form>
<div align="center">ORACLE大文本的保存kkkkkkkkk</div>
<% //初始化
conn = dbtool.connectDatabase("ok");
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
sql = "select * from hhf";
resultSet = stmt.executeQuery(sql);
//保存大文本
Class.forName("oracle.jdbc.OracleDriver").newInstance();
String connStr="jdbc:oracle:thin:@localhost:1521:hhfora";
String user="maximo";
String password="maximo";
conn = DriverManager.getConnection(connStr,user,password);
conn.setAutoCommit(false);
stmt=conn.createStatement();
conn.setAutoCommit(false);
PreparedStatement pstmt = conn.prepareStatement("insert into hhf(code,blob) values(?,empty_blob())");
pstmt.setString(1,"66666");
pstmt.executeUpdate();
pstmt.close();
pstmt = conn.prepareStatement("select blob from hhf where code= ? for update");
pstmt.setString(1,"66666");
ResultSet rset = pstmt.executeQuery(sql);
String filename = "d://test.doc";
File f = new File(filename);
FileInputStream fin = new FileInputStream(f);
System.out.println("file size = " + fin.available());
byte[] data = new byte[(int)fin.available()];
fin.read(data);

pstmt = conn.prepareStatement("update hhf set blob=? where code='66666'");
ByteArrayInputStream kk = new ByteArrayInputStream(data);
pstmt.setBinaryStream(1, kk,(int)fin.available());
fin.close();
pstmt.executeUpdate();
pstmt.close();
conn.commit();
conn.close();
%>
</body>
</html>
</font>
 
import java.sql.*;
import java.io.*;
import oracle.sql.*;
import oracle.jdbc.*;
public class LongExample1 {
public static void main(String [] args)
throws SQLException, IOException {
// register the Oracle JDBC drivers
DriverManager.registerDriver(
new oracle.jdbc.OracleDriver()
);
// create a Connection object, and connect to the database
// as lob_user using the Oracle JDBC Thin driver
Connection myConnection = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:ORCL",
"lob_user",
"lob_password"
);
// 禁止自動寫入
myConnection.setAutoCommit(false);
String sourceDirectory = "C://sample_files//";
writeLONG(myConnection, sourceDirectory, "textContent.txt");
writeLONGRAW(myConnection, sourceDirectory, "binaryContent.doc");
// 關閉JDBC
myConnection.close();
}
private static void writeLONG(
Connection myConnection,
String sourceDirectory,
String fileName
) throws SQLException, IOException {
// step 1: 建立文件對象
File myFile = new File(sourceDirectory + fileName);
// step 2: 獲得文件長度
int fileLength = (int) myFile.length();
// step 3: 建立輸入對象準備讀取文件內容
InputStream myInputStream = new FileInputStream(myFile);
// step 4: 建立預讀數據語句對象
// to add a row to the long_content table
PreparedStatement myPrepStatement = myConnection.prepareStatement(
"INSERT INTO long_content(file_name, long_column) " +
"VALUES (?, ?)"
);
// step 5: 綁定文件名和輸入流至prepared statement object
myPrepStatement.setString(1, fileName);
myPrepStatement.setAsciiStream(2, myInputStream, fileLength);
// step 6: 執行sql
myPrepStatement.execute();
// step 7: 執行寫入
myConnection.commit();
// step 8: 關閉輸入流和預讀語句
myInputStream.close();
myPrepStatement.close();
System.out.println("Wrote content from file " +
fileName + " to LONG");
}

private static void writeLONGRAW(
Connection myConnection,
String sourceDirectory,
String fileName
) throws SQLException, IOException {
// step 1: 建立文件對象
File myFile = new File(sourceDirectory + fileName);
// step 2: 得到文件長度
int fileLength = (int) myFile.length();
// step 3:
InputStream myInputStream = new FileInputStream(myFile);
// step 4:
PreparedStatement myPrepStatement = myConnection.prepareStatement(
"INSERT INTO long_raw_content(file_name, long_raw_column) " +
"VALUES (?, ?)"
);
// step 5:
myPrepStatement.setString(1, fileName);
myPrepStatement.setBinaryStream(2, myInputStream, fileLength);
// step 6:
myPrepStatement.execute();
// step 7:
myConnection.commit();
// step 8:
myInputStream.close();
myPrepStatement.close();
System.out.println("從文件 " +
fileName + "寫入內容到LONG RAW");
}
}
 
是不是字段类型不对?
 
字段类型是对的 BLOB[:D]
 
[:D][red][/red]按你的方法写我还是保存不了大文本:
最近我在网上看到这样的资料:
Oracle的PreparedStatement类不完全支持BLOB和CLOB等大对象的处理,尤其是Thin驱动程序不支持利用PreparedStatement对象的setObject()和setBinaryStream()方法设置BLOB的值,也不支持利用setCharacterStream()方法设置CLOB的值。只有locator本身中的方法才能够从数据库中获取LOB类型的值。可以使用PreparedStatement对象插入或更新LOB,但需要使用locator才能获取LOB的值。由于存在这二个问题,因此,我建议使用locator的方法来插入、更新或获取LOB的值。
我想是不是这个原因哟,locator的方法怎么做呀,谢谢!!!!
 
我把代码改成同你一样 可还是保存不了大文本呀,帮我再看看 谢谢了
<font face="Arial, Helvetica, sans-serif"><%@ page language="java" %>
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*,java.util.*,java.text.*" errorPage="" %>
<%@page import="java.io.*"%>
<%@page import="oracle.sql.*"%>
<%@page import="oracle.jdbc.*"%>
<jsp:useBean id = "dbtool" scope = "session" class = "com.bms.pub.db.tools" />
<%
Connection conn = null;
Statement stmt = null;
String sql;
ResultSet resultSet = null;
byte[] mFileBody;

%>
<html>
<head>
<title>大文本的保存</title>
<meta http-equiv="Content-Type" content="text/html;
charset=gb2312">
<style type="text/css">
<!--
.style1 {
font-size: x-large;
font-weight: bold;
}
a:link {
color: #D4D0C8;
}
.style2 {font-size: large}
body {
background-color: #FFFFFF;
}
-->
</style>
</head>
<body>
<form name="form1" method="post" action="">
<font size="2"> </font>
<table width="100%" height="20" border="0" cellpadding="0" cellspacing="0">
<tr bgcolor="#CCCCCC">

<input name="find" type="submit" id="find" value="统计(T)">
</font></div></th>
</tr>
</table>
</form>
<div align="center">ORACLE大文本的保存kkkkkkkkk</div>

<% //初始化
//conn = dbtool.connectDatabase("ok");
//stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
//sql = "select * from hhf";
//resultSet = stmt.executeQuery(sql);

//保存大文本
Class.forName("oracle.jdbc.OracleDriver").newInstance();
String connStr="jdbc:oracle:thin:@localhost:1521:maximo";
String user="maximo";
String password="maximo";
conn = DriverManager.getConnection(connStr,user,password);

// 禁止自動寫入
conn.setAutoCommit(false);
// step 1: 建立文件對象
File myFile = new File("c://1234.doc");
// step 2: 獲得文件長度
int fileLength = (int)myFile.length();
// step 3: 建立輸入對象準備讀取文件內容
FileInputStream myInputStream = new FileInputStream(myFile);
mFileBody = new byte[(int)myFile.length()];
myInputStream.read(mFileBody,0,(int)myFile.length());
// step 4: 建立預讀數據語句對象
// to add a row to the long_content table
PreparedStatement myPrepStatement = conn.prepareStatement("insert into hhf(code,blob) values(?,?)");
// step 5: 綁定文件名和輸入流至prepared statement object
myPrepStatement.setString(1,"123456");
myPrepStatement.setBinaryStream(2,myInputStream,myInputStream.available());
//step 6: 執行sql
//myPrepStatement.executeUpdate();
myPrepStatement.execute();
// step 7: 執行寫入
conn.commit();
// step 8: 關閉輸入流和預讀語句
myInputStream.close();
myPrepStatement.close();

//System.out.println("Wrote content from file " + "21 to LONG" + myInputStream + " "+ fileLength + " = "+ myInputStream.available());

// 關閉JDBC
conn.close();

%>



</body>
</html>
</font>
 
[:D]
我已经搞定啦 谢谢各位
 
后退
顶部