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

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

jdbc往mysql讀取大文本文件

2019-11-11 05:19:52
字體:
供稿:網(wǎng)友
package jdbc;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.Reader;import java.sql.Connection;import java.sql.PReparedStatement;import java.sql.ResultSet;import org.junit.Test;import utils.JdbcUtil;public class TestClob {	//讀取本地文件寫到數(shù)據(jù)庫	@Test	public void add() {		Connection conn = null;		PreparedStatement st = null;		ResultSet rs = null;		try {			conn = JdbcUtil.getConnection();			String sql = "insert into testclob(content) values(?)";			st = conn.prepareStatement(sql);			String path = TestClob.class.getClassLoader().getResource("1.txt").getPath();			File file = new File(path);			st.setCharacterStream(1, new FileReader(file), file.length());			int num = st.executeUpdate();			if (num > 0) {				System.out.println("插入成功!?。?quot;);			}		} 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 content from testclob where id = 3";			st = conn.prepareStatement(sql);			rs = st.executeQuery();						if(rs.next()){								FileWriter out = new FileWriter("d://1.java");				Reader reader = rs.getCharacterStream("content");				char buffer[]= new char[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);		}	}}
//工具類
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;		}	}}


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 深州市| 西畴县| 昭通市| 偏关县| 丰城市| 滨海县| 博客| 资阳市| 密山市| 盐津县| 赣州市| 崇义县| 汶上县| 伊吾县| 隆安县| 犍为县| 永和县| 兴宁市| 体育| 克拉玛依市| 聂荣县| 吉水县| 滦南县| 炉霍县| 连城县| 阿合奇县| 乾安县| 罗江县| 尉犁县| 凌云县| 江山市| 繁峙县| 伽师县| 丽江市| 宁海县| 衡南县| 涟水县| 开远市| 巧家县| 五原县| 绥芬河市|