创建JTextArea

时间:2012-07-23 19:28:51

标签: java netbeans-7

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

答案 0 :(得分:1)

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

“从YourTable中选择fileName”

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

textArea.append(fileName +“\ n”);

答案 1 :(得分: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");

当然你需要先建立ui!查看this了解更多elp

答案 2 :(得分:0)

您可以添加:

textarea.append(text + " stored successfully\n");
执行声明后立即

答案 3 :(得分: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);