国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

MySQL 操作Blob字段工具(Java)

2019-11-06 07:42:28
字體:
來源:轉載
供稿:網友

Navicat工具可以很好的查看Blob中的文本數據,可視對于二進制數據,沒有辦法查看,我希望有一個工具可以簡單的讀取和寫入數據庫中的BLOB字段進行一些測試工作,我簡單的用java實現了這個小工具,下文貼出工具類的源碼(超級簡單),如果只是解決工作中修改BLOG字段,那么直接下載工具就可以了,希望對您有幫助。覺得好用,或者有問題,請留言,謝謝。 MySQLBlobTools工具下載地址

工具簡要介紹

一個簡單的java實現的命令行工具,可以對單條記錄的Blob二進制數據與文件的交互操作。還可以通過腳本實現批量處理。工具簡單實現方法:

java -jar MySQLBlobTools.jar -u -d "jdbc:mysql://localhost:3306/test?user=root&passWord=test&useUnicode=true" -f "./DVP-12SA/DVP-12SA.zPRotconfig set protocol_content =? where id=?" -i 5

參數說明如下:

-r 從記錄中讀Blob數據并寫入文件(-f參數)-u 用文件內容更新數據庫中的Blob字段-w 插入一條Blob記錄-f 需要讀/寫的文件名-d 數據庫連接串,需要指定用戶名和密碼信息-s SQL語句,更新需要指定兩個參數,第一個參數為內容,第二個參數為操作的記錄條件,讀和插入只需一個參數-i 操作記錄的ID信息,讀和更新需要指定

代碼

代碼共兩個文件,需要mysql的驅動jar包。

命令行參數解析

參見Java GetOpt類

MySQLBlobTools類

