日期:2014-05-16 浏览次数:20943 次
try{
Class.forName("com.mysql.jdbc.Driver");//驱动的名称
Connection c=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/image?user=root&password=liuys");
//访问的数据库的帐号密码
Statement s=c.createStatement();
File f = new File("F:/photo/zhangting/0.jpg" );
try{
PreparedStatement pstm = c.prepareStatement("insert into image(name,img) values (?,?)");
InputStream is = new FileInputStream(f);
pstm.setString(1, "m1");
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){
e.printStackTrace();}//捕获异常
}