Cześć,
mam problem z zapisaniem plików (txt) do bazy danych MySQL. Prośba o pomoc.
Connection conn = null;
Statement stm = null;
String sql = "INSERT INTO PRICES(NAME,FIlE) VALUES(?,?)";
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("mysql://localhost/price.db");
stm = conn.createStatement();
String command = "DROP TABLE PRICES";
try {
stm.executeUpdate(command);
} catch (Exception ex) {
}
command = "CREATE TABLE PRICES("
+ "name TEXT,"
+ "file BLOB"
+ ");";
stm.executeUpdate(command);
String filePath = "C:\\work\\ProjektyNetbeans\\SidGroup\\plik2.txt";
File file = new File(filePath);
try (
FileInputStream inputStream = new FileInputStream(file);
PreparedStatement stmt = conn.prepareStatement(sql);) {
stmt.setString(1, "Mike");
stmt.setBinaryStream(2, inputStream, file.length());
stmt.executeUpdate();
System.out.println("File saved successfully.");
} catch (SQLException ex) {
Logger.getLogger(DBInitialization.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (SQLException ex) {
Logger.getLogger(DBInitialization.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(DBInitialization.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(DBInitialization.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(DBInitialization.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(DBInitialization.class.getName()).log(Level.SEVERE, null, ex);
}
}
otrzymuje w konsoli następujące komunikaty:
SEVERE: null
java.sql.SQLException: No suitable driver found for mysql://localhost/price.db