Connection cn;
Statement st;
PreparedStatement pstmt=null;
PreparedStatement pst;
ResultSet rs;
Object fname, mname, lname, bdate, nation, statusq,InstNo, photo, combo, place, mimi;
int status;
private void btnNextMouseClicked(java.awt.event.MouseEvent evt) {
fname=txtFirtsName.getText();
lname=txtLastName.getText();
mname=txtMiddleName.getText();
InstNo=txtInstituteNo.getText();
place=txtPlacBirth.getText();
//photo=txtPicturePath.getText();
status=combostatus.getSelectedIndex();
Object dave=((JTextField)chooserBirthDate.getDateEditor().getUiComponent()).getText();
Object isa=combonation.getSelectedItem();
Object photo=pictureName.getClass();
// pst.setBytes();
// bdate=((JTextField)chooserBirthDate.getDateEditor().getUiComponent()).getText();
if(status==1){
statusq=("In Active");
}
else{
statusq="Active";}
try{
String addrecords="insert into brothers(FirstName, MiddleName, LastName, BirthDate, BirthPlace, Nationality, InstituteNumber, Status, Picture) values('"+
fname +"', '" +
mname +"', '" +
lname +"', '" +
dave +"', '" +
place +"', '" +
isa +"', '" +
InstNo +"', '" +
statusq +"', '" +
photo +"')";
//wrapField();
st.executeUpdate(addrecords);
}
我的文件选择器代码并将图片转换为二进制:
private void btnFileChooserActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser izoChooser=new JFileChooser();
izoChooser.showOpenDialog(null);
File pictureBrother=izoChooser.getSelectedFile();
pictureName=pictureBrother.getAbsolutePath();
txtPicturePath.setText(pictureName);
try {
File image=new File(pictureName);
FileInputStream fis=new FileInputStream(image);
ByteArrayOutputStream bos=new ByteArrayOutputStream();
byte[] buf=new byte[1024];
for(int readNum; (readNum=fis.read(buf))!=-1;){
bos.write(buf,0,readNum);
}
person_image=bos.toByteArray();
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
//e.printStackTrace();
}
我在代码底部声明了这些变量:
private javax.swing.JTextField txtTrial;
// End of variables declaration
String pictureName=null;
int s=0;
byte[] person_image=null;
}
问题:我的代码没有抛出任何错误,但在数据库中,它只在图片的 BLOB 列上注册了 8B,无论我选择哪张图片。但是,如果直接从数据库上传图片,然后转到数据库然后上传,图片就会上传到数据库。可能是什么问题?
我的主要问题在这里:我猜, Object photo=pictureName.getClass();
应该是什么,因为如果我要使用准备好的声明,那么它应该是:preparedStatemnt.setBytes(10, person_image);。
但是没有 .getBytes();