|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?注册
x
package com.qhit;
import java.sql.*;
public class OracleConn {
/** * @param args */ public static void main(String[] args) {
//设置连接url String url = "jdbcracle:thinlocalhost:1521:xmrc";
//用户名 String user = "scott";
//密码 String password="tiger";
Connection conn = null; java.sql.Statement st = null; java.sql.PreparedStatement pst = null; java.sql.ResultSet rs = null; try{
//实例化oracle驱动,注意要把驱动包导入进来,驱动包在oracle中可以找到,或者到网上下都可以 new oracle.jdbc.driver.OracleDriver();
//使用驱动管理器和url,用户名,密码,连接数据库得到连接conn
conn = java.sql.DriverManager.getConnection(url, user, password);
//取消自动提交,默认的时候都是自动提交的
conn.setAutoCommit(false);
st = conn.createStatement();
//测试数据库的连接有没有成功
/*String sql = "select * from emp";
rs = st.executeQuery(sql);
while(rs.next()){
int c = rs.getMetaData().getColumnCount();
for(int i=0;i<c;i ){
System.out.print(rs.getString(i 1) "--------");
}
System.out.println();
}*/
//插入图片到数据库
/*
[1] [2]
String sql = "insert into image_tab(id,image) values(1,empty_blob())";
st = conn.createStatement();
st.executeUpdate(sql);
String sql = "select image from image_tab where id = 1 for update";
rs = st.executeQuery(sql);
if(rs.next()){ oracle.sql.BLOB b = (oracle.sql.BLOB)rs.getBlob(1);
java.io.OutputStream os = b.getBinaryOutputStream();
java.io.InputStream is = new java.io.FileInputStream("D:\\样品.jpg");
int i = 0;
while((i = is.read()) != -1){
os.write(i);
}
is.close();
os.close();
}*/
//从数据库里检索出图片
String sql = "select image from image_tab where id=1";
rs = st.executeQuery(sql);
if(rs.next()){
oracle.sql.BLOB b = (oracle.sql.BLOB)rs.getBlob(1);
java.io.InputStream is = b.getBinaryStream();
java.io.FileOutputStream fos = new java.io.FileOutputStream("E:\\test.jpg");
int i = 0;
while((i = is.read())!= -1){
fos.write(i);
}
fos.close();
is.close();
}
conn.commit();
}catch(Exception e){
try{
conn.rollback();
}catch(Exception e1){
e1.printStackTrace();
}
e.printStackTrace();
}finally{
try{
if(rs != null)rs.close();
if(st != null)st.close();
if(pst != null)pst.close();
if(conn != null)conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
上一页 [1] [2] |
|