1. 程式人生 > >Java將檔案、圖片路徑儲存到資料庫

Java將檔案、圖片路徑儲存到資料庫

1.如果直接把檔案、圖片儲存到資料庫,存取效率非常低,而且大量消耗資料庫儲存空間,因此我們只需要儲存檔案路徑即可。

2.通過檔案選擇器獲取檔案的路徑

3.執行SQL語句將檔案路徑新增到資料庫。

4.程式碼演示讀取檔案

package com.JDBC;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import
java.io.InputStreamReader; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.swing.JFileChooser; import javax.swing.filechooser.FileSystemView; public class testFileChooserSelectPath { String url ="jdbc:mysql://localhost:3306/test"
; String driver="com.mysql.jdbc.Driver"; String user = "root"; String password = "root"; Connection conn=null; PreparedStatement ps=null; ResultSet resultSet=null; File selectedFile=null; String contextPath; public void connectionDB(){ try { Class.forName(driver); conn = DriverManager.getConnection(url, user, password); System.out.println("Successfully connected"
); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void SelectPathInsertIntoDB(){ JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory()); int returnValue = jfc.showSaveDialog(null); if (returnValue == JFileChooser.APPROVE_OPTION) { selectedFile = jfc.getSelectedFile(); System.out.println(selectedFile.getAbsolutePath()); } String path=selectedFile.getAbsolutePath(); System.out.println(path); try { //連線資料庫再操作 conn = DriverManager.getConnection(url, user, password); String insertsql="insert into tb_Path(path) value(?)"; ps=conn.prepareStatement(insertsql); ps.setString(1, path); ps.executeUpdate(); System.out.println("檔案路徑成功插入資料庫"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void ReadPathFromDB(){ try { conn = DriverManager.getConnection(url, user, password); String selectsql="select path from tb_Path where id=?"; ps=conn.prepareStatement(selectsql); ps.setInt(1,12); ResultSet rs = ps.executeQuery(); while(rs.next()){ contextPath = rs.getString("path"); System.out.println(contextPath); } File f = new File(contextPath); //使用FileReader讀取檔案內容出現亂碼 // FileReader fr=new FileReader(f); // while(fr.read()!=-1){ // char ch=(char) fr.read(); // System.out.print(ch); // } // fr.close(); // FileInputStream fi=new new FileInputStream(f); BufferedReader br=null; br=new BufferedReader(newInputStreamReader( fi,"UTF-8")); String line = null; while ((line = br.readLine()) != null) { System.out.println(line); } } 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(); } } public static void main(String[] args) { testFileChooserSelectPath t=new testFileChooserSelectPath (); //t.SelectPathInsertIntoDB(); t. ReadPathFromDB(); } }

這裡寫圖片描述