Tag: file

Saving files in BLOB table column of a data base

To insert a file, is it in any format, you need call the method setBinaryStream, implemented by PreparedStatement.

PreparedStatemente.setBinaryStream(int index, Inputstream is, int length);

In sample, we set a table called FILEthat contains BLOB column called BIN.

//Normal connection, as any JDBC connection
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@<IP>:<PORT>:<SID>","<USER>","<PASSWORD>");

//Reading the file and retrieving an InputStream
File file= new File("<COMPLETE_FILE_PATH>");
FileInputStream fis = new FileInputStream(file);

//Preparing statement
PreparedStatement ps = conn.prepareStatement("INSERT INTO FILE(bin) VALUES(?)");

//Passing InputStream and file length
ps.setBinaryStream(1, fis, (int)file.length());

ps.execute();

ps.close();
conn.close();

I used Oracle 8i to execute this sample. I haven’t a MySQL/PostgreSQL/MS SQL Server in my dispose, then you’ll responsible for testing in this data bases and send me the results, OK 😉

Thanks! Until next time!


Retrieving hard drives or removable devices

You’ve tried retrieve the hard drives or removable devices using java.io.File and passed “/” like parameter in class constructor? The result is the root of classpath, isn’t?

To obtain the hard drives or removable devices, use File.listRoots(). This code return a File array, where each index of array is a removable device or hard disk unit.

Follow this sample:

File[] units = File.listRoots();

for(File unit : units){
    System.out.println(unit.getAbsolutePath());
}

Manipulating ID3 Tags of MP3 files

This post will teach you how to manipulate ID3 tags of MP3 files using Java Jid3Lib Tag Library. First, download the jar file from http://javamusictag.sourceforge.net/.

The code below will show you how to get any information of MP3 file:

try {
    MP3File mp3 = new MP3File("D:\\Musics\\MP3\\ACDC\\ACDC - Back In Black.mp3");
    System.out.println("Album:" + mp3.getID3v2Tag().getAlbumTitle());
    System.out.println("Band:" + mp3.getID3v2Tag().getLeadArtist());
    System.out.println("Composer:" + mp3.getID3v2Tag().getAuthorComposer());
    System.out.println("Year:" + mp3.getID3v2Tag().getYearReleased());
} catch (Exception e) {
	e.printStackTrace();
}

The result:

Album: Back in Black
Band: AC/DC
Composer: Angus Young/Brian Johnson/Malcolm Young
Year:

To change any information of any ID3 tags, try the code bellow:

mp3.getID3v2Tag().setYearReleased("1980");
mp3.save();

This library has many methods that help you to customize any MP3 file.


  • Advertisement

  • Advertisement

  • Copyright © 1996-2010 André L. S.. All rights reserved.
    iDream theme by Templates Next | Powered by WordPress