Java 如何使用jbutton将图像插入数据库并成功地显示在jlabel和jtable上
我创建了以下代码行,以便成功地将数据输入数据库和jtable,除了我的passport(image)列之外,它们都在工作,它将字节提交到数据库和jtable中,但不在jlabel上显示图片,这与在jtextfield中也显示各自数据的其他代码不同,combobox e.t.c.我真的需要它使用这种格式工作,虽然我用另一种方式(见下面的代码)工作,但我更喜欢这种方式Java 如何使用jbutton将图像插入数据库并成功地显示在jlabel和jtable上,java,sql,swing,Java,Sql,Swing,我创建了以下代码行,以便成功地将数据输入数据库和jtable,除了我的passport(image)列之外,它们都在工作,它将字节提交到数据库和jtable中,但不在jlabel上显示图片,这与在jtextfield中也显示各自数据的其他代码不同,combobox e.t.c.我真的需要它使用这种格式工作,虽然我用另一种方式(见下面的代码)工作,但我更喜欢这种方式 private void SubmitActionPerformed(java.awt.event.ActionEvent evt)
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();
}
}
您应该查找“SQL注入”并适当地修复查询是否可以添加
ResizeImage
类?好的…这是一个@Berger公共图标ResizeImage(ImageIcon格式){Image img=format.getImage();Image mypassport=img.getScaleInstance(imagLabel.getWidth(),imagLabel.getHeight(),Image.SCALE\u平滑);ImageIcon mypassport1=新的ImageIcon(mypassport);诚实地返回mypassport1;}除非您必须返回,否则不要返回。将图像存储在“已知”位置,然后存储图像的路径而不是存储OK Thank@MadProgrammer