当前位置: 技术问答>java相关
走过不如路过:高分求助
来源: 互联网 发布时间:2015-08-27
本文导语: 各位高手: 以下是我的一段程序:请问各位大虾帮我找出错误: 表结构:(oracle) CREATE TABLE SYSTEM.HRMACH_CONTRACT_PHOTO ( ARCHIVE_CD VARCHAR2(11) NOT NULL, PHOTO ...
各位高手:
以下是我的一段程序:请问各位大虾帮我找出错误:
表结构:(oracle)
CREATE TABLE SYSTEM.HRMACH_CONTRACT_PHOTO
(
ARCHIVE_CD VARCHAR2(11) NOT NULL,
PHOTO BLOB,
CONSTRAINT PK_HRMACH_CONTRACT_PHOTO PRIMARY KEY (ARCHIVE_CD)
)
程序
import oracle.jdbc.driver.*;
import java.sql.*;
import java.io.*;
public static void main(String[] args)
{
try
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn =
DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.168:1521:imart ","system","manager");
PreparedStatement ps = conn.prepareStatement("INSERT INTO HRMACH_CONTRACT_PHOTO(ARCHIVE_CD,PHOTO) values (?,?)");
ps.setString(1,"111111134");
File inFile = new File("c:\5.jpg");
int length = (int)inFile.length() ;
FileInputStream inFileStream =new FileInputStream(inFile);
ps.setBinaryStream(2,inFileStream,length);
ps.execute();
conn.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
结果能把数据存进库里,但报出如下错误:
java.sql.SQLException: Io 异常: Connection reset by peer: socket write error
但我看大家好像都是这么做的,但我死活都报错,我希望各位各位高手在自己的机器上能
调一下它,因我上一次也提出此问题,没有给出我满意的答案。到时我将200分相让
(那个贴叫"高分求助:在oracle中保存图片问题",希望到那报一下名好给分)
以下是我的一段程序:请问各位大虾帮我找出错误:
表结构:(oracle)
CREATE TABLE SYSTEM.HRMACH_CONTRACT_PHOTO
(
ARCHIVE_CD VARCHAR2(11) NOT NULL,
PHOTO BLOB,
CONSTRAINT PK_HRMACH_CONTRACT_PHOTO PRIMARY KEY (ARCHIVE_CD)
)
程序
import oracle.jdbc.driver.*;
import java.sql.*;
import java.io.*;
public static void main(String[] args)
{
try
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn =
DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.168:1521:imart ","system","manager");
PreparedStatement ps = conn.prepareStatement("INSERT INTO HRMACH_CONTRACT_PHOTO(ARCHIVE_CD,PHOTO) values (?,?)");
ps.setString(1,"111111134");
File inFile = new File("c:\5.jpg");
int length = (int)inFile.length() ;
FileInputStream inFileStream =new FileInputStream(inFile);
ps.setBinaryStream(2,inFileStream,length);
ps.execute();
conn.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
结果能把数据存进库里,但报出如下错误:
java.sql.SQLException: Io 异常: Connection reset by peer: socket write error
但我看大家好像都是这么做的,但我死活都报错,我希望各位各位高手在自己的机器上能
调一下它,因我上一次也提出此问题,没有给出我满意的答案。到时我将200分相让
(那个贴叫"高分求助:在oracle中保存图片问题",希望到那报一下名好给分)
|
我给你一个我使用好用的;
你最好最一个数据库连接类,然后在需要时创建这个连接类,或者继承。
package jp.co.nec.JTC;
import java.sql.*;
import java.util.*;
import java.io.*;
public class JTCDBConnectionF implements Serializable{
/* 僐僱僋僔儑儞僆僽僕僃僋僩 */
protected Connection con = null;
/* 僐儞僗僩儔僋僞 */
public JTCDBConnectionF() {
}
public void dbOpen() throws java.lang.Exception{
if (con == null || con.isClosed()) {
String url = "jdbc:oracle:oci8:@apsv";
String user = "scott";
String pass = "tiger";
// 僪儔僀僶偺儘乕僪
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
// 俢俛偵愙懕
con = DriverManager.getConnection(url,user,pass);
// 僩儔儞僓僋僔儑儞僞僀僾偺愝掕
con.setTransactionIsolation( con.TRANSACTION_READ_COMMITTED );
con.setAutoCommit(false);
System.out.println("DB OPEN");
} else {
System.out.println("OPENED DB");
}
}
public JTCerrorF dbClose() {
try {
if (con != null && !con.isClosed()) {
//儘乕儖僶僢僋(惓忢廔椆帪偼close偡傞慜偵忋埵偱僐儈僢僩偡傞偙偲)
con.rollback();
con.close();
System.out.println("DB CLOSE");
} else {
System.out.println("CLOSED DB");
}
}
catch (SQLException se) {
// return new JTCerrorF(se.getErrorCode(),se);
return new JTCerrorF("PD05",se); //DB CLOSE
}
finally {
con = null;
}
return (JTCerrorF)null;
}
}
你看看这个好用吗,
这是我以前项目中用的。
你最好最一个数据库连接类,然后在需要时创建这个连接类,或者继承。
package jp.co.nec.JTC;
import java.sql.*;
import java.util.*;
import java.io.*;
public class JTCDBConnectionF implements Serializable{
/* 僐僱僋僔儑儞僆僽僕僃僋僩 */
protected Connection con = null;
/* 僐儞僗僩儔僋僞 */
public JTCDBConnectionF() {
}
public void dbOpen() throws java.lang.Exception{
if (con == null || con.isClosed()) {
String url = "jdbc:oracle:oci8:@apsv";
String user = "scott";
String pass = "tiger";
// 僪儔僀僶偺儘乕僪
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
// 俢俛偵愙懕
con = DriverManager.getConnection(url,user,pass);
// 僩儔儞僓僋僔儑儞僞僀僾偺愝掕
con.setTransactionIsolation( con.TRANSACTION_READ_COMMITTED );
con.setAutoCommit(false);
System.out.println("DB OPEN");
} else {
System.out.println("OPENED DB");
}
}
public JTCerrorF dbClose() {
try {
if (con != null && !con.isClosed()) {
//儘乕儖僶僢僋(惓忢廔椆帪偼close偡傞慜偵忋埵偱僐儈僢僩偡傞偙偲)
con.rollback();
con.close();
System.out.println("DB CLOSE");
} else {
System.out.println("CLOSED DB");
}
}
catch (SQLException se) {
// return new JTCerrorF(se.getErrorCode(),se);
return new JTCerrorF("PD05",se); //DB CLOSE
}
finally {
con = null;
}
return (JTCerrorF)null;
}
}
你看看这个好用吗,
这是我以前项目中用的。
|
import oracle.jdbc.driver.*;
import java.sql.*;
import java.io.*;
public static void main(String[] args)
{
try
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn =
DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.168:1521:imart ","system","manager");
PreparedStatement ps = conn.prepareStatement("INSERT INTO HRMACH_CONTRACT_PHOTO(ARCHIVE_CD,PHOTO) values (?,?)");
ps.setString(1,"111111134");
File inFile = new File("c:\5.jpg");
int length = (int)inFile.length() ;
FileInputStream inFileStream =new FileInputStream(inFile);
ps.setBinaryStream(2,inFileStream,length);
ps.execute();
ps.close();//关闭ps
conn.close();
}
catch (Exception e)
{
e.printStackTrace();
}finally {
conn = null;
}
}
import java.sql.*;
import java.io.*;
public static void main(String[] args)
{
try
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn =
DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.168:1521:imart ","system","manager");
PreparedStatement ps = conn.prepareStatement("INSERT INTO HRMACH_CONTRACT_PHOTO(ARCHIVE_CD,PHOTO) values (?,?)");
ps.setString(1,"111111134");
File inFile = new File("c:\5.jpg");
int length = (int)inFile.length() ;
FileInputStream inFileStream =new FileInputStream(inFile);
ps.setBinaryStream(2,inFileStream,length);
ps.execute();
ps.close();//关闭ps
conn.close();
}
catch (Exception e)
{
e.printStackTrace();
}finally {
conn = null;
}
}