Hugo Monteiro
Hugo Monteiro

Reputation: 17

Creating JTextArea

i'm using some code to getfilenames and store it on a DB and everything works fine, but i wanted to show what files it stored on a TextArea, how can i do it?

Here is the code used to store the file names. BTW i'm doing this in 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);
        }

}

Thanks in advance.

Upvotes: 0

Views: 167

Answers (4)

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33544

1. Create a JTextArea like this...

   JTextArea txt = new JTextArea();

2. Read the file which contains the data from the Data Base.

3. And then write it to the 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);

Upvotes: 0

lukastymo
lukastymo

Reputation: 26809

  • read file names from database:

"Select fileName from YourTable"

  • add the names to textarea in for-each

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

Upvotes: 1

Reimeus
Reimeus

Reputation: 159874

You can add:

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

immediately after your execute statement.

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347332

Somewhere here you could

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

Of course you need to have build the ui first! Check out this for more elp

Upvotes: 0

Related Questions