Insertar un registro a mysql desde un txt.
Publicado por marcel (6 intervenciones) el 27/03/2015 20:44:27
amigos tengo un problema quiero insertar un registro de datos que obtengo de un txt lo cual ya me funciona toma las variables del txt pero nose xq no me lo inserta a MYSQL este es.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package InsertarRegistro ;
import java.lang.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class InsertarRegistro {
private String rutaArchivo;
private String linea;
String[] parts;
String part1 ;
String part2 ;
String part3 ;
String part4 ;
String servidor ;
String usuarioDB ;
String passwordDB ;
protected Connection conn = null;
protected Statement stmt = null;
/**
* Conecta con la base de datos 'cliente' de un servidor
* MySQL local.
* @throws java.lang.Exception
*/
public void conectar() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
servidor = "jdbc:mysql://localhost:3306/ecos";
usuarioDB="root";
passwordDB="";
this.conn = DriverManager.getConnection(servidor,usuarioDB,passwordDB);
this.stmt = conn.createStatement();
}
public void leerArchivo(){
try{
this.rutaArchivo="c:/ecografia/archivo.txt";
FileReader fr=new FileReader(rutaArchivo);
BufferedReader entradaArchivo=new BufferedReader(fr);
linea=entradaArchivo.readLine();
while (linea != null) {
parts = linea.split("\\|");
part1 = parts[0]; // 004
part2 = parts[1]; // 034556
part3 = parts[2]; // 004
part4 = parts[3]; // 004
System.out.println(linea);
System.out.println(part1);
System.out.println(part2);
System.out.println(part3);
System.out.println(part4);
linea=entradaArchivo.readLine();
}
}catch(IOException ex){
System.out.println("Error en la apertura del archivo "+ex.toString());
}
}
/**
* @throws Exception DOCUMENT ME!
*/
@SuppressWarnings("empty-statement")
public void desconectar() throws Exception {
if (this.stmt != null) {
try {
this.stmt.close();
} catch (SQLException SQLE) {
;
}
}
if (this.conn != null) {
try {
this.conn.close();
} catch (SQLException SQLE) {
;
}
}
}
protected void insertarUnCliente() throws SQLException {
String sql = "insert into semst01 " + "(CEDPAC, NOMPAC, DIRPAC, TELPAC) " + " values ('"+part2+"','"+part4+"','"+part1+"','"+part3+"')";
stmt.execute(sql);
}
public static void main(String[] args) throws ClassNotFoundException, SQLException{
InsertarRegistro ie = new InsertarRegistro();
try {
ie.conectar();
ie.leerArchivo();
ie.insertarUnCliente();
ie.desconectar();
} catch (Exception e) {
}
System.out.println("Ok!");
}
}
Valora esta pregunta


0