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();
}
}
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();
}
}
解决方案 »
- 新手求教!大侠们伸下援手吧!!!关于类与对象的问题,是在事件方法中一般类的对象创建和方法使用。。。
- 窗口关闭问题
- struts框架中为什么地址栏输入的是http://localhost:7001/struts-app/login.do而打开了一个jsp页面??
- 关于java写socket服务和c写的客户端的通信
- 老知识,新问题-----关于汉字文本编码
- 汉字的unicode与汉字的区位码有什么样的关系?
- 做对日的软件外包,不知道有没有前途?给点意见
- 做聊天室,用Socket好?还是DatagramSocket呢?
- C --〉JAVA,这段函数如何翻译
- 大家能否讲一讲怎样继承基类的synchronized方法,谢谢
- String编译器优化问题
- Java 窗体监听问题
是否要写成
"d:/ImageUtilTest.java"或
"d:\\ImageUtilTest.java"
1、读取到地址存入数据库
BufferedReader bw=new BufferedReader(new FileReader(file));
String url = bw.readLine();
preparedStatement.setString(1, url);2、数据库存入一个文件流,但是数据库字段类型要改变了。
FileInputStream fileInputStream = new FileInputStream(file);
preparedStatement.setBinaryStream(1,fileInputStream,fileInputStream.available());
最后想说的是,文件地址是没错的,楼主写的那个方法setCharacterStream真心没怎么用过,所以不是很清楚。继续探讨,呵呵。
public ResultSet executeQuery() throws SQLException
返回一个不为空的结果集
public int executeUpdate() throws SQLException
返回:1、返回INSERT、UPDATE或者DELETE语句执行后的更新行数;
2、返回0表示SQL语句没有执行成功。
public boolean execute() throws SQLException
返回:true表示SQL语句执行的结果返回ResultSet对象;
false表示SQL语句执行结果返回的是更新行数或者没有返回。
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
new ClobInerst().insert();
}
public void insert() throws Exception{
String sql="insert into clob_table(blob) values(?)";
//注册驱动
//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);*/
File file=new File("src/1.jpg");
BufferedInputStream bis=new BufferedInputStream(new FileInputStream(file));
preparedStatement.setBinaryStream(1, bis,file.length());
//执行语句
preparedStatement.executeUpdate();
preparedStatement.close();
connection.close();
//bw.close();
}}
Exception in thread "main" java.lang.AbstractMethodError: com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setBinaryStream(ILjava/io/InputStream;J)V
at com.csxh.jdbc.ClobInerst.insert(ClobInerst.java:44)
at com.csxh.jdbc.ClobInerst.main(ClobInerst.java:22)
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
new ClobInerst().insert();
}
public void insert() throws Exception{
String sql="insert into clob_table(blob) values(?)";
//注册驱动
//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);*/
File file=new File("src/1.jpg");
BufferedInputStream bis=new BufferedInputStream(new FileInputStream(file));
preparedStatement.setBinaryStream(1, bis,file.length());
//执行语句
preparedStatement.executeUpdate();
preparedStatement.close();
connection.close();
//bw.close();
}}
Exception in thread "main" java.lang.AbstractMethodError: com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setBinaryStream(ILjava/io/InputStream;J)V
at com.csxh.jdbc.ClobInerst.insert(ClobInerst.java:44)
at com.csxh.jdbc.ClobInerst.main(ClobInerst.java:22)
//Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
这里应该是你的笔误,把注释去掉
2,preparedStatement.setBinaryStream(1, bis,file.length());
这里不要用file.length().
preparedStatement.setBinaryStream(1, bis,bis.available());
具体区别我也不是特别清楚,楼主如果有兴趣可以看看相关资料。测试通过的。
这句的