用JAVA写入和读取MYSQL的图片的实例
2009-12-26 04:15
781 查看
主要的程序:
package greeds.jdbc.sample;
import greeds.jdbc.util.JDBCUtil;
import java.io.*;
import java.sql.*;
public class MySQLBlobSample {
public static void main(String[] args) throws Exception {
// 写入数据库
/*
*
* Connection conn = null; PreparedStatement pstm = null;
*
* try { String filepath = "D://strahovski-ep201-011.jpg"; File file =
* new File(filepath); FileInputStream fin = new FileInputStream(file);
* conn = JDBCUtil.getConnection();
*
* String sql = "insert into Dish(id,photo) values(?,?)"; pstm =
* conn.prepareStatement(sql); pstm.setInt(1, 3);
* pstm.setBinaryStream(2, fin,(int)file.length());
*
* int r = pstm.executeUpdate(); System.out.println("INSERT"+r+"ROW");
*
* fin.close();
*
*
* } catch (FileNotFoundException e) { e.printStackTrace(); } finally{
* //JDBCUtil.release(null, pstm, conn); }
*/
// 从数据库读取文件
Connection conn = null;
Statement stm = null;
ResultSet rs = null;
try {
conn = JDBCUtil.getConnection();
stm = conn.createStatement();
String sql = "select * from Dish where id=1";
rs = stm.executeQuery(sql);
rs.next();
Blob blob = rs.getBlob("photo");
FileOutputStream out = new FileOutputStream(new File("E:/my.jpg"));
InputStream in = blob.getBinaryStream();
int i;
while ((i = in.read()) != -1) {
out.write(i);
}
in.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtil.release(rs, stm, conn);
}
}
}
JDBCUtil.java程序
package greeds.jdbc.util;
import java.sql.*;
import java.io.InputStream;
import java.util.Properties;
public class JDBCUtil {
private static Properties env;
static {
try {
env = new Properties();
InputStream is = JDBCUtil.class
.getResourceAsStream("../config/conn.properties");
env.load(is);
is.close();
} catch (Exception e) {
e.printStackTrace();
throw new ExceptionInInitializerError(e);
}
}
public static final ThreadLocal<Connection>tl = new ThreadLocal<Connection>();
public static Connection getConnection() throws Exception {
Connection conn = tl.get();
if (conn == null) {
Class.forName(env.getProperty("driver"));
conn = DriverManager.getConnection(env.getProperty("url"), env
.getProperty("username"), env.getProperty("password"));
tl.set(conn);
}
return conn;
}
public static void release(ResultSet rs, Statement stm, Connection conn) {
if (rs != null)
try {
rs.close();
} catch (Exception e) {
}
if (stm != null)
try {
stm.close();
} catch (Exception e) {
}
if (conn != null)
try {
conn.close();
} catch (Exception e) {
}
}
}
配置文件 conn.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/greedsdata?useUnicode=true&characterEncoding=utf-8
username=caps
password=caps
不能写入成功的主要原因是 红色字体处。
package greeds.jdbc.sample;
import greeds.jdbc.util.JDBCUtil;
import java.io.*;
import java.sql.*;
public class MySQLBlobSample {
public static void main(String[] args) throws Exception {
// 写入数据库
/*
*
* Connection conn = null; PreparedStatement pstm = null;
*
* try { String filepath = "D://strahovski-ep201-011.jpg"; File file =
* new File(filepath); FileInputStream fin = new FileInputStream(file);
* conn = JDBCUtil.getConnection();
*
* String sql = "insert into Dish(id,photo) values(?,?)"; pstm =
* conn.prepareStatement(sql); pstm.setInt(1, 3);
* pstm.setBinaryStream(2, fin,(int)file.length());
*
* int r = pstm.executeUpdate(); System.out.println("INSERT"+r+"ROW");
*
* fin.close();
*
*
* } catch (FileNotFoundException e) { e.printStackTrace(); } finally{
* //JDBCUtil.release(null, pstm, conn); }
*/
// 从数据库读取文件
Connection conn = null;
Statement stm = null;
ResultSet rs = null;
try {
conn = JDBCUtil.getConnection();
stm = conn.createStatement();
String sql = "select * from Dish where id=1";
rs = stm.executeQuery(sql);
rs.next();
Blob blob = rs.getBlob("photo");
FileOutputStream out = new FileOutputStream(new File("E:/my.jpg"));
InputStream in = blob.getBinaryStream();
int i;
while ((i = in.read()) != -1) {
out.write(i);
}
in.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtil.release(rs, stm, conn);
}
}
}
JDBCUtil.java程序
package greeds.jdbc.util;
import java.sql.*;
import java.io.InputStream;
import java.util.Properties;
public class JDBCUtil {
private static Properties env;
static {
try {
env = new Properties();
InputStream is = JDBCUtil.class
.getResourceAsStream("../config/conn.properties");
env.load(is);
is.close();
} catch (Exception e) {
e.printStackTrace();
throw new ExceptionInInitializerError(e);
}
}
public static final ThreadLocal<Connection>tl = new ThreadLocal<Connection>();
public static Connection getConnection() throws Exception {
Connection conn = tl.get();
if (conn == null) {
Class.forName(env.getProperty("driver"));
conn = DriverManager.getConnection(env.getProperty("url"), env
.getProperty("username"), env.getProperty("password"));
tl.set(conn);
}
return conn;
}
public static void release(ResultSet rs, Statement stm, Connection conn) {
if (rs != null)
try {
rs.close();
} catch (Exception e) {
}
if (stm != null)
try {
stm.close();
} catch (Exception e) {
}
if (conn != null)
try {
conn.close();
} catch (Exception e) {
}
}
}
配置文件 conn.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/greedsdata?useUnicode=true&characterEncoding=utf-8
username=caps
password=caps
不能写入成功的主要原因是 红色字体处。
相关文章推荐
- Java+MySql图片数据保存与读取的具体实例
- Java Swing中读取/写入图片到MySQL中类型转换
- Java+MySql图片数据保存与读取的具体实例
- java+mysql实现保存图片到数据库,以及读取数据库存储的图片
- java通过InputStream读取和写入文件操作实例代码
- Java实现用Mysql存取图片操作实例
- Java 连接 MySQL读取 Blob 生成图片
- java读取mysql中保存的图片并显示在jsp上
- Java读取与写入图片文件
- 【java】mysql+springMvc+easyui实现图片的保存和读取显示
- java操作mysql存储读取图片
- Java读取网页内容并下载图片的实例
- java--File的读取和写入实例
- Java+MySql图片数据保存与读取的具体实例
- java+mysql实现保存图片到数据库,以及读取数据库存储的图片
- Java对象在Mysql的写入读取
- java+mysql实现保存图片到数据库,以及读取数据库存储的图片
- Java从网络读取图片并保存至本地实例
- Java按行读取正在被动态写入的大文件实例--使用RandomAccessFile(1)
- Java中IO流文件读取、写入和复制的实例