jdbc 使用PreparedStatement来存储和读取大数据(Blob或Clob)
2013-05-26 00:00
555 查看
//假设这里建立的数据库位use,而建立的表位luser
import java.io.*;
import java.sql.*;
public class DBB{
private static String driver ="com.mysql.jdbc.driver";
private static String url ="jdbc:mysql://localhost:3306/use";
private static String user="root";
private static String pass="admin";
public static void main(String[]args){
Connection con= null;
PreparedStatement sta= null;
ResultSet res= null;
try{
//获取数据源
//假设就是从d盘中读取的一张照片;
File file= new File("d:" + File.separator + "photo.jpg");
int length= (int)file.length();
InputStream input = new FileInputStream(file);
//加载mysql驱动
Class.forName(driver);
//取得链接对象
con= DriverManager.getConnection(url,user,pass);
//取得操作对象
sta= con.prepareStatement("insert into luser values(?,?,?);");
//加载数据
sta.setInt(1,110);
sta.setString(2,"namefile");
sta.setBinaryStream(3,input ,length);
sta.executeUpdate();
sta.clearParameters();
input.close();//释放资源;
}catch(SQLException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}catch(ClassNotFoundException e){
e.printStackTrace();
}
finally{
if(con!=null){
try{
con.close();
}catch(SQLException e){ //(_)%^&**(_)(&*)(*#%$%^&*(()__)(((
e.printStackTrace();
}
}
}
Statement stat= null;
try{
//数据的取得
stat= con.createStatement();
stat.executeQuery("select* from luser;");
res.next();
String filename= res.getString(2);
Blob blob= res.getBlob(3);
//把获得的数据写到指定的文件中
FileOutputStream out= new FileOutputStream("d:" + File.separator+ "dong" +".bak");
out.write(blob.getBytes(1,(int)blob.length()));
out.flush();//把内存中的东西刷出来
out.close();//释放资源
}catch(SQLException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
finally{
if(stat!= null){
try{
stat.close();
}catch(SQLException e){
e.printStackTrace();
}
}
if(con!=null){
try{
con.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
}
import java.io.*;
import java.sql.*;
public class DBB{
private static String driver ="com.mysql.jdbc.driver";
private static String url ="jdbc:mysql://localhost:3306/use";
private static String user="root";
private static String pass="admin";
public static void main(String[]args){
Connection con= null;
PreparedStatement sta= null;
ResultSet res= null;
try{
//获取数据源
//假设就是从d盘中读取的一张照片;
File file= new File("d:" + File.separator + "photo.jpg");
int length= (int)file.length();
InputStream input = new FileInputStream(file);
//加载mysql驱动
Class.forName(driver);
//取得链接对象
con= DriverManager.getConnection(url,user,pass);
//取得操作对象
sta= con.prepareStatement("insert into luser values(?,?,?);");
//加载数据
sta.setInt(1,110);
sta.setString(2,"namefile");
sta.setBinaryStream(3,input ,length);
sta.executeUpdate();
sta.clearParameters();
input.close();//释放资源;
}catch(SQLException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}catch(ClassNotFoundException e){
e.printStackTrace();
}
finally{
if(con!=null){
try{
con.close();
}catch(SQLException e){ //(_)%^&**(_)(&*)(*#%$%^&*(()__)(((
e.printStackTrace();
}
}
}
Statement stat= null;
try{
//数据的取得
stat= con.createStatement();
stat.executeQuery("select* from luser;");
res.next();
String filename= res.getString(2);
Blob blob= res.getBlob(3);
//把获得的数据写到指定的文件中
FileOutputStream out= new FileOutputStream("d:" + File.separator+ "dong" +".bak");
out.write(blob.getBytes(1,(int)blob.length()));
out.flush();//把内存中的东西刷出来
out.close();//释放资源
}catch(SQLException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
finally{
if(stat!= null){
try{
stat.close();
}catch(SQLException e){
e.printStackTrace();
}
}
if(con!=null){
try{
con.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
}
相关文章推荐
- jdbc 使用PreparedStatement来存储和读取大数据(Blob或Clob)
- 使用Derby读取大数据(BLOB、CLOB)的问题
- 使用JDBC和Hibernate在oracle中插入和读取Blob数据的图片的java程序
- 通过JDBC向数据库中存储&读取Blob数据
- Spring JDBC-使用Spring JDBC获取本地连接对象以及操作BLOB/CLOB类型数据
- 使用SQLite3存储和读取数据
- 在VC下采用ADO实现BLOB(Binary)数据的存储,读取,修改,删除。
- 使用文件进行数据存储及读取文件内容
- spark中通过jdbc读取和存储数据
- oracle blob数据类型存储读取的小例子
- 使用SQLite3存储和读取数据
- 数据库SQLite 数据大量存储 和快速读取 链表查询的优化 使用gosn
- 使用Hibernate存取大对象数据类型(clob和blob)
- 使用SQLite3存储和读取数据
- jdbc学习:操作Oracle的CLOB和BLOB数据
- [转] iOS开发24:使用SQLite3存储和读取数据
- 使用SQLite3存储和读取数据
- 使用 hibernate 存取大对象数据类型(clob和blob)
- iOS开发24:使用SQLite3存储和读取数据
- 使用JDBC连接MySQL数据库--典型案例分析(一)---员工数据读取与操作