将txt中数据导入mysql数据库中_将一个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

/**

* @param arg0

* 判断一个整数是奇数还是偶数

* 返回1为奇数,0为偶数

*/

public int test(int arg0){

return a & 1;

}

import java.io.File;

/**

* 删除文件下的所有子目录及文件

* @description

*

* @author JKF23525

* @version 1.0, 2010-4-8

*/

public class DeleteFile

{

public static void delete(String path)

{

File file = new File(path);

if(!file.exists())

{

return;

}

File[] files = file.listFiles();

for (File f : files)

{

if (f.isDirectory())

{

delete(f.getAbsolutePath());

f.delete();

}

else

{

f.delete();

}

}

file.delete();

}

public static void main(String[] args)

{

String path = "e://dadada";

delete(path);

}

}

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/fzhlee/archive/2010/05/11/5579411.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值