課程
/后端開發(fā)
/Java
/Java Socket應(yīng)用---通信是這樣練成的
mysql數(shù)據(jù)庫可以上傳保存文件的嗎?、
2016-04-25
源自:Java Socket應(yīng)用---通信是這樣練成的 6-1
正在回答
public?class?Ckb_test?{?? ?? ????public?static?void?main(String[]?args)?throws?SQLException,?IOException?{?? ????????//?TODO?Auto-generated?method?stub?? ????????read();?? ????}?? ????static?void?create()?throws?SQLException,?IOException?? ????{?? ????????Connection?conn=null;?? ????????PreparedStatement?prest=null;?? ????????ResultSet?resultset=null;?? ?????????? ????????try?{?? ????????????//2.建立連接?? ????????????conn=JdbcUtils.getConnection();?? ????????????//單例設(shè)計模式?? ????????????conn=JdbcUtilsSingle.getInstance().getConnection();?? ????????????//3.創(chuàng)建語句?? ????????????String?sql="insert?into?ckb_test(text)?values(?)";?? ????????????prest=conn.prepareStatement(sql);?? ????????????File?file=new?File("src/cn/com/JDBC/JdbcUtils.java");?? ????????????Reader?reader=new?BufferedReader(new?FileReader(file));?? ????????????prest.setCharacterStream(1,?reader,?(int)file.length());?? ????????????//4.執(zhí)行語句?? ?????????????? ????????????int?i=prest.executeUpdate();?? ????????????reader.close();?? ????????????System.out.println("i="+i);?? ????????}?finally?? ????????{?? ????????????JdbcUtils.free(resultset,?prest,?conn);?? ????????}?? ????}?? ????static?void?read()?throws?SQLException,?IOException?? ????{?? ????????Connection?conn=null;?? ????????Statement?st=null;?? ????????ResultSet?resultset=null;?? ?????????? ????????try?{?? ????????????//2.建立連接?? ????????????conn=JdbcUtils.getConnection();?? ????????????//單例設(shè)計模式?? ????????????conn=JdbcUtilsSingle.getInstance().getConnection();?? ????????????//3.創(chuàng)建語句?? ????????????st=conn.createStatement();?? ????????????//4.執(zhí)行語句?? ????????????resultset=st.executeQuery("select?text?from?ckb_test");?? ????????????//5.處理結(jié)果?? ????????????while(resultset.next())?? ????????????{?? ????????????????Clob?clob=resultset.getClob(1);?? ????????????????Reader?reader=clob.getCharacterStream();?? ????????????????//reader=resultset.getCharacterStream(1);?? ????????????????File?file=new?File("JdbcUtils.java");?? ????????????????Writer?writer=new?BufferedWriter(new?FileWriter(file));?? ????????????????char[]?buff=new?char[1024];?? ????????????????for(int?i=0;(i=reader.read(buff))>0;)?? ????????????????{?? ????????????????????writer.write(buff,0,i);?? ????????????????}?? ????????????????writer.close();?? ????????????????reader.close();?? ????????????}?? ?????????????? ????????}?finally?? ????????{?? ????????????JdbcUtils.free(resultset,?st,?conn);?? ????????}?? ????}?? ?? }?? ?? ----------------------------------------------------------------------------------------------------------?? public?class?PictureBlob?{?? ?? ????public?static?void?main(String[]?args)?throws?SQLException,?IOException?{?? ????????read();?? ????}?? ????static?void?create()?throws?SQLException,?IOException?? ????{?? ????????Connection?conn=null;?? ????????PreparedStatement?prest=null;?? ????????ResultSet?resultset=null;?? ?????????? ????????try?{?? ????????????//2.建立連接?? ????????????conn=JdbcUtils.getConnection();?? ????????????//單例設(shè)計模式?? ????????????conn=JdbcUtilsSingle.getInstance().getConnection();?? ????????????//3.創(chuàng)建語句?? ????????????String?sql="insert?into?blob_test(big_bit)?values(?)";?? ????????????prest=conn.prepareStatement(sql);?? ????????????File?file=new?File("C:\\Documents?and?Settings\\Administrator\\My?Documents\\My?Pictures\\cxg.jpg");?? ????????????InputStream?in=new?BufferedInputStream(new?FileInputStream(file));?? ????????????prest.setBinaryStream(1,?in,?(int)file.length());?? ????????????//4.執(zhí)行語句?? ?????????????? ????????????int?i=prest.executeUpdate();?? ????????????in.close();?? ????????????System.out.println("i="+i);?? ????????}?finally?? ????????{?? ????????????JdbcUtils.free(resultset,?prest,?conn);?? ????????}?? ?????????? ????}?? ?????? ????static?void?read()?throws?SQLException,?IOException?? ????{?? ????????Connection?conn=null;?? ????????Statement?st=null;?? ????????ResultSet?resultset=null;?? ?????????? ????????try?{?? ????????????//2.建立連接?? ????????????conn=JdbcUtils.getConnection();?? ????????????//單例設(shè)計模式?? ????????????conn=JdbcUtilsSingle.getInstance().getConnection();?? ????????????//3.創(chuàng)建語句?? ????????????st=conn.createStatement();?? ????????????//4.執(zhí)行語句?? ????????????resultset=st.executeQuery("select?big_bit?from?blob_test");?? ????????????//5.處理結(jié)果?? ????????????while(resultset.next())?? ????????????{?? ????????????????Blob?blob=resultset.getBlob(1);?? ????????????????InputStream?in=blob.getBinaryStream();?? ????????????????//reader=resultset.getCharacterStream(1);?? ????????????????File?file=new?File("1.jpeg");?? ????????????????OutputStream?out=new?BufferedOutputStream(new?FileOutputStream(file));?? ????????????????byte[]?buff=new?byte[1024];?? ????????????????for(int?i=0;(i=in.read(buff))>0;)?? ????????????????{?? ????????????????????out.write(buff,0,i);?? ????????????????}?? ????????????????out.close();?? ????????????????in.close();?? ????????????}?? ?????????????? ????????}?finally?? ????????{?? ????????????JdbcUtils.free(resultset,?st,?conn);?? ????????}?? ????}?? }
Helios_ 提問者
public void upload(String userid, InputStream is) throws SQLException{
Connection conn=Init.getConnection();
String sql="" +
" insert file(userid,file) " +
" values(?,?) " ;
PreparedStatement ptmt=conn.prepareStatement(sql);
ptmt.setString(1, userid);
ptmt.setBinaryStream(2, is);
ptmt.execute();
}
public InputStream download(String userid) throws SQLException{
" select * from file " +
" where userid like ? ";
ptmt.setString(1, "%"+userid+"%");
ResultSet rs=ptmt.executeQuery();
rs.next();
return rs.getBinaryStream("file");
首先,文件是可以保存到數(shù)據(jù)庫中的,但是不建議這么做(Why請百度)。實際的做法是把文件的路徑保存到數(shù)據(jù)庫中,然后讀取文件的話先是從數(shù)據(jù)庫中讀到路徑,再根據(jù)路徑得到文件,希望能幫到
可以自己在數(shù)據(jù)庫中建一個文件夾,然后將圖片資源上傳到該文件夾中
舉報
分享的是 Java 中的網(wǎng)絡(luò)編程,使用Socket實現(xiàn)網(wǎng)絡(luò)聊天通信
3 回答怎么在數(shù)據(jù)庫中查看上傳的圖片呀???
1 回答求源碼:文件上傳數(shù)據(jù)庫
3 回答圖片直接可以保存在數(shù)據(jù)庫中?
1 回答將數(shù)據(jù)發(fā)送到服務(wù)器
1 回答上傳文件是遇到數(shù)據(jù)太長的問題
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2016-05-01
2016-05-01
public void upload(String userid, InputStream is) throws SQLException{
Connection conn=Init.getConnection();
String sql="" +
" insert file(userid,file) " +
" values(?,?) " ;
PreparedStatement ptmt=conn.prepareStatement(sql);
ptmt.setString(1, userid);
ptmt.setBinaryStream(2, is);
ptmt.execute();
}
public InputStream download(String userid) throws SQLException{
Connection conn=Init.getConnection();
String sql="" +
" select * from file " +
" where userid like ? ";
PreparedStatement ptmt=conn.prepareStatement(sql);
ptmt.setString(1, "%"+userid+"%");
ResultSet rs=ptmt.executeQuery();
rs.next();
return rs.getBinaryStream("file");
}
2016-05-01
首先,文件是可以保存到數(shù)據(jù)庫中的,但是不建議這么做(Why請百度)。實際的做法是把文件的路徑保存到數(shù)據(jù)庫中,然后讀取文件的話先是從數(shù)據(jù)庫中讀到路徑,再根據(jù)路徑得到文件,希望能幫到
2016-04-26
可以自己在數(shù)據(jù)庫中建一個文件夾,然后將圖片資源上傳到該文件夾中