import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;/** * MySQL Blob tools * @author yinhaibo * @since 2017 * @version 1.00 */public class MySQLBlobTools { private static String VERSION = "0.0.1"; private final static int MODE_INVALID = -1; private final static int MODE_READ = 0; private final static int MODE_WRITE = 1; private final static int MODE_UPDATE = 2; public static void main(String[] args){ String DBUrl = null; String File = null; String SQL = null; String Id = null; int mode = MODE_INVALID; GetOpt getopt = new GetOpt(args, "hvrwuf:s:i:d:"); int c; while((c = getopt.getNextOption()) != -1){ switch(c){ case 'h': printHelpMessage(); System.exit(0); case 'v': printVersionMessage(); System.exit(0); case 'r': case 'w': case 'u': if (mode != MODE_INVALID){ printHelpMessage(); System.out.println(); System.out.println("Only a mode parameter can specified. r = read, w = write, u = update."); System.exit(0); } if (c == 'r'){ mode = MODE_READ; }else if(c == 'w'){ mode = MODE_WRITE; }else if(c == 'u'){ mode = MODE_UPDATE; } break; case 'f': File = getopt.getOptionArg(); break; case 's': SQL = getopt.getOptionArg(); break; case 'i': Id = getopt.getOptionArg(); break; case 'd': DBUrl = getopt.getOptionArg(); break; default: printHelpMessage(); System.exit(0); } } if (File == null || DBUrl == null || SQL == null){ printHelpMessage(); System.out.println(); System.out.println("Need -f, -d -s parameter specified."); System.exit(0); } int rv = 0; switch(mode){ case MODE_READ: rv = readBlob(File, DBUrl, SQL, Id); break; case MODE_WRITE: rv = writeBlob(File, DBUrl, SQL); break; case MODE_UPDATE: rv = updateBlob(File, DBUrl, SQL, Id); break; } System.exit(rv); } private static int updateBlob(String filename, String dBUrl, String SQL, String id) { FileInputStream fis = null; Connection conn = null; PreparedStatement pstmt = null; try { Class.forName("org.gjt.mm.mysql.Driver").newInstance(); conn = DriverManager.getConnection(dBUrl); File file = new File(filename); fis = new FileInputStream(file); pstmt = conn.prepareStatement(SQL); // //這種方法原理上會丟數據,因為file.length()返回的是long型 pstmt.setBinaryStream(1, fis, fis.available()); // 第二個參數為文件的內容 pstmt.setInt(2, Integer.parseInt(id)); pstmt.executeUpdate(); System.out.println("Success to write a blob data."); return 1; } catch (Exception ex) { System.out.println("[blobInsert error : ]" + ex.toString()); } finally { // 關閉所打開的對像// try { pstmt.close(); fis.close(); conn.close(); } catch (IOException e) { } catch (SQLException e) { } } return 0; } private static int writeBlob(String filename, String dBUrl, String SQL) { FileInputStream fis = null; Connection conn = null; PreparedStatement pstmt = null; try { Class.forName("org.gjt.mm.mysql.Driver").newInstance(); conn = DriverManager.getConnection(dBUrl); File file = new File(filename); fis = new FileInputStream(file); pstmt = conn.prepareStatement(SQL); // //這種方法原理上會丟數據,因為file.length()返回的是long型 pstmt.setBinaryStream(1, fis, fis.available()); // 第二個參數為文件的內容 pstmt.executeUpdate(); pstmt.close(); pstmt = conn.prepareStatement("SELECT LAST_INSERT_ID()"); ResultSet rs = pstmt.executeQuery(); rs.next(); rs.close(); pstmt.close(); System.out.println("Success to insert a blob record."); return rs.getInt(1); } catch (Exception ex) { System.out.println("[blobInsert error : ]" + ex.toString()); } finally { // 關閉所打開的對像// try { pstmt.close(); fis.close(); conn.close(); } catch (IOException e) { } catch (SQLException e) { } } return 0; } private static int readBlob(String filename, String dBUrl, String SQL, String id) { Connection conn = null; PreparedStatement pstmt = null; byte[] Buffer = new byte[4096]; try { Class.forName("org.gjt.mm.mysql.Driver").newInstance(); conn = DriverManager.getConnection(dBUrl); pstmt = conn.prepareStatement(SQL); // //這種方法原理上會丟數據,因為file.length()返回的是long型 pstmt.setInt(1, Integer.parseInt(id)); ResultSet rs = pstmt.executeQuery(); rs.next(); File file = new File(filename); if (!file.exists()) { file.createNewFile(); // 如果文件不存在,則創建 } FileOutputStream fos = new FileOutputStream(file); InputStream is = rs.getBinaryStream(1); int size = 0; long totalLen = 0; while ((size = is.read(Buffer)) != -1) { totalLen += size; fos.write(Buffer, 0, size); } fos.close(); System.out.println("Read blob data success, total length:" + totalLen); return 1; } catch (Exception ex) { System.out.println("Read blob data error : ]" + ex.toString()); } finally { // 關閉所打開的對像// try { pstmt.close(); conn.close(); } catch (SQLException e) { } } return 0; } private static void printVersionMessage() { System.out.println("MySQLBlobTools was make to Operate blob field in mysql database. The tools just can process only a record."); System.out.println("Author: yinhaibo Email: yhb_yinhaibo@163.com"); System.out.println("Version: " + VERSION); } private static void printHelpMessage() { printVersionMessage(); System.out.println("-r read mode, need d, f, s, i parameters"); System.out.println("-w write mode, need d, f, s, i parameters"); System.out.println("-u update mode, need d, f, s, i parameters"); System.out.println("-d <MySQL URI> MySQL database uri, jdbc:mysql://localhost:3306/user?user=root&password=12345678"); System.out.println("-f <Filename> file for reading or writing"); System.out.println("-s <Operate SQL>, select blob from images where id=?, ? for i paramter"); System.out.println("-i <Record Id>"); System.out.println("For example:"); System.out.println("java MySQBlobTools.jar -r -f Blob.png -s /"select blob from images where id = ?/" -i 123"); System.out.println("java MySQBlobTools.jar -w -f Blob.png -s /"insert into images (blob) values(?) /""); System.out.println("java MySQBlobTools.jar -u -f Blob.png -s /"update images set blob=? where id=?/" -i 123"); }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 珠海市| 莎车县| 齐齐哈尔市| 沅江市| 都兰县| 沙坪坝区| 海宁市| 三明市| 云霄县| 济源市| 瑞安市| 紫阳县| 宁都县| 汽车| 隆安县| 太湖县| 施秉县| 兴和县| 永宁县| 自治县| 绩溪县| 昌邑市| 淄博市| 宁明县| 珠海市| 崇礼县| 茂名市| 寻乌县| 宁安市| 交口县| 龙门县| 澜沧| 鄂尔多斯市| 锡林浩特市| 玉溪市| 汉阴县| 汝南县| 且末县| 京山县| 大港区| 邵武市|