0

Here i am trying to retrieve a image stored in BLOB format and storing it in another table in the same BLOB format only! And one more issue is this that i have a JFrame named WelcomeStaffs from which i have to access the JTextField named subject_txt and staff_txt values and store it in the database.. But those are only blank! Not getting printed in the output, while i print it!

Here is the code of the FaceRecognizer class..

import static cern.jet.math.Bessel.i1;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import facedetection.*;
import com.googlecode.javacv.*;
import com.googlecode.javacv.cpp.*;
import com.googlecode.javacpp.Loader;
import com.mysql.jdbc.Connection;
import java.sql.Blob;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;


public class FaceRecognizer extends JFrame 
{
  // GUI components
  private FaceRecogPanel facePanel;
  private JButton recogBut;
  private JTextField nameField;     // where the name (and distance info) appears
  private JButton done;
  private JButton exit;


  public FaceRecognizer()
  {
    super("Face Recognizer");
    FaceRecognizer fac;
    Container c = getContentPane();
    c.setLayout( new BorderLayout() );   

    // Preload the opencv_objdetect module to work around a known bug.
    Loader.load(opencv_objdetect.class);

    facePanel = new FaceRecogPanel(this); // the sequence of pictures appear here
    c.add( facePanel, BorderLayout.CENTER);


    // button for recognizing a highlighted face
    done = new JButton("Done");
    recogBut = new JButton("Recognize Face");
    recogBut.addActionListener( new ActionListener() {
       public void actionPerformed(ActionEvent e)
       { nameField.setText("");
         recogBut.setEnabled(false);
         facePanel.setRecog();
       }
    });
    done.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e)
      {
          WelcomeStaffs wc = new WelcomeStaffs();
          Connection con = null;
          try{
            String url = "jdbc:mysql://localhost:3306/facedetection";
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("Conncection Success");
            con = (Connection) DriverManager.getConnection(url, "root", "2204");
            System.out.println("Database Connected");
             }
          catch(Exception ex)
          {
              System.out.println("Exception = "+ex);
          }
          String nm = nameField.getText().toUpperCase();
          String name ="",course="";
          System.out.println("Hello im till here!!");
          String subject = wc.subject_txt.getText();
          String staff = wc.staff_txt.getText();
          String day = wc.day_txt.getText();
          String date = wc.date_txt.getText();
          String time = wc.time_txt.getText();
          int roll_no = 0;
          System.out.println(subject+staff);
          String sql = "SELECT roll_no,name,course,photo FROM students WHERE name=?";
          try {

              PreparedStatement ps = con.prepareStatement(sql);
              ps.setString(1,nm);
              ResultSet rs = ps.executeQuery();
              while(rs.next()) //retrieving values from database and storing it!
              {
                  roll_no = rs.getInt("roll_no");
                  name = rs.getString("name");
                  course = rs.getString("course");
                  byte[]imagedata = rs.getBytes("photo");
                  image = Toolkit.getDefaultToolkit().createImage(imagedata);
                  format = new ImageIcon(image);
                  System.out.println("Retrieved Data!!!!!!!");
              }
          } catch (SQLException ex) {
              Logger.getLogger(FaceRecognizer.class.getName()).log(Level.SEVERE, null, ex);
          }
          try{
              if(roll_no == 0 || name.equals("") || course.equals(""))
              {
                  System.out.println("EMPTY");
              }
              else
              {
                sql = "INSERT INTO attendance(roll_no, name, course, subject, staff, day, time, date, photo) VALUES(?,?,?,?,?,?,?,?,?)";
              PreparedStatement ps1 = con.prepareStatement(sql);
              ps1.setInt(1,roll_no);
              ps1.setString(2,name);
              ps1.setString(3,course);
              ps1.setString(4,subject);
              ps1.setString(5,staff);
              ps1.setString(6,day);
              ps1.setString(7,time);
              ps1.setString(8,date);
              ps1.setBlob(9, (Blob) format);
              int i1 = ps1.executeUpdate();
              JOptionPane.showMessageDialog(null,i1+" Data Inserted");
              }

          }
          catch(Exception ec)
          {
              System.out.println(""+ec);
          }

      }
  });

    nameField = new JTextField(20);   // for the name of the recognized face
    nameField.setEditable(false);

    JPanel p = new JPanel();
    p.add(recogBut);
    p.add( new JLabel("Name: "));
    p.add( nameField);
    p.add(done);
    c.add(p, BorderLayout.SOUTH);


    addWindowListener( new WindowAdapter() {
      public void windowClosing(WindowEvent e)
      { facePanel.closeDown();    // stop snapping pics
        /*Attendance_Chart ac = new Attendance_Chart();
        ac.setVisible(true);*/
        System.exit(0);
      }
    });

    pack();  
    setResizable(false);
    setVisible(true);
  } // end of FaceRecognizer()



  public void setRecogName(final String faceName, final String dist)
  // update face name and its distance in the nameField; called from panel
  { 
    SwingUtilities.invokeLater(new Runnable() {
      public void run() 
      {  nameField.setText( faceName);  
         recogBut.setEnabled(true);
      }
    });
  }  // end of setRecogName()


  // -------------------------------------------------------

  public static void main( String args[] )
  {  
       new FaceRecognizer(); 

  }

  public ImageIcon format = null;
  public Image image = null;
  public Blob blob = null;
} 

