我使用一些代码来获取文件名并将其存储在数据库中,一切正常,但我想显示它在TextArea上存储了哪些文件,我该如何做呢?
以下是用于存储文件名的代码。顺便说一句,我正在用NetBeans做这件事。
private void ActualizerBDActionPerformed(java.awt.event.ActionEvent evt) {
Conectar();
File folder = null;
try {
String ppp = new File(".").getCanonicalPath();
folder = new File(ppp + "\\ImagensDB");
} catch (IOException iOException) {
}
File[] listOfFiles = folder.listFiles();
String query = "insert into dados (Num, Nome, Autor, Data, Preco, Categoria)" +"values(?,?,?,?,?,?)";
PreparedStatement prepStmt=null;
try {
prepStmt = con.prepareStatement(query);
} catch (SQLException ex) {
Logger.getLogger(JanelaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
}
for (int j = 0; j < listOfFiles.length; j++) {
if (listOfFiles[j].isFile()) {
try {
String text = listOfFiles[j].getName();
String txsp[] = text.split("-");
prepStmt.setString(1, txsp[0]);
prepStmt.setString(2, txsp[1]);
prepStmt.setString(3, txsp[2]);
prepStmt.setString(4, txsp[3]);
prepStmt.setString(5, txsp[4]);
prepStmt.setString(6, txsp[5]);
prepStmt.execute(); // executa o INSERT
} catch (SQLException ex) {
Logger.getLogger(JanelaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
JOptionPane.showMessageDialog(null, "Dados Introduzidos com Sucesso!");
try {
con.close();
} catch (SQLException ex) {
Logger.getLogger(JanelaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
}
}提前谢谢。
发布于 2012-07-24 03:37:48
在这里的某个地方你可以
String text = listOfFiles[j].getName();
String txsp[] = text.split("-");
prepStmt.setString(1, txsp[0]);
prepStmt.setString(2, txsp[1]);
prepStmt.setString(3, txsp[2]);
prepStmt.setString(4, txsp[3]);
prepStmt.setString(5, txsp[4]);
prepStmt.setString(6, txsp[5]);
prepStmt.execute(); // executa o INSERT
myTextArea.append(text + "\n");当然,您需要先构建ui!有关更多帮助,请查看this
发布于 2012-07-24 03:38:10
“从YourTable选择fileName”
中的文本区域
textArea.append(fileName + "\n");
发布于 2012-07-24 03:38:06
您可以添加:
textarea.append(text + " stored successfully\n");紧跟在execute语句之后。
https://stackoverflow.com/questions/11619183
复制相似问题