|
|
|
| 您的位置:首页>>JDBC>>java操纵SQL Server2000大型对象 |
|
|
java操纵SQL Server2000大型对象
|
| 2005-07-23 来源:CSDN 作者:CSDN |
大家或许已经看到了,我前面的一篇文章 其实前面的文章是有限制的 因为String 类型能够接收的字节数相当有限,所以 一旦文本或者图象较大则是无法全部存与数据库中 相应的从数据库取出来也是不完全的了
本程序修正了上面的程序,热烈推荐大家看我在上遍文章参考的那本书!
import java.io.*; import java.sql.*;
public class Db { public static void main(String[] args) {
Db ac = new Db();
String blobname = "D:\\test1.jpg"; //blob文件名
String in = "insert into ";
String in1 = "(id,data) values(´0012´,?)";
String tablename = "Ss";
String sqlstr = ""; // sql 语句 sqlstr = in + tablename + in1;
ThreadUseExtends thread = new ThreadUseExtends(blobname,sqlstr); thread.insert(); thread.getCover();
} }
class ThreadUseExtends { String filename1;//blob filename
String str; //ReadFiles r1 = new ReadFiles();
//构造函数要有(blob文件名,clob文件名,sql语句) public ThreadUseExtends(String name1,String sqlstr) {
filename1 = name1; str = sqlstr; System.out.println("I carry out this"); } public void insert() { try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url = "jdbc:odbc:LW";
Connection con = DriverManager.getConnection(url); //String testLong = r1.ReadFile(filename1); File file=new File(filename1); InputStream fin=new FileInputStream(file); int fileLength=(int)file.length();
//byte[] ba = testLong.getBytes(); System.out.println("str=" + str);
PreparedStatement stm = con.prepareStatement(str);
stm.setBinaryStream(1,fin,fileLength);
stm.execute(); stm.close(); con.close(); } catch (Exception e) { e.printStackTrace(); }
} //本函数用于把数据库的内容从数据库中读出,并保存在文件中。 public void getCover() { InputStream in=null; Connection cn = null; PreparedStatement pst = null; byte buf[]=new byte[50000]; DataOutputStream output=null; int size; System.out.println("have carry out this"); try { output=new DataOutputStream(new FileOutputStream("D:\\test2.jpg")); } catch(IOException e) { System.err.println("File not opened \n"+e.toString()); System.exit(1); } try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url = "jdbc:odbc:LW"; cn = DriverManager.getConnection(url); pst=cn.prepareStatement("SELECT data FROM Ss where ID =´0012´"); // pst.setString(1,id); ResultSet rs=pst.executeQuery(); while(rs.next()) { in=rs.getBinaryStream("data"); while ((size=in.read(buf,0,50000))!=-1) { output.write(buf,0,size); } } } catch (SQLException sqle) { System.err.println("Error in CoverServlet : getCover()-" + sqle); sqle.printStackTrace() ; } catch (Exception e) { e.printStackTrace(); }
finally { try { pst.close() ; cn.close() ; } catch (Exception e) { e.printStackTrace(); } }
}
}//ThreadUseExtends class
|
|
|
|