I have Problem to Save My Image Using File Chooser in derby Database. I want to build a File Chooser Method to get image from User and save the image into database . but now the image Would not be save in my database. Please Help me. Thank You
My Tables are as follow:
-- create table LABORATORY(
-- ID INT NOT NULL,
-- TESTERS_ID INT NOT NULL,
-- PATIENTS_ID INT,
-- AZMAYESH BLOB
-- PRIMARY KEY(ID));
And My Class code :
public class Database {
//FileInputStream fis = null;
//PreparedStatement ps = null;
private Connection connection;
private ResultSet resultSet;
private Statement statement;
public Database() throws SQLException{/*nabayad public bashe*/
try{
String userName="reza";
String password="reza";
String hostURL="jdbc:derby://localhost:1527/reza";
connection=DriverManager.getConnection(hostURL,userName,password);
statement=connection.createStatement(TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
}
catch(SQLException err){
JOptionPane.showMessageDialog(null, err.getMessage());
}
}
public void azmayesh() throws SQLException, IOException {
int id=LastIdOf("LABORATORY");
String INSERT_PICTURE = "insert into LABORATORY(ID,TESTERS_ID,PATIENTS-ID,AZMAYESH) values (?,?,?,?)";
FileInputStream fis = null;
PreparedStatement ps = null;
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open text file");
fileChooser.setInitialDirectory(new File(System.getProperty("user.home")));
fileChooser.getExtensionFilters().addAll(
new FileChooser.ExtensionFilter("image Files", "*.jpg")
);
File file = fileChooser.showOpenDialog(null);
fis = new FileInputStream(file);
ps = connection.prepareStatement(INSERT_PICTURE);
ps.setInt(1, id);
//ps.setInt(2, id);
//ps.setInt(3, id);
ps.setBinaryStream(4, fis, (int) file.length());
ps.executeUpdate();
JOptionPane.showMessageDialog(null, "Azmayesh Ersal shod.");
//connection.commit();
//ps.close();
//fis.close();
}
}