JDBC-处理属性为text的大文本问题

来源:互联网 发布:unetbootin windows 编辑:程序博客网 时间:2024/06/06 16:42

和处理常见属性大致相同,不同点在于大文本的输入借助于java中io流从读取,然后作为参数传递进去。

import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.io.Reader;import java.sql.SQLException;import com.mysql.jdbc.Connection;import com.mysql.jdbc.PreparedStatement;import com.mysql.jdbc.ResultSet;/** * 存入和读取大文本 * @author Neuclil * */public class ClobText {/** * @param args * @throws SQLException  * @throws FileNotFoundException  */public static void main(String[] args) throws FileNotFoundException, SQLException {// TODO Auto-generated method stubcreate();}static void create() throws SQLException, FileNotFoundException {Connection conn = null;PreparedStatement ps = null;ResultSet rs = null;try {conn = JdbcUtil.getConnection();String sql = "insert into text values(1 ,?)";ps = (PreparedStatement) conn.prepareStatement(sql);<span style="color:#ff0000;">File file = new File("src/JdbcUtil.java");Reader reader = new BufferedReader(new FileReader(file));ps.setCharacterStream(1, reader, (int)file.length());</span>int count = ps.executeUpdate();System.out.println(count + " row affected");try {reader.close();} catch (IOException e) {e.printStackTrace();}} finally {JdbcUtil.free(rs, ps, conn);}}}


0 0