我知道我们可以使用fc.setMultiSelectionEnabled(true)
启用多项选择。但我需要一种方法来添加所有选定的
文件到数据库中。
我已尝试使用getFilesSelected()
,但问题是当我点击输入时,只会将第一个选定的图像添加到数据库中。
这就是我尝试过的,
fc.addChoosableFileFilter(new ImageFilter());
if (fc.showOpenDialog(btnBrowse) == JFileChooser.APPROVE_OPTION){
textField.setEditable(true);
img_name.setEditable(true);
textField.setText(fc.getSelectedFile().getAbsolutePath());
img_name.setText(fc.getSelectedFile().getAbsolutePath().substring(fc.getSelectedFile().getAbsolutePath().lastIndexOf("\\")+1));
String ext = fc.getSelectedFile().getAbsolutePath().substring(fc.getSelectedFile().getAbsolutePath().lastIndexOf("\\")+1);
file_ext = ext.substring(ext.indexOf('.'),(ext.length()));
String query = " insert into load_images(format,image_name,file_loc,photo,date_field) values (?,?,?,?,?)";
preparedStmt = con.prepareStatement(query);
preparedStmt.setString (1,file_ext);
preparedStmt.setString (2,img_name.getText());
preparedStmt.setString (3, textField.getText());
File f=new File(textField.getText());
int size=(int) f.length();
FileInputStream fis=new FileInputStream(f);
preparedStmt.setBinaryStream(4,fis,size);
preparedStmt.setTimestamp(5, getCurrentTimeStamp());
preparedStmt.execute();
答案 0 :(得分:0)
我得到了它的工作。我创建了一个函数来将每个图像插入到数据库中。
以下是代码:
fc.setSelectedFile(null);
fc.setCurrentDirectory(new java.io.File("C:/Users"));
fc.setDialogTitle("File Browser");
fc.setAcceptAllFileFilterUsed(false);
fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
fc.resetChoosableFileFilters();
fc.setMultiSelectionEnabled(true);
fc.addChoosableFileFilter(new ImageFilter());
if (fc.showOpenDialog(btnBrowse) == JFileChooser.APPROVE_OPTION)
{
inputFiles = fc.getSelectedFiles();
for (File f: inputFiles)
{
path = f.getAbsolutePath();
imgName = f.getName();
textField.setEditable(true);
img_name.setEditable(true);
textField.setText(path);
img_name.setText(imgName);
file_ext = imgName.substring(imgName.indexOf('.'),(imgName.length()));
insert(file_ext,imgName,path);
}
}
这是函数,
public void insert(String fileext, String imgNam, String url)
{
try
{
Class.forName("oracle.jdbc.OracleDriver").newInstance();
Connection con = DriverManager
.getConnection("jdbc:oracle:thin:@10.180.86.139:1521:syndupgdb","fcrhost","FCRHOST");
String query = " insert into load_images(format,image_name,file_loc,photo,date_field) values (?,?,?,?,?)";
preparedStmt = con.prepareStatement(query);
preparedStmt.setString (1,fileext);
preparedStmt.setString (2,imgNam);
preparedStmt.setString (3,url);
File fil=new File(url);
int size=(int) fil.length();
FileInputStream fis=new FileInputStream(fil);
preparedStmt.setBinaryStream(4,fis,size);
preparedStmt.setTimestamp(5, getCurrentTimeStamp());
preparedStmt.execute();
con.close();
JOptionPane.showConfirmDialog(null, "Your data Has been Inserted","Result", JOptionPane.DEFAULT_OPTION,JOptionPane.PLAIN_MESSAGE);
}
catch (Exception e1)
{
System.out.println("Exception:" + e1);
}
}