// end of FaceRecognizer class

Here is the output i get!

Conncection Success for Staffs
 detection/recognition duration: 9ms
Database Connected for Staffs
Table Created
Conncection Success
Database Connected
Hello im till here!!

Retrieved Data!!!!!!!
java.lang.ClassCastException: javax.swing.ImageIcon cannot be cast to `java.sql.Blob`

Please help! Thank you!

  • byte[]imagedata = rs.getBytes("photo"); ps1.setBlob(9,imagedata); Do something like this, it will work – Noor Nawaz Mar 25 '16 at 09:55
  • thanks for giving me a hint! it worked though by ps1.setBytes(9,imagedata); Thank You! – Jerry Pillai Mar 25 '16 at 10:08
  • By the way any suggestion for those values of textfields please? I am unable to retrieve those values! please! – Jerry Pillai Mar 25 '16 at 10:10
  • WelcomeStaffs wc = new WelcomeStaffs(); U just create its object, it has no value(s), if it is a JFrame then u need to show that frame and insert some values in that fields. After that u will be able to get text from those fields – Noor Nawaz Mar 25 '16 at 10:14
  • Actually what i am doing here is I am inputting some values in WelcomeStaffs frame and then a button next is there in the frame itself it will open up my FaceRecognizer and then in FaceRecognizer a button done and a textfield is there.. once i press the done button the values of WelcomeStaffs and FaceRecognizer get stored in the database! i hope u understood! bit complicated! please help and thanks! – Jerry Pillai Mar 25 '16 at 10:36
  • For [example](http://stackoverflow.com/questions/35069359/trying-to-retrieve-both-text-and-blob-from-mysql-to-jtable/35072936#35072936), [example](http://stackoverflow.com/questions/13833089/how-to-show-an-image-from-ms-access-to-jpanel-in-java-netbeans/13833233#13833233), [example](http://stackoverflow.com/questions/29983710/displaying-images-from-mysql-database-on-a-single-column-of-jtable/29983992#29983992), [example](http://stackoverflow.com/questions/20752432/convert-bufferedinputstream-into-image/20753089#20753089) (that one does read and write) – MadProgrammer Mar 25 '16 at 11:55
  • You need to write the image data to a `Blob` type, you're going to have to write the image to an `InputStream` and use that via the `setBinaryStream` method a `PreparedStatement`. To do this, you're going to need to draw the `ImageIcon` on a `BufferedImage` and write it out via `ImageIO.write` – MadProgrammer Mar 25 '16 at 11:58

0 Answers0