日期:2014-05-20  浏览次数:20734 次

我的数据库插入文本代码哪里错了 求高手解释。 万分感谢
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;


public class ClobInerst {


/**
 * @param args
 * @throws Exception 
 */
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
new ClobInerst().insert("d://ImageUtilTest.java");
}
public void insert(String s) throws Exception{
String sql="insert into clob_table values(1,?)";
//注册驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//建立连接
Connection connection=DriverManager.getConnection("jdbc:sqlserver://localhost:1433; DatabaseName=TuShuGuanDB",
"sa", "123456");
//创建语句
PreparedStatement preparedStatement=connection.prepareStatement(sql);
File file=new File(s);
BufferedReader bw=new BufferedReader(new FileReader(file));
preparedStatement.setCharacterStream(1, bw);
//执行语句
preparedStatement.executeUpdate();
preparedStatement.close();
connection.close();
//bw.close();
}


}


------解决方案--------------------
呵呵,刚看了下,你有两个错误。

1,//注册驱动
//Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
这里应该是你的笔误,把注释去掉


2,preparedStatement.setBinaryStream(1, bis,file.length());
这里不要用file.length().
preparedStatement.setBinaryStream(1, bis,bis.available());
具体区别我也不是特别清楚,楼主如果有兴趣可以看看相关资料。

测试通过的。