Folge dem Video um zu sehen, wie unsere Website als Web-App auf dem Startbildschirm installiert werden kann.
Anmerkung: Diese Funktion ist in einigen Browsern möglicherweise nicht verfügbar.
/**
*
*/
package de.tutorials;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
/**
* @author Tom
*
*/
public class BlobExtractionExample {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setServerName("localhost");
dataSource.setPort(3306);
dataSource.setUser("root");
dataSource.setPassword("");
dataSource.setDatabaseName("test");
Connection con = dataSource.getConnection();
File file = new File("c:/someWordDocument.doc");
insertDocumentIntoDatabase(file, "file_data", "data", con);
File out = fetchDocumentFromDatabase("file_data", "data", 1, con);
if (out.exists()) {
Runtime.getRuntime().exec("cmd /c " + out.getAbsoluteFile());
}
con.close();
}
private static File fetchDocumentFromDatabase(
String tableName,
String columnName,
int id,
Connection con) throws Exception {
Statement statement = con.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT " + columnName
+ " FROM " + tableName + " WHERE id = " + id);
if (!resultSet.next()) {
return new File("");
} else {
byte[] buffer = new byte[8192];
int bytesRead = 0;
InputStream inputStream = resultSet.getBinaryStream(columnName);
File outFile = new File("c:/out.doc");
FileOutputStream fileOutputStream = new FileOutputStream(outFile);
while ((bytesRead = inputStream.read(buffer)) > 0) {
fileOutputStream.write(buffer, 0, bytesRead);
}
fileOutputStream.flush();
inputStream.close();
fileOutputStream.close();
resultSet.close();
return outFile;
}
}
private static void insertDocumentIntoDatabase(
File file,
String tableName,
String columnName,
Connection con) throws Exception {
PreparedStatement preparedStatement = con
.prepareStatement("INSERT into file_data (data) values (?)");
preparedStatement.setBinaryStream(
1,
new FileInputStream(file),
(int) file.length());
preparedStatement.execute();
preparedStatement.close();
}
}