java解析文本文件数据并保存入库的问题!

edwin_su 2005-11-30 09:58:43
问题 1
有一个文本文件a.txt里存储数据为:(以下的数据不是真实数据)
0001 0002 0003 0004
0005 0006 0007 0008
0009 0000 0011 0097
数据库有表A,字段名为:
ID NAME AGE PASSWORD
现在要用java程序实现将文本文件a.txt中的每行数据插入到数据库表A中
ID NAME AGE PASSWORD
--------------------------------
0001 0002 0003 0004
0005 0006 0007 0008
0009 0000 0011 0097

<其中a.txt当中的数据以Tab键分开,回车键换行>

请问哪位做过相关的开发工作能提供一些代码和思路!谢谢!
===============================================================================
问题 2
这个程序需要定时触发,要用windows的计划任务。可我不清楚计划任务如何去触发这个程序执行?
...全文
943 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
xpzheng 2005-12-01
  • 打赏
  • 举报
回复
学习,
zhangji__2000 2005-12-01
  • 打赏
  • 举报
回复
public static void main(String[] args) {
Connection conn =null;
PreparedStatement pstmt=null;
String sql="";
try {
File f = new File("C://aa.txt");
FileReader fr = new FileReader(f);
BufferedReader bf = new BufferedReader(fr);
sql = "insert tab values(?,?,?,?)";
conn ="取一个连接";
pstmt = conn.prepareStatement(sql);
String line =bf.readLine();
while(line!=null){
int i=1;
System.out.println("bf.readLine()"+line);
String[] sss = line.split("\\t");
pstmt.setString(i++,sss[0]);
pstmt.setString(i++,sss[1]);
pstmt.setString(i++,sss[2]);
pstmt.setString(i++,sss[3]);
pstmt.addBatch();
line = bf.readLine();
}
pstmt.executeBatch();
}
catch (Exception e) {
}finally{
//释放资源数据库
}
}
h2342166 2005-12-01
  • 打赏
  • 举报
回复
\t 是什么意思?
HouJinkun2005 2005-12-01
  • 打赏
  • 举报
回复
楼主的问题和我刚刚做的一件事一样,不过我解吸的是指定目录下的所有xml文件.

你先做一个class来接数据,几列就做几个属性。
读文本文件,读到'\n'就换行,读到'\t'就把值写到属性里。
必要的时候要用Integer.parseInt

至于第二个问题,
//驱动类,TestAutoDispose.java
public class TestAutoDispose
{
//AutoDisposeBean就是那个解析文本文件的写如数据库的类
private static AutoDisposeBean a=new AutoDisposeBean();
public static void main(String[] args)
{
try
{
//访问解析类的方法
a.access(args[0]);
}
catch (Exception e)
{
System.out.println(e);
}
}
}
然后再写个AutoDispose.bat
@echo off
call ???.bat (这个是我用来设置classpath和path的,你根据自己的需要,可以不用的。
java TestAutoDispose c:\
在计划任务里添加运行这个批处理文件的任务。
eureka0891 2005-12-01
  • 打赏
  • 举报
回复
String[] str = line.split("\t");//?
没什么问题,
楼主的这种是是tab来分割的csv文件
yaray 2005-11-30
  • 打赏
  • 举报
回复
有何问题?
believefym 2005-11-30
  • 打赏
  • 举报
回复
String[] str = line.split("\t");//?
yaray 2005-11-30
  • 打赏
  • 举报
回复
问题 1
public static void main(String[] args) {
if(args==null || args.length==0){
System.out.println("invalid, please appoint the data file name and path.");
return;
}

String inputFile = args[0];
String sql = "insert tableName values(?,?,?,?)";

Connection conn = ....;
PreparedStatement pstmt = conn.prepareStatement(sql);

BufferedReader br = new BufferedReader(new FileReader(inputFile)); //指定文件路径,可由main方法参数获得
String line = null;
while((line=br.readLine())!=null){
String[] str = line.split("\t");// need JDK 1.4.x or latest

//合法性验证通过后进行数据库操作
pstmt.setString(1, str[0]);
pstmt.setString(2, str[1]);
pstmt.setString(3, str[2]);
pstmt.setString(4, str[3]);

//int num = pstmt.executeUpdate();// 方案A 即读即插 入库
pstmt.addBatch(); // 方案B (1)
}
int[] insertRowNumber = pstmt.executeBatch(); // 方案B (2) 批量入库
System.out.println("count row: " + insertRowNumber);
}


问题 2
以上程序写在一个独立的具有static main方法的Java类中,在计划任务中执行命令: java xxxxx d:/data/datas.txt
kevin911 2005-11-30
  • 打赏
  • 举报
回复
用readline,再加上StringTokenizer应该也能解决
believefym 2005-11-30
  • 打赏
  • 举报
回复
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
String line = "";
while((line=br.readLine())!=null){
String[] str = line.split("\\t");
//数据库操作
//...
}

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