如何使用jbutton将图像插入数据库并成功显示在jlabel和jtable上

时间:2017-03-20 13:31:46

标签: java sql swing

我创建了以下代码行,以便成功将数据输入到数据库和jtable中,除了我的passport(image)列之外它们都工作,它将字节提交到数据库和jtable但不显示图片在jlabel上,与其他代码不同,它们也在jtextfield,combobox等中显示各自的数据我真的需要它使用这种格式,虽然我用其他方式工作(见下面的代码),但我更喜欢这种方式

private void SubmitActionPerformed(java.awt.event.ActionEvent evt) {
try {
            String reg1 = reg.getText();
            String name1 = name.getText();
            String address1 = address.getText();
            String parent_name1 = Parent_name.getText();
            String Phone_no1 = Phone_no.getText();
            String DateOfBirth1 = ((JTextField)DateOfBirth.getDateEditor().getUiComponent()).getText(); 
            String class1 = ClassComboBox.getSelectedItem().toString();
            String GenderCombobox11 = GenderCombobox1.getSelectedItem().toString();         
            String term1 = TermComboBox.getSelectedItem().toString();
            byte[] passport1 = upload_path.getText().getBytes();
                    format = new ImageIcon(passport1);
                    imagLabel.setIcon(ResizeImage(format));            

            String query = "insert into JSS1_DB (reg_no, name, address, parent_name, parent_no, D_O_B, class, Gender, term, passport)"
                + "values('"+reg1+"','"+name1+"', '"+address1+"', '"+parent_name1+"', '"+Phone_no1+"', '"+DateOfBirth1+"',"
                + " '"+class1+"', '"+GenderCombobox11+"', '"+term1+"', '"+passport1+"')";        


          PreparedStatement pst;
          pst = db_connector.prepareStatement(query);         
          pst.execute();    

           Object[] objs = {reg1, name1, address1, parent_name1, Phone_no1, DateOfBirth1, class1, GenderCombobox11, term1, passport1};

          JOptionPane.showMessageDialog(null, "Submitted successfully");         
          pst.close();          
          JSS1_class JSS1 = new JSS1_class();        
          model = (DefaultTableModel) JSS1.JSS1_Table.getModel();
          model.addRow(objs);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Kindly Input unique Student's registration no");          
            ex.printStackTrace();
        }        

    } 

0 个答案:

没有答案