Roberto,
 
Lá você encontra um exemplo de leitura de arquivo texto e gravação de uma tabela, etc.
Acredito que ajude.
 
Eliseu Pfaffenseller
Analista de Sistemas Senior.
 
======================================================
 
Fonte com exemplo de leitura de um arq.texto, utilização da classe StringTokenizer, e gravação de uma tabela no BD.
 
/*Example: Parsing a text file into a database table
In the course of modernizing a record keeping system, you encounter a flat file of data that was created long before the rise of the modern relational database. Rather than type all the data from the flat file into the DBMS, you may want to create a program that reads in the text file, inserting each row into a database table, which has been created to model the original flat file structure.
In this case, we examine a very simple text file. There are only a few rows and columns, but the principle here can be applied and scaled to larger problems. There are only a few steps:
Open a connection to the database.
Loop until the end of the file:
Read a line of text from the flat file.
Parse the line of text into the columns of the table.
Execute a SQL statement to insert the record.
Here is the code of the example program:
*/
import java.io.*;
import java.sql.*;
import java.util.*;
 
public class TextoParaTabelaBD { //ToDatabaseTable {
   private static final String DB = "contacts",
                               TABLE_NAME = "records",
                               HOST = "jdbc:mysql://db_lhost:3306/",
                               ACCOUNT = "account",
                               PASSWORD = "nevermind",
                               DRIVER = "org.gjt.mm.mysql.Driver",
                               FILENAME = "records.txt";
 
   public static void main (String[] args) {
      try {
 
         // connect to db
         Properties props = new Properties();
         props.setProperty("user", ACCOUNT);
         props.setProperty("password", PASSWORD);
 
         Class.forName(DRIVER).newInstance();
         Connection con = DriverManager.getConnection(
            HOST + DB, props);
         Statement stmt = con.createStatement();
 
         // open text file
         BufferedReader in = new BufferedReader(
                                new FileReader(FILENAME));
 
         // read and parse a line
         String line = in.readLine();
         while(line != null) {
 
            StringTokenizer tk = new StringTokenizer(line);  //aqui você pode testar o ";"
            String first = tk.nextToken(),
                   last = tk.nextToken(),
                   email = tk.nextToken(),
                   phone = tk.nextToken();
 
            // execute SQL insert statement
            String query = "INSERT INTO " + TABLE_NAME;
            query += " VALUES(" + quote(first) + ", ";
            query += quote(last) + ", ";
            query += quote(email) + ", ";
            query += quote(phone) + ");";
            stmt.executeQuery(query);
 
            // prepare to process next line
            line = in.readLine();
         }
         in.close();
      }
 
      catch( Exception e) {
         e.printStackTrace();
      }
   }
 
   // protect data with quotes
   private static String quote(String include) {
      return("\"" + include + "\"");
   }
}
 
===========================================================
 
-----Original Message-----
From: Roberto da Silva [mailto:[EMAIL PROTECTED]
Sent: 01 July, 2003 09:16
To: [EMAIL PROTECTED]
Subject: [java-list] Sou mais um novato!!!

 

Moçada, procurei pela Internet mas não achei o que queria, alguém poderia me enviar um código bem simples, de como abrir um arquivo, ler....estou usando o Java.io mas ainda não consegui entender direito o Java então estou domando porrada dele!!!!

 

Obrigado

Roberto da Silva

Responder a