您的位置:首页 > 其它

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

2016-10-13 20:14 218 查看
和处理常见属性大致相同,不同点在于大文本的输入借助于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 stub
create();
}

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);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  JDBC
相关文章推荐