Java 向SQL Server插入檔案資料

來源:互聯網
上載者:User

標籤:

package sqlserver;import java.util.Date;import java.util.UUID;import java.text.SimpleDateFormat;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.PrintStream;import java.sql.*;public class sqlserver {static String uuid=null;//////////////////////////////////////////////////////public static void main(String[] args) {FileOutputStream bos = null;try {bos = new FileOutputStream("F:/output.txt");} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.setOut(new PrintStream(bos));System.out.println(insertData());//    getData();    }public static Connection MSSQLConnection(){String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; // 載入JDBC驅動String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=filesdata"; // 串連伺服器和資料庫testString userName = "sa"; // 預設使用者名String userPwd = "sa"; // 密碼Connection dbConn = null;SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");try {Class.forName(driverName);dbConn = DriverManager.getConnection(dbURL, userName, userPwd);System.out.println(df.format(new Date())+" "+"資料庫連接成功。"); // 如果串連成功return dbConn;    // 控制台輸出Connection// Successful!} catch (Exception e) {e.printStackTrace();}return null;}// read the file and insert into the video table;static String insertData() {int isFilesFound=0;SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date2 = new Date();Connection dbConn = null;dbConn=MSSQLConnection();//UUID uuid = UUID.randomUUID();String f_id = UUID.randomUUID().toString();//用來產生資料庫的主鍵id非常不錯..uuid=f_id;File file = new File("F:/工作檔案/PSD.rar");System.out.println(df.format(new Date())+" "+"正在處理檔案:"+file.getName()+" UUID:"+uuid);FileInputStream fis = null;try{fis = new FileInputStream(file);isFilesFound=1;} catch(FileNotFoundException e){System.out.println(df.format(new Date())+" "+"未找到檔案。");}if(isFilesFound==1){try {System.out.println(df.format(new Date())+" "+"開始向資料庫寫入檔案。");PreparedStatement ps = dbConn.prepareStatement("Insert into files (f_id,f_name,f_content,f_date) values (?,?,?,?)");ps.setString(1,f_id); ps.setString(2,file.getName());ps.setBinaryStream(3,fis,(int)file.length());ps.setDate(4,new java.sql.Date(date2.getTime()));System.out.println(df.format(new Date())+" "+"正在寫入..。"); ps.executeUpdate(); ps.close(); try{ fis.close(); System.out.println(df.format(new Date())+" "+"檔案已寫入資料表且資料流已關閉。"); }catch(IOException e){ System.out.println(df.format(new Date())+" "+"資料流無法關閉。"); } } catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}try {dbConn.close();System.out.println(df.format(new Date())+" "+"關閉資料庫連接.");} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return df.format(new Date())+" 插入資料完成。"; }// get the data file from database;static String getData(){Connection dbcon = null;SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");try{dbcon = MSSQLConnection();//OutputStream out = new FileOutputStream("d:/1.exe");Statement st = dbcon.createStatement();ResultSet rs = st.executeQuery("select * from files where f_id = ‘f7453b56-92f2-4aa9-8781-6ca85ab3f0ce‘");while(rs.next()){java.io.InputStream fi=rs.getBinaryStream("f_content");File file=new File("d:/1.zip");FileOutputStream fo = new FileOutputStream(file);BufferedOutputStream bo = new BufferedOutputStream(fo);byte[] buff = new byte[1024];System.out.println(df.format(new Date())+" "+"正在從資料庫寫入本地...");while((fi.read(buff))>0){bo.write(buff);}System.out.println(df.format(new Date())+" "+"寫入完畢.");bo.close();fo.close();fi.close();System.out.println(df.format(new Date())+" "+"關閉讀寫流.");}}catch(Exception ex){ex.printStackTrace();}finally{if (dbcon!=null){try{dbcon.close();System.out.println(df.format(new Date())+" "+"關閉資料庫連接.");}catch(Exception ex){ex.printStackTrace();}}dbcon = null;}return null;}//////////////////////////////////////////////////////}

 

Java 向SQL Server插入檔案資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.