我测试的时候,两个类都是执行成功的,但是只有第一个类执行之后数据库里面才有图片数据,我想请教一下,第二个类为什么执行成功数据库里面没有数据?
- public class InsertIMG {
- public static void main(String[] args) {
- File file = new File("D:\\zz.jpg");
- InputStream is =null;
- try {
- is = new FileInputStream(file);
- } catch (FileNotFoundException e1) {
- e1.printStackTrace();
- }
- Connection conn = DBUtil.getConnection();
- try {
- PreparedStatement ps = conn.prepareStatement("update T_MEASURE_PIC SET PIC1=? where jlid='1'");
- ps.setBinaryStream(1, is,is.available());
- int i =ps.executeUpdate();
- if(i!=0){
- System.out.println("true");
-
- }else{
-
- System.out.println("false");
- }
-
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- ///第二个类
- public class InsertPIC {
- public static void main(String[] args) {
-
- try {
- Connection conn = DBUtil.getConnection();
- InputStream is = new FileInputStream(new File("D:\\zz.jpg"));
- Boolean b=Insert(conn,is);
- System.out.println(b);
- conn.commit();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- e.getMessage();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- }
- public static boolean Insert(Connection con,InputStream is){
- try {
- PreparedStatement ps = con.prepareStatement("update T_MEASURE_PIC SET PIC1=? where jlid='1'");
- ps.setBinaryStream(1, is,is.available());
- con.setAutoCommit(true);
- int i =ps.executeUpdate();
- if(i!=0){
- System.out.println("true.I的值是:"+i);
- return true;
-
- }else{
-
- System.out.println("false");
- return false;
- }
-
- } catch (SQLException e) {
- e.printStackTrace();
- e.getMessage();
- return false;
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- e.getMessage();
- return false;
- }
- }
- }
复制代码
|