这里我试图检索以BLOB格式存储的图像,并将其以相同的BLOB格式存储在另一个表中! 还有一个问题这是我有一个名为WelcomeStaffs的JFrame,我必须从中访问名为subject_txt和staff_txt值的JTextField并将其存储在数据库中..但这些只是空白!我打印时没有在输出中打印!
以下是FaceRecognizer类的代码..
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
这是我得到的输出!
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`
请帮忙! 谢谢!