码迷,mamicode.com
首页 > 数据库 > 详细

mysql插入图片数据

时间:2014-12-05 17:34:24      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:java   图片   mysql   

import java.sql.*;  
import java.util.Scanner;
import java.io.*;  
 
public class mysql插入图片 {  
    private static final File File = null;
 private static String String;
  
    public static  Connection getConn() {  
        Connection conn = null;  
        try {  
            Class.forName("com.mysql.jdbc.Driver");  
            conn = DriverManager.getConnection(  
                    "jdbc:mysql://localhost:3306/xinxi", "root", "123456");  
        } catch (ClassNotFoundException e) {  
            e.printStackTrace();  
        } catch (SQLException e) {  
            e.printStackTrace();  
        }  
       
        return conn;  
    }  
 
public static void add( String  fromFileName,  String toFileName) {  
    Connection conn = getConn();  
    String sql = <span style="font-family: simsun;">"insert into  image( id , image)  values(?,?)"</span><span style="font-family: simsun;">;</span>
    PreparedStatement ps;  
    try {  
        ps = conn.prepareStatement(sql);  
        ps.setString(1, "tupian");    
        File file1=new File("D:\\test.jpg");
        InputStream in;  
        in = new BufferedInputStream(new FileInputStream(file1));  
        ps.setBinaryStream(2, in, (int) file1.length());  
        int count = 0;  
        count = ps.executeUpdate();  
        if(count==1)  
        {  
             System.out.println("success");  
         }else 
        {  
            System.out.println("failed");  
         }  
        in.close();  
        ps.close();  
        conn.close();  
    } catch (SQLException e) {  
        e.printStackTrace();  
    }catch (FileNotFoundException e) {  
        e.printStackTrace();  
    } catch (IOException e) {  
        e.printStackTrace();  
    }  
}

mysql插入图片数据

标签:java   图片   mysql   

原文地址:http://blog.csdn.net/yixian918/article/details/41748499

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!