怎样把Connection对象转换成OracleConnection对象(100分)

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

ncyqhhf

Unregistered / Unconfirmed
GUEST, unregistred user!
[?]我想把Connection对象转换成OracleConnection对象,这样后面的事情才好做,我写的代码如下:
<div align="center">ORACLE大文本的保存</div>
<% //初始化
conn = dbtool.connectDatabase("ok");
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
sql = "select * from hhf";
resultSet = stmt.executeQuery(sql);
//保存大文本

conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:hhfora","maximo","maximo");

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);
if (rset.next()) blob = rset.getBlob("blob");
filename = "d://test.doc";
File f = new File(filename);
FileInputStream fin = new FileInputStream(f);
System.out.println("file size = " + fin.available());
pstmt = conn.prepareStatement("update hhf set blob=? where code='66666'");
******************
//OutputStream outhhf = blob.getBinaryOutputStream();
若用connection对象来连接数据,函数getBinaryOutputStream()就不能用,
用OracleConnection连接数据库,函数getBinaryOutputStream()就能同,
我不知道怎样把Connection对象连接转换成OracleConnection对象连接,望大 家能给我帮助,谢谢大家!!
*****************
//byte[] data = new byte[(int)fin.available()];
//fin.read(data);
//outhhf.write(data);
fin.close();
//outhhf.close();
//pstmt.setBlob(2,blob);
pstmt.setString(1,"66666");
pstmt.executeUpdate();
pstmt.close();
conn.commit();
conn.close();


%>
 
不用转啊
你是要把 文件写到blob的字段里啊
File f = new File(filename);
FileInputStream fin = new FileInputStream(f);

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();
就可以了

 
谢谢! 我试试看了, 成功了就给你加分 。我是个初学者,以后希望多多帮助![:D]
 
[?]我测试了没有问题呀 ,可没有把大文本数据保存到数据库你呀?
 
[:)]我修改后的代码如下,就是没有把大文本保存到数据库,你帮我看看,在下多谢了:
<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>
 
我怀疑是这句话有问题(因为在数据中,我的大文本字段是第2个字段):
pstmt.setBinaryStream(1,kk,(int)fin.available());
可我改成:
pstmt.setBinaryStream(2,kk,(int)fin.available());
后就出现这样的错误,请帮我看看,多谢了!!!
异常为:- 索引中丢失 IN 或 OUT 参数:: 1
java.sql.SQLException: 索引中丢失 IN 或 OUT 参数:: 1
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:168)
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:210)
at oracle.jdbc.driver.OracleStatement.checkBindsInAndOut(OracleStatement.java:1323)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1842)
at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:363)
at jsp_servlet._jsp._app._xqltj.__main._jspService(__main.java:503)
at weblogic.servlet.jsp.JspBase.service(JspBase.java:27)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:265)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:200)
at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:2495)
at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2204)
at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:139)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:120)
 
[?]请帮我再解决后面这个问题怎么样
 
后退
顶部