使用JDBC處理位元據

來源:互聯網
上載者:User

 

使用JDBC處理位元據

對於MySQL中的BLOB類型,可調用如下方法設定:

PreparedStatement. setBinaryStream(i, inputStream, length);

//大位元據存入到資料庫中

    public void insert(){

       Connection con=null;

       PreparedStatement st=null;

       ResultSet rs=null;

      

       try{

       //擷取串連

       try {

           con=DBManager.getConnection();

           String sql="insert into testblob (images) value(?)";

           st=con.prepareStatement(sql);

          

           File f=new File("yes.gif");

          

           st.setBinaryStream(1, new FileInputStream(f),f.length());

           int result=st.executeUpdate();

           if(result>0){

              System.out.println("插入成功");

           }else{

              System.out.println("插入失敗");

           }

       } catch (SQLException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       } catch (FileNotFoundException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

      

       }

       //資源釋放

       finally{

           DBManager.release(con, st, rs);

       }

}

 

對MySQL中的BLOB類型,可調用如下方法擷取:

InputStream in  = resultSet.getBinaryStream(i);

InputStream in  = resultSet.getBlob(i).getBinaryStream();

 

public void find(){

       Connection con=null;

       PreparedStatement st=null;

       ResultSet rs=null;

       try {

           con=DBManager.getConnection();

           String sql="select images from testblob where id=1";

           st=con.prepareStatement(sql);

           rs=st.executeQuery();

           //結果集的讀取

           if(rs.next()){

              InputStream fis=rs.getBinaryStream("images");

              byte[] buff=new byte[1024];

              int len=0;

              FileOutputStream fos=new FileOutputStream("yes.gif");

              while((len=fis.read(buff))>0){

                  fos.write(buff,0,len);

              }

              fos.close();

              fis.close();

           }

       } catch (SQLException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       } catch (FileNotFoundException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       } catch (IOException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

    }

附加:DBManager.java對資料庫的連結

public class DBManager {

   

    private static String username;

    private static String password;

    private static String url;

    private static String driver;

 

    static{

       try{

           InputStream in = DBManager.class.getClassLoader().getResourceAsStream("config/dbcp.properties");

           Properties prop = new Properties();

           prop.load(in);

           driver = prop.getProperty("driverClassName");

           url = prop.getProperty("url");

           username = prop.getProperty("username");

           password = prop.getProperty("password");

           Class.forName(driver);

       }catch (Exception e) {

           throw new ExceptionInInitializerError(e);

       }

    }

    public static Connection getConnection()
throws
SQLException{

       return DriverManager.getConnection(url,
username, password);

    }

    public static void release(Connection conn,Statement st,ResultSet rs){

       if(rs!=null){

           try{

              rs.close();

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

            rs = null;

       }

       if(st!=null){

           try{

              st.close();

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

           st = null;

       }

       if(conn!=null){

           try{

              conn.close();

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

           conn = null;

       }

    }

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.