我的代码如下:
package CRH;import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.io.*;public class i { static public java.sql.Connection conn = null; static public Statement stmt = null; static public ResultSet rs = null;
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("加载成功");
} catch (java.lang.ClassNotFoundException e) {
System.out.println("加载失败错误");
}
try { conn = DriverManager.getConnection(
"jdbc:oracle:thin:@yoyo0708:1521:CRHHMS", "crh",
"crh");
System.out.println("连接数据库成功"); } catch (Exception r) {
System.out.println(r.getMessage() + " 连接错误");
}
String infile="c:\1.bmp";
boolean defaultCommit = conn.getAutoCommit();
conn.setAutoCommit(false);
try {
/* 插入一个空的BLOB对象 */
stmt.executeUpdate("INSERT INTO crh.dd VALUES ('01', EMPTY_BLOB())");
/* 查询此BLOB对象并锁定 */
ResultSet rs = stmt.executeQuery("SELECT BMP FROM crh.dd WHERE ID='01' FOR UPDATE");
while (rs.next()) {
/* 取出此BLOB对象 */
oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("BMP");
/* 向BLOB对象中写入数据 */
BufferedOutputStream out = new BufferedOutputStream(blob.getBinaryOutputStream());
BufferedInputStream in = new BufferedInputStream(new FileInputStream(infile));
int c;
while ((c=in.read())!=-1) {
out.write(c);
}
in.close();
out.close();
}
/* 正式提交 */
conn.commit();
} catch (Exception ex) {
/* 出错回滚 */
conn.rollback();
throw ex;
}
/* 恢复原提交状态 */
conn.setAutoCommit(defaultCommit);
}
}
每次运行的时候都是提示:
加载成功
连接数据库成功
Exception in thread "main" java.lang.NullPointerException
at CRH.i.main(i.java:44)
可是在SQL*Plus下运行出错那行的SQL语句完全没有错误,请大家帮我看一下,怎么改能让程序插入大对象,谢谢!!!
package CRH;import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.io.*;public class i { static public java.sql.Connection conn = null; static public Statement stmt = null; static public ResultSet rs = null;
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("加载成功");
} catch (java.lang.ClassNotFoundException e) {
System.out.println("加载失败错误");
}
try { conn = DriverManager.getConnection(
"jdbc:oracle:thin:@yoyo0708:1521:CRHHMS", "crh",
"crh");
System.out.println("连接数据库成功"); } catch (Exception r) {
System.out.println(r.getMessage() + " 连接错误");
}
String infile="c:\1.bmp";
boolean defaultCommit = conn.getAutoCommit();
conn.setAutoCommit(false);
try {
/* 插入一个空的BLOB对象 */
stmt.executeUpdate("INSERT INTO crh.dd VALUES ('01', EMPTY_BLOB())");
/* 查询此BLOB对象并锁定 */
ResultSet rs = stmt.executeQuery("SELECT BMP FROM crh.dd WHERE ID='01' FOR UPDATE");
while (rs.next()) {
/* 取出此BLOB对象 */
oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("BMP");
/* 向BLOB对象中写入数据 */
BufferedOutputStream out = new BufferedOutputStream(blob.getBinaryOutputStream());
BufferedInputStream in = new BufferedInputStream(new FileInputStream(infile));
int c;
while ((c=in.read())!=-1) {
out.write(c);
}
in.close();
out.close();
}
/* 正式提交 */
conn.commit();
} catch (Exception ex) {
/* 出错回滚 */
conn.rollback();
throw ex;
}
/* 恢复原提交状态 */
conn.setAutoCommit(defaultCommit);
}
}
每次运行的时候都是提示:
加载成功
连接数据库成功
Exception in thread "main" java.lang.NullPointerException
at CRH.i.main(i.java:44)
可是在SQL*Plus下运行出错那行的SQL语句完全没有错误,请大家帮我看一下,怎么改能让程序插入大对象,谢谢!!!
int c声明的时候就要赋值为0,int c = 0;这里不对
int c = 0;//实际读取长度
byte[] buffer = new buffer[1024];//缓冲数组,每次都读取到这里面,一次读取最多1M
while ((c=in.read(buffer))!=-1) { //读取数据到buffer数组,c是实际读取的长度
out.write(buffer,0,c); //每次从buffer数组中向输出流写0到实际读取长度个byte
}
in.close();
out.close();