-1

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();
}

}
Uluk Biy
  • 48,655
  • 13
  • 146
  • 153
Reza
  • 33
  • 7
  • You have mentioned `mysql`, but you are trying to connect to `derby`. – Rajesh N May 22 '15 at 10:20
  • Hi, my bad , How to Change the code to connect to mysql? – Reza May 22 '15 at 10:21
  • Update your hostUrl to match your localhost database. Ex. jdbc:mysql://... – Laurentiu L. May 22 '15 at 10:23
  • am Using Java netbeans default Database, Is it derby or mysql? – Reza May 22 '15 at 10:28
  • with String userName="reza"; String password="reza"; String hostURL="jdbc:derby://localhost:1527/reza"; I am connect to data base. – Reza May 22 '15 at 10:29
  • Hey @Reza can you describe " the image Would not be save in my database". Is there any error? if yes, post it. – Uluk Biy May 22 '15 at 10:45
  • Hi Dear Uluk Biy, the project build successful, I am using JavaFX to use that method to save image in my database , after I call the method there is a red lines appear in my netbeans IDE . – Reza May 22 '15 at 12:24

2 Answers2

0

Change this:

    String userName="reza";
    String password="reza";
    String hostURL="jdbc:derby://localhost:1527/reza";

to this

    String userName="mysqlusername";
    String password="mysqlpassword";
    String hostURL="jdbc:mysql://localhost:3306/mysqldatabasename";

Change localhost to your hostname or IP. 3306 is default port for MySQL.

NOTE: You should have mysql-jdbc-connector in your classpath.

Rajesh N
  • 2,554
  • 1
  • 13
  • 17
0
  if(request.getParameter("PhotoFiled")!= null && request.getParameter("PhotoFiled").length()> 0) 
  {
     FileInputStream Photograph_Stream = new FileInputStream(request.getParameter("PhotoFiled"));
     if(Photograph_Stream.available()>0)
     {
          ImgBn.Photograph = new byte[Photograph_Stream.available()] ;
          Photograph_Stream.read( ImgBn.Photograph, 0, Photograph_Stream.available()); 
     }
  }
Uluk Biy
  • 48,655
  • 13
  • 146
  • 153