大家好,我有两个问题请教一下各们高手,先谢谢了。
现在有几万个文本文件,怎么导入到MySql数据库里,数据表包括两个字段,一个路径字段,还有一个内容字段用来存文本文件里的内容。
一个文件,只一条记录的内容字段。还有一个问题,用java将数据库里几万条记录导出时,一条记录一个文件,并分别保存到多个文件夹里?能解决第一个问题,就给分,谢谢啊。
现在有几万个文本文件,怎么导入到MySql数据库里,数据表包括两个字段,一个路径字段,还有一个内容字段用来存文本文件里的内容。
一个文件,只一条记录的内容字段。还有一个问题,用java将数据库里几万条记录导出时,一条记录一个文件,并分别保存到多个文件夹里?能解决第一个问题,就给分,谢谢啊。
我帮楼主找了一个,看看合适不将一个student.txt文件的数据导入MySQL数据库中一张student1表中import java.io.*;
import java.sql.*;
import java.util.*; public class TextToDataBase { /**
* @param args
* 本程序涉及文件IO,字符串分隔StringTokenizer,JDBC,数据库sql语句
*/
public static void main(String[] args) {
Connection con=null;
PreparedStatement pstm=null; FileReader fr=null;
BufferedReader br=null;
try { Class.forName("com.mysql.jdbc.Driver"); con=DriverManager.getConnection("jdbc:mysql://localhost:3306/exercise1","root","root"); pstm=con.prepareStatement("insert into student1 (ID,name,age,gendar,score) values(?,?,?,?,?)");
fr=new FileReader("D:\\Exercise\\student.txt");
br=new BufferedReader(fr);
for(int i=0;i <5;i++){ String s=br.readLine();
StringTokenizer st=new StringTokenizer(s);
int ID=Integer.parseInt(st.nextToken());
String name=st.nextToken();
int age=Integer.parseInt(st.nextToken());
String gendar=st.nextToken();
int score=Integer.parseInt(st.nextToken());
pstm.setInt(1,ID);
pstm.setString(2,name);
pstm.setInt(3,age);
pstm.setString(4,gendar);
pstm.setInt(5,score);
pstm.executeUpdate();
}
br.close();
pstm.close();
con.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) { e.printStackTrace();
} catch (FileNotFoundException e) { e.printStackTrace();
} catch (IOException e) { e.printStackTrace();
}
}
} /**
注意:将student.txt文件内容建成如下形式:
1 张三 20 male 80
2 lisi 24 female 86
3 wangwu 25 male 93
4 liuliu 36 female 89
5 zhaoqi 29 male 81
**/
import java.sql.*;public class TextInsert { public static void main(String[] args) {
Connection con = null;
PreparedStatement pstm = null; try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost/test?"
+ "user=monty" + "characterEncoding=GBK"); pstm = con.prepareStatement("insert into text values(?,?)"); String path = "/home/yl/Internet/tomcat/webapps/unicode";
File file = new File(path);
String test[];
test = file.list();
for (int i = 0; i < test.length; i++) {
String pathName;
pathName = path + "/" + test[i];
StringBuffer content = new StringBuffer();
File f = new File(pathName);
FileInputStream ism = new FileInputStream(f);
InputStreamReader isr = new InputStreamReader(ism);
BufferedReader bs = new BufferedReader(isr);
String receivePackage = bs.readLine();
while (receivePackage != null) {
content.append(receivePackage).append('\n');
receivePackage = bs.readLine();
}
pstm.setString(1, pathName);
pstm.setString(2, content.toString());
pstm.executeUpdate();
ism.close();
}
pstm.close();
con.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) { e.printStackTrace();
} catch (FileNotFoundException e) { e.printStackTrace();
} catch (IOException e) { e.printStackTrace();
}
}
}
import java.sql.*;public class TextInsert { public static void main(String[] args) {
Connection con = null;
PreparedStatement pstm = null; try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost/test?"
+ "user=monty" + "characterEncoding=GBK"); pstm = con.prepareStatement("insert into text values(?,?)"); String path = "/home/yl/Internet/tomcat/webapps/unicode";
File file = new File(path);
String test[];
test = file.list();
for (int i = 0; i < test.length; i++) {
String pathName;
pathName = path + "/" + test[i];
StringBuffer content = new StringBuffer();
File f = new File(pathName);
FileInputStream ism = new FileInputStream(f);
InputStreamReader isr = new InputStreamReader(ism);
BufferedReader bs = new BufferedReader(isr);
String receivePackage = bs.readLine();
while (receivePackage != null) {
content.append(receivePackage).append('\n');
receivePackage = bs.readLine();
}
pstm.setString(1, pathName);
pstm.setString(2, content.toString());
pstm.executeUpdate();
ism.close();
}
pstm.close();
con.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) { e.printStackTrace();
} catch (FileNotFoundException e) { e.printStackTrace();
} catch (IOException e) { e.printStackTrace();
}
}
}