I am fetching a image from database and directly read it into panel using Imageio.read method with Bufferimage reference variable but it gives me error javax.imageio.IIOException: Can't read input file! I don't understand why it gives me error.
My database code:
public class ValidateClass {
public static byte[] validateImageAndEmpId(Connection con, String empId) {
byte[] imagedata = null;
try {
PreparedStatement ps = con.prepareStatement("select image from fingerprint where empId=?");
ps.setString(1, empId);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
Blob blob = rs.getBlob("image");
imagedata = blob.getBytes(1, (int) blob.length());
System.out.println("Length" + imagedata);
System.out.println("testlen" + imagedata.length);
}
} catch (Exception e) {
e.printStackTrace();
}
return imagedata;
}
}
Code where I call this method:
public CEntityForm() {
jButtonStep1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButtonStep3_actionPerformed(e);
}
});
/*jButtonStep1 for 1 to 1 match fingerprint matching which is not in use */
jtool.add(jButtonStep1);
try {
// Taking picture1
//Set picture new
m_bimage1 = ImageIO.read(new File(new java.io.File("").getAbsolutePath() + "\\L1Right.Jpeg"));
m_panel1.setBufferedImage(m_bimage1);
//Send image for skeletinization
m_finger1.setFingerPrintImage(m_bimage1);
finger1 = m_finger1.getFingerPrintTemplate();
//See what skeletinized image looks like
m_bimage1 = m_finger1.getFingerPrintImageDetail();
m_panel1.setBufferedImage(m_bimage1);
//fingerprint matching details of finger 1 in number format so commented jtextfield2
// jTextField1.setText(m_finger1.ConvertFingerPrintTemplateDoubleToString(finger1));
/*end of picture 1 details*/
// Taking picture2
//Set picture new
Connection con = Conn.getConnection();
byte[] bytearry = ValidateClass.validateImageAndEmpId(con, $emp_Id);
// System.out.println("Input="+input);
m_bimage2 = ImageIO.read(new ByteArrayInputStream(bytearry));
// m_bimage2 = ImageIO.read(new File(new java.io.File("").getAbsolutePath() + "\\L1Right_Copy.Jpeg"));
m_panel2.setBufferedImage(m_bimage2);
//Send image for skeletinization
m_finger2.setFingerPrintImage(m_bimage2);
finger2 = m_finger2.getFingerPrintTemplate();
//See what skeletinized image looks like
m_bimage2 = m_finger2.getFingerPrintImageDetail();
m_panel2.setBufferedImage(m_bimage2);
//fingerprint matching details of finger 2 in number format so commented jtextfield2
//jTextField2.setText(m_finger2.ConvertFingerPrintTemplateDoubleToString(finger2));
/*end of picture 1 details*/
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.PLAIN_MESSAGE);
}
this.getContentPane().setLayout(new GridLayout(2, 2));
this.getContentPane().add(m_panel1);
this.getContentPane().add(m_panel2);
this.getContentPane().add(jtool);
this.setTitle("Entity");
this.setSize(new Dimension(800, 700));
}
Main Method
public class Testmain {
public static void main(String[] args) {
try{
new CEntityForm().setVisible(true);
}catch(Exception e){
e.printStackTrace();
}
}
Output:
javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(ImageIO.java:1301)
at main.CEntityForm.<init>(CEntityForm.java:81)
at main.Testmain.main(Testmain.java:18)
The exception seems to come from your other call to ImageIO.read(); the first one in the constructor. There you're passing a file that presumably doesn't exist.
I have an item database (jdbc) the functionality is: insert, delete, and update using GUI JTable, all work good but I need to display the image in the JTable but I couldn't :(
If you could help me in that, here's the code for the main functionality:
fill Table method:
try{
connection= DriverManager.getConnection(url, usr, pas);
statement = connection.createStatement();
resultSet = statement.executeQuery("select * from item");
itemTable.setModel(DbUtils.resultSetToTableModel(resultSet));
} catch(SQLException ex){
JOptionPane.showMessageDialog(null, ex);}
add method:
try{
PreparedStatement insert =connection.prepareStatement("INSERT INTO items"
+"(name, price, image, ID)"
+"VALUES(?,?,?,?)");
insert.setString(1, nameTextField.getText());
insert.setDouble(2, Double.parseDouble(priceTextField.getText()));
insert.setByte(3, image);
insert.setInt(4, Integer.parseInt(IDTextField.getText()));
int row = insert.executeUpdate();
fillTable();
}catch (SQLException ex){
JOptionPane.showMessageDialog(null,ex);}
update method:
try{
PreparedStatement update =connection.prepareStatement("update item set "
+ "name=? , price=?, image=? where id=?""
+"VALUES(?,?,?,?)");
update.setString(1, nameTextField.getText());
update.setDouble(2, Double.parseDouble(priceTextField.getText()));
update.setByte(3, image);
update.setInt(4, Integer.parseInt(IDTextField.getText()));
int row = update.executeUpdate();
fillTable();
}catch (SQLException ex){
JOptionPane.showMessageDialog(null,ex);}
delete method:
try{
Statement delete = connection.createStatement();
delete.executeUpdate("delete from item where id="+IDTextField.getText());
priceTextField.setText("");
nameTextField.setText("");
idTextField.setText("");
fillTable();
}catch (SQLException ex){
JOptionPane.showMessageDialog(null,ex);}
A button event to choose image from JFileChooser:
String imagePath = null;
JFileChooser file;
private void ChooseImageButtonActionPerformed(java.awt.event.ActionEvent evt){
int result = file.showSaveDialog(null);
if(result == JFileChooser.APPROVE_OPTION){
File selectedFile = file.getSelectedFile();
String path = selectedFile.getAbsolutePath();
imagePath = path;
}
}
The important methods are fillTable() and add().
fillTable method could be like this:
//you need a TableModel, let´s call it model and let´s call your JTable myJTable
TableModel model = (javax.swing.table.DefaultTableModel) myJTable.getModel();
myJTable.setModel(model);
//try to add results
try {
connection = DriverManager.getConnection(url, usr, pas);
statement = connection.createStatement();
resultSet = statement.executeQuery("select * from item");
while (resultSet.next()) {
//after your query, get the Image
Blob blob = resultSet.getBlob(3);//column 3
byte[] data = blob.getBytes(1, (int) blob.length());
BufferedImage img = null;
img = ImageIO.read(new ByteArrayInputStream(data));
Icon imgToTable = imageToIcon(img);//use the method below
//add row to your table (throught the model)
model.addRow(new Object[]{resultSet.getObject(1).toString(), resultSet.getObject(2).toString(), imgToTable,resultSet.getObject(4).toString()});
//itemTable.setModel(DbUtils.resultSetToTableModel(resultSet));
}
resultSet.close();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex);
}
Add method could be like this:
try{
PreparedStatement insert =connection.prepareStatement("INSERT INTO items"
+"(name, price, image, ID)"
+"VALUES(?,?,?,?)");
insert.setString(1, nameTextField.getText());
insert.setDouble(2, Double.parseDouble(priceTextField.getText()));
//i recommend to use something like this to configure you image
file = new File(imagePath);
fis = new FileInputStream(file);
if (null == fis) {
fis = new FileInputStream(imagePath);
}
//configure image
insert.setBinaryStream(3, fis);
// insert.setByte(3, image);
insert.setInt(4, Integer.parseInt(IDTextField.getText()));
int row = insert.executeUpdate();
fillTable();
}catch (SQLException ex){
JOptionPane.showMessageDialog(null,ex);}
And use this to convert from Image to Icon (to put it in a Table Cell):
//from Image a Icon
public Icon imageToIcon(Image image) {
ImageIcon imgIcon = new ImageIcon(image);
Icon iconReturn = (Icon) imgIcon;
return iconReturn;
}
If in your database you are not using blob let me know.
I am trying to select the image from a table which is retrieved from the database and then displays it as an icon on a label.
i found it difficult to convert object returned by getValueAt() method to bufferedImage object..
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try
{
pr=con.prepareStatement("SELECT `sender id`, `image`, `status` FROM `transfer` WHERE `receiver_id`=?");
pr.setString(1,jTextField3.getText());
rs=pr.executeQuery();
jTable1.setModel(DbUtils.resultSetToTableModel(rs));
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this, e);
}
}
private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {
try
{
int index=jTable1.getSelectedRow();
TableModel model=jTable1.getModel();
**BufferedImage ima=(BufferedImage) model.getValueAt(index,1);**
JLabel l=new JLabel(new ImageIcon(ima));
imagePane.getViewport().add(l);
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this, e);
}
}
You should first convert blob to InputStream, and then to BufferedImage:
TableModel model=jTable1.getModel();
Blob myBlob = model.getValueAt(index, 1);
InputStream myIS = myBlob.getBinaryStream();
BufferedImage myImage = ImageIO.read(myIS);
How do I get a Text Area in Netbeans to display content that I already have saved in a Text File? I want the text area txtAllOrders to display the content that I have in a text file Output.txt when I click on the button btnViewOrders.
I'm not a pro programmer in Java so pls correct me if I'm wrong, but I would try this:
FileInputStream fstream = new FileInputStream("YourFile.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String sw ="";
try
{
while(sw != null)
{
sw = br.readLine();
txtAllOrders.append(sw + "\n");
}
} catch (IOException ex) {
ex.printStackTrace();
}
Try to put the following code to the click event of your btnViewOrders
File file = new File("Output.txt");
FileInputStream fis = null;
String text = "";
try {
fis = new FileInputStream(file);
int content;
while ((content = fis.read()) != -1) {
text += (char) content;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
txtAllOrders.setText(text);
Everytime I delete something from my JButton it deletes in the database but my jpanel wont update...
I wonder if there is an easy fix for this?
private void DeleteActionPerformed(java.awt.event.ActionEvent evt) {
infLabel.setText(inform[2]);
Connection connection = FlatFinder.getConnection();
Statement selectStmt = null;
try {
selectStmt = connection.createStatement();
Flat activeFlat = (Flat) AdminAll.getSelectedValue();
String sql = String.format(
"DELETE FROM `flats` WHERE flat_id = %d",
activeFlat.getId()
);
selectStmt.executeUpdate(sql);
} catch (SQLException ex) {
Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex);
}
this.repaint();
}
This is my current code. Is there any easy solution to update the jpanel when I press the delete button?