Boas,
Brother, vc tem de ler o file linha a linha.
Depois faz uma rotina pra inserir num Array ou HashMap.
Classe Conexao
Tente Assim:
import java.sql.Connection;
import java.sql.DriverManager;
import com.sybase.jdbc3.jdbc.*;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
public class Conexao {
private Connection conn;
private String user;
private String pass;
public Conexao()
{
conn=null;
}
public void carregarDriver()
{
try
{
Class.forName("com.sybase.jdbc3.jdbc.SybDriver");
System.out.println("Driver carregado");
}
catch(Exception e)
{
System.out.println("Erro de leitura "+e);
}
}
public void abrirConexao()
{
try
{
carregarDriver();
System.out.println("A Conectar...");
//setConn(DriverManager.getConnection("jdbc:sybase:Tds:localhost:2638", "***", "***"));
setConn(DriverManager.getConnection("jdbc:sybase:Tds:localhost:2638", getUser().toString(), getPass().toString()));
System.out.println("Conectado!!!");
}
catch (Exception e)
{
System.out.println("O erro da Conexao é: "+e);
JOptionPane.showMessageDialog(null, "Login Inválido!!!","ERROR!!!", 0);
}
}
public void fecharConexao()
{
try {
getConn().close();
} catch (SQLException ex) {
Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE, null, ex);
}
}
public Connection getConn() {
return conn;
}
public void setConn(Connection conn) {
this.conn = conn;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
}
Classe Ficheiro
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
/**
*
* @author User
*/
public class Ficheiro {
private String strLine;
private String file;
private int i;
private HashMap hm;
public Ficheiro()
{
i = 1;
hm = new HashMap();
}
public void lerFile(String f)
{
try
{
FileInputStream fstream = new FileInputStream(f);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
//Ler ficheiro linha a linha
while ((strLine = br.readLine()) != null)
{
//Guarda em cada posiçao do HM uma linha do ficheiro
System.out.println("Linha: "+strLine);
getHm().put(i,strLine);
i++;
}
in.close();
}catch (Exception e)
{
System.err.println("Error: " + e.getMessage());
}
}
public String getFile() {
return file;
}
public HashMap getHm() {
return hm;
}
public void setFile(String file) {
this.file = file;
}
}
Classe TratarDados
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
public class TratarDados {
private String tabela;
private String caracter;
public void tratarDados(HashMap h,Connection cn)
{
int y = 0;
try
{
for(int j=1;j<=h.size();j++)
{
if(j==1)
{
String sql = "create table "+ getTabela().toString()+"(";
String auxSql;
String s = h.get(j).toString();
String[] auxS = s.split(getCaracter().toString(),s.length());
for(int i=0;i<auxS.length;i++)
{
if(y==0) {
auxSql = auxS[i]+ " text null primary key,";
//auxSql = "'"+auxS[i]+"'"+ " text null primary key,";
y=1;
}
else
{
if(i+1==auxS.length)
auxSql = auxS[i]+ " text null";
else
auxSql = auxS[i]+ " text null,";
}
sql = sql + auxSql;
}
sql = sql + ");";
System.out.println(sql);
java.sql.Statement stmt1 = cn.createStatement();
stmt1.executeUpdate(sql);
}
else
{
String sql = "insert into "+getTabela().toString()+" values(";
String auxLinha="";
String letra="#";
auxLinha = h.get(j).toString().replaceAll(getCaracter().toString(), letra);
String[] data = auxLinha.split(letra,auxLinha.length());
for(int w=0; w<data.length;w++)
{
if(w+1==data.length)
sql = sql + "'"+data[w]+"'";
else
sql = sql +"'"+data[w]+"'"+",";
}
sql = sql + ")";
java.sql.Statement stmt2 = cn.createStatement( );
stmt2.execute(sql);
System.out.println(sql);
}
}
}
catch(SQLException e)
{
System.out.println("Erro: "+e.getMessage());
e.printStackTrace();
}
}
public String getTabela() {
return tabela;
}
public void setTabela(String tabela) {
this.tabela = tabela;
}
public String getCaracter() {
return caracter;
}
public void setCaracter(String caracter) {
this.caracter = caracter;
}
}
Na hora de incovar vc faz assim:
private void <seuMetodo>(java.awt.event.MouseEvent evt) {
try {
Ficheiro f = new Ficheiro();
f.setFile(txtFicheiro.getText().toString());//Aponta pro file que vc quer, no meu caso eu tou indo buscar o file à textfield
f.lerFile(f.getFile());
//Configura conexao
Conexao cn = new Conexao();
cn.setUser(txtUser.getText().toString());
cn.setPass(txtPass.getText().toString());
cn.abrirConexao();
TratarDados td = new TratarDados();
td.setTabela(txtTabela.getText().toString());
td.setCaracter(txtCaracter.getText().toString());
td.tratarDados(f.getHm(), cn.getConn());
JOptionPane.showMessageDialog(null, "Exported File Success!!!","Alert",JOptionPane.NO_OPTION);
cn.fecharConexao();
}catch(Exception e) {
//JOptionPane.showMessageDialog(null,e.getMessage());
//JOptionPane.showMessageDialog(null, "Ola","Erro",JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
}
Tente isso e diga se funcionou.
Vlw cara.
//Graveyard