base de datos a .txt
Publicado por juan (1 intervención) el 21/04/2010 06:42:12
tengo un pequeño problema estoy haciendo un codigo que recoja los datos de la base de datos y los guardes en un txt he probado de muchas formas pero no me resulta queiro saber que es lño malo del codigo porfavor
import java.sql.*;
import java.io.*;
public class TxtProfesores {
public static void main(String[]args)throws IOException,SQLException,ClassNotFoundException {
String dbUrl = "jdbc:odbc:puenteOdbc";
String user = "";
String password = "";
FileReader entrada = null;
FileWriter salida= null;
int P=0;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//BufferedReader entrada = new BufferedReader(new InputStreamReader(System.in));
Connection c = DriverManager.getConnection( dbUrl,user, password);
Statement s = c.createStatement();
String querySql="SELECT * FROM Profesor";
ResultSet r = s.executeQuery(querySql);
while(r.next()) {
try {
entrada=new FileReader("r");
salida=new FileWriter("Profesores.txt");
while((P=entrada.read())!=-1){
salida.write(P);
}
}catch (IOException ex) {
System.out.println(ex);
}finally{
if(entrada!=null){
try{
entrada.close();
}catch(IOException ex){}
}
if(salida!=null){
try{
salida.close();
}catch(IOException ex){}
}
}
}
r.close();
}
}
import java.sql.*;
import java.io.*;
public class TxtProfesores {
public static void main(String[]args)throws IOException,SQLException,ClassNotFoundException {
String dbUrl = "jdbc:odbc:puenteOdbc";
String user = "";
String password = "";
FileReader entrada = null;
FileWriter salida= null;
int P=0;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//BufferedReader entrada = new BufferedReader(new InputStreamReader(System.in));
Connection c = DriverManager.getConnection( dbUrl,user, password);
Statement s = c.createStatement();
String querySql="SELECT * FROM Profesor";
ResultSet r = s.executeQuery(querySql);
while(r.next()) {
try {
entrada=new FileReader("r");
salida=new FileWriter("Profesores.txt");
while((P=entrada.read())!=-1){
salida.write(P);
}
}catch (IOException ex) {
System.out.println(ex);
}finally{
if(entrada!=null){
try{
entrada.close();
}catch(IOException ex){}
}
if(salida!=null){
try{
salida.close();
}catch(IOException ex){}
}
}
}
r.close();
}
}
Valora esta pregunta


0