处理大数据对象 -处理BLOB数据
import java.sql.* ;
import java.io.File ;
import java.io.FileInputStream ;
import java.io.InputStream ;
public class BlobDemo01
{
//定义数据库驱动程序
public static final String driver = "org.gjt.mm.mysql.Driver" ;
//定义数据库连接地址
public static final String url = "jdbc:mysql://localhost:3306/spiderman" ;
//定义数据库名称
public static final String user = "root" ;
//定义数据库密码
public static final String pass = "" ;
public static void main(String[] args)
{ //加载数据库驱动程序
try
{
Class.forName(driver) ;
}
catch(ClassNotFoundException e)//抛出异常
{
e.printStackTrace() ;
return ;
}
//加载数据库连接
Connection conn = null ;
try
{
conn = DriverManager.getConnection(url,user,pass);
}
catch(SQLException e)
{
e.printStackTrace() ;
return ;
}
//执行数据库操作
PreparedStatement pst = null ;
String name = "IronMan";
String sql = "Insert Into userblob(user,photo)values(?,?) ";
File f = new File("f:"+File.separator+"shabi.jpg") ; //指定文件路径
InputStream input = null ;
try
{
input = new FileInputStream(f) ;
}
catch (Exception e)
{
e.printStackTrace() ;
}
try
{
pst = conn.prepareStatement(sql);//实例化Statement对象
pst.setString(1,name) ; //设置第一个"?"内容
pst.setBinaryStream(2,input) ; //设置输入流
pst.executeUpdate();
}
catch(SQLException e)
{
e.printStackTrace();
return ;
}
finally
{
if(pst!=null)
{
try
{
pst.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
if(conn!=null)
{
try
{
conn.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}
}
}
import java.sql.* ;
import java.io.File ;
import java.io.FileInputStream ;
import java.io.InputStream ;
import java.Blob ;
import java.sql.ResultSet ;
import java.io.FileOutputStream ;
import java.io.OutputStream ;
public class StatementDemo01
{
//定义数据库驱动程序
public static final String driver = "org.gjt.mm.mysql.Driver" ;
//定义数据库连接地址
public static final String url = "jdbc:mysql://localhost:3306/student" ;
//定义数据库名称
public static final String user = "root" ;
//定义数据库密码
public static final String pass = "" ;
public static void main(String[] args)throws Exception
{ //加载数据库驱动程序
try
{
Class.forName(driver) ;
}
catch(ClassNotFoundException e)//抛出异常
{
e.printStackTrace() ;
return ;
}
//加载数据库连接
Connection conn = null ;
try
{
conn = DriverManager.getConnection(url,user,pass);
}
catch(SQLException e)
{
e.printStackTrace() ;
return ;
}
//执行数据库操作
PreparedStatement pst = null ;
ResultSet rs = null ; //保存查询到的内容
int id = 1 ;
String sql = "select user,photo from userblob where id = ? ";
try
{
pat = conn.prepareStatement(sql);//实例化PreparedStatement对象
}
catch(SQLException e)
{
e.printStackTrace();
return ;
}
rs = pst.executeQuery(); //执行查询
if(rs.next())
{
String name = rs.getString(1) ;
System.out.println("姓名"+name) ;
Blob b = rs.getBlob(2) ;
File f = new File("f:"+File.separator+"load.jpg") ;
OutputStream output = null ;
out = new OutputStream(f) ;
out.write(b.getBytes(1,(int)b.length())) ;
out.close() ;
}
finally
{
if(rs!=null)
{
try
{
rs.close();
}
catch (SQLException e)
{
e.printStackTrace() ;
}
}
if(pst!=null)
{
try
{
pst.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
if(conn!=null)
{
try
{
conn.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}
}
}
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: mogilefs
- 下一篇: 网页(HTML)中的特殊字符