0

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

}

提前致谢。

4

4 回答 4

1
  • 从数据库中读取文件名:

“从 YourTable 中选择文件名”

  • 在 for-each 中将名称添加到 textarea

textArea.append(fileName + "\n");

于 2012-07-23T19:38:09.630 回答
0

你可以在这里的某个地方

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");

当然,您需要先构建用户界面!查看以获取更多信息

于 2012-07-23T19:37:48.417 回答
0

你可以加:

textarea.append(text + " stored successfully\n");

在您的执行语句之后立即。

于 2012-07-23T19:38:05.923 回答
0

1.像这样创建一个 JTextArea...

   JTextArea txt = new JTextArea();

2.从数据库中读取包含数据的文件。

3.然后将其写入JTextArea

   File f = new File("d:\\My.txt");
   FileReader fr = new  FileReader(f);
   BufferedReader br = new BufferedReader(fr);


   StringBuilder sb = new StringBuilder();
   String s = null;

   while ((br.readLine())!=null){

       sb.append(br.readLine());

     }

  s = sb.toString();

 txt.setText(s);
于 2012-07-23T19:38:50.770 回答