標籤:
@Test public void run() throws ClassNotFoundException, SQLException, FileNotFoundException { try { Connection conn = jdbcUtils. getConnection(); PreparedStatement pst = conn .prepareStatement( "insert into myblob values(null,?)"); File file = new File( "E:\\Java\\學習\\day17-jdbc\\視頻\\1.課程介紹.avi" ); FileInputStream fs = new FileInputStream(file); pst.setBinaryStream(1, fs, ( int) file.length()); int row = pst.executeUpdate(); if (row != 0) { System. out.println( "插入成功"); } } catch (Exception e) { e.printStackTrace(); } }
@Test
public void save() throws ClassNotFoundException, SQLException,
FileNotFoundException {
try {
Connection conn = jdbcUtils.getConnection();
PreparedStatement pst = conn
.prepareStatement("insert into mytext values(null,?)");
File file = new File("E:\\Java\\Workspaces\\DAO\\a.txt");
FileReader fr = new FileReader(file);
pst.setCharacterStream(1, fr, (int) file.length());
int row = pst.executeUpdate();
if (row != 0) {
System.out.println("插入成功");
}
} catch (Exception e) {
e.printStackTrace();
}
}
向mysql資料庫中插入大二進位檔案和大文本