java向mysql中插入图片,类型为blob

try{
Class.forName("com.mysql.jdbc.Driver");//驱动的名称
Connection c=DriverManager.getConnection("jdbc:mysql://localhost/数据库名?user=用户名&password=密码");
//访问的数据库的帐号密码
Statement s=c.createStatement();
File f = new File("E:/aa/IMG0105A.jpg" );
try{
PreparedStatement pstm = c.prepareStatement("insert into imgp(name,img) values (?,?)");
InputStream is = new FileInputStream(f);
pstm.setString(1, "lm");
pstm.setBinaryStream(2, is, is.available());
int count = pstm.executeUpdate();
if(count>0){
System.out.println("插入成功");
}else{
System.out.println("插入失败");
}
is.close();
pstm.close();
c.close();

} catch (Exception e){
e.printStackTrace();
}

s.close();
}catch(Exception e){
System.out.println("chu");
e.printStackTrace();}//捕获异常
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值