我正在使用一些代码获取文件名并将其存储在数据库上,并且一切正常,但是我想显示它存储在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);
        }

}


提前致谢。

最佳答案

在这里的某个地方你可以

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以获得更多内容

关于java - 创建JTextArea,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11619183/

10-10 10:59