dokladnie jak w temacie nie wiem jak zapisac do bazy mysql zdjecie , wiem ze ma byc typem BLOB w bazie ale jak to sie je w javie nie wiem i nie moge zadnego tutka znaleŹĆ na googlach
0
0
Co prawda to jest dla bazy oraclowej ale dla MySql powinno być podobnie:
public class TestConnection {
public static void main(String[] args) throws Exception {
String plik = "poziomka.gif";
Connection conn=OracleConnection.getConnection();
Statement stmt = conn.createStatement();
conn.setAutoCommit(false);
stmt.executeUpdate("insert into lukasz_picture values (1,'"+ plik + "',empty_blob())");
ResultSet res = stmt.executeQuery("select photo from pictures where id=1 for update nowait");
res.next();
BLOB oracleBlob = ((OracleResultSet) res).getBLOB(1);
stmt.close();
FileInputStream fin = new FileInputStream(new File(plik));
OutputStream out = oracleBlob.getBinaryOutputStream();
byte[] buffer = new byte[oracleBlob.getBufferSize()];
int length = 0;
while ((length = fin.read(buffer)) != -1) {
out.write(buffer, 0, length);
}
fin.close();
out.close();
conn.commit();
}
}
Przy czym klasa OracleConnection wygląda tak:
public class OracleConnection {
private static Connection connection;
private OracleConnection() {
}
public static Connection getConnection() throws ClassNotFoundException,
SQLException, ParserConfigurationException, SAXException, IOException {
Configuration conf = new Configuration("conf.xml");
Properties props = new Properties();
String dbURL = "jdbc:oracle:thin:@" + conf.getHost() + ":"
+ conf.getPort() + ":" + conf.getSid();
if (connection == null) {
Class.forName("oracle.jdbc.driver.OracleDriver");
props.put("user", conf.getUsername());
props.put("password", conf.getPassword());
connection = DriverManager.getConnection(dbURL, props);
}
return connection;
}
}
A klasa Configuration odczytuje konfigurację z pliku XML :-)