国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

jdbc網(wǎng)mysql數(shù)據(jù)庫(kù)讀取圖片

2019-11-11 04:24:39
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
blob字段用于存儲(chǔ)二進(jìn)制數(shù)據(jù),例如圖像、聲音、二進(jìn)制文等。
package utils;import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PReparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.Properties;public class JdbcUtil {	private static String passWord;	private static String username;	private static String url;	static {		try {			InputStream in = JdbcUtil.class.getClassLoader().getResourceAsStream("db.properties");			Properties pr = new Properties();			pr.load(in);			username=pr.getProperty("username");			url = pr.getProperty("url");			password = pr.getProperty("password");			String driver = pr.getProperty("driver");			Class.forName(driver);		} catch (Exception e) {			throw new RuntimeException(e);		}	}		public static Connection getConnection() throws SQLException {		return DriverManager.getConnection(url, username, password);	}	public static void release(Connection conn, PreparedStatement st, ResultSet rs) {		if(rs!=null){			try{				rs.close();			}catch (Exception e){				throw new RuntimeException(e);			}			rs = null;		}		if(st != null){			try{				st.close();			}catch (Exception e){				throw new RuntimeException(e);			}			st = null;		}		if(conn != null){			try{				conn.close();			}catch (Exception e){				throw new RuntimeException(e);			}			conn = null;		}	}}
package jdbc;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import org.junit.Test;import utils.JdbcUtil;public class TestBolb {	@Test	public void add() {		Connection conn = null;		PreparedStatement st = null;		ResultSet rs = null;		try {			conn = JdbcUtil.getConnection();			String sql = "insert into testblob(content) values(?)";			st = conn.prepareStatement(sql);//			String path = TestClob.class.getClassLoader().getResource("123.jpg").getPath();			String path = "d://123.jpg";			File file = new File(path);			st.setBinaryStream(1, new FileInputStream(file), file.length());			int num = st.executeUpdate();			if (num > 0) {				System.out.println("插入成功??!");			}		} catch (Exception e) {			throw new RuntimeException(e);		} finally {			JdbcUtil.release(conn, st, rs);		}	}	@Test	public void read(){		Connection conn = null;		PreparedStatement st = null;		ResultSet rs = null;		try{			conn = JdbcUtil.getConnection();			String sql = "select * from testblob where id = 1";			st = conn.prepareStatement(sql);			rs = st.executeQuery();			if (rs.next()) {				FileOutputStream out = new FileOutputStream("d://1.jpg");				InputStream reader = rs.getBinaryStream("content");				byte buffer[] = new byte[1024];				int len = 0;				while ((len = reader.read(buffer)) > 0) {					out.write(buffer, 0, len);				}				out.close();				reader.close();			}		}catch(Exception e){			throw new RuntimeException(e);		}finally{			JdbcUtil.release(conn, st, rs);		}	}	}
上一篇:隨機(jī) Random

下一篇:HashMap源碼剖析

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 蒙阴县| 五寨县| 若羌县| 偏关县| 安乡县| 麟游县| 临安市| 新郑市| 大悟县| 泰顺县| 长宁区| 宁德市| 株洲市| 来安县| 武定县| 阿勒泰市| 凤山县| 义马市| 石城县| 景德镇市| 儋州市| 张家口市| 杂多县| 青川县| 呼伦贝尔市| 抚顺县| 名山县| 巴林右旗| 福鼎市| 海宁市| 阜阳市| 渝北区| 油尖旺区| 江永县| 星子县| 葫芦岛市| 湖北省| 富宁县| 女性| 嘉善县| 厦门市|