先谢谢你的回复!
我是要把txt 文件分解。 然后保存到数组里,再插到数据库里,可我遇到难点:
1.怎么循环插入(找到一行子符串 ,分解后,保存到数组里,插入数据库 )
2.我取数组时,发生 越界 ,我不知怎么处理
txt 文件格式:
[1]
单位名称=舟山市普陀区发展和改革局
项目所属地区=舟山
项目名称=舟山岛际流动海水淡化供水
地址=舟山市普陀区沈家门中大街125号
联系人=缪红波
我的代码:
我只要 System.out.println(lineItems[2]);
就报错 java.lang.ArrayIndexOutOfBoundsException:
我的代码:
/*
* Created on 2006-8-25
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.io.*;
import java.util.*;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
public class ph {
public static void main(String[] args) {
try {
//
驱动程序名
String driverName="com.mysql.jdbc.Driver";
//
数据库用户名
String userName="sa";
//
密码
String userPasswd="sa";
//
数据库名
String dbName="jdsh";
//
表名
String tableName="ip";
//
联结字符串
String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;
Class.forName("com.mysql.jdbc.Driver"
.newInstance();
Connection connection=DriverManager.getConnection(url);
Statement stmt = connection.createStatement();
File file = new File("d:\\sin.txt"
;
RandomAccessFile dataFile=new RandomAccessFile(file, "r"
;
String line=dataFile.readLine();
while(line!=null)
{
line=new String(line.getBytes("ISO_8859_1"
,"GBK"
;
String[] lineItems=line.split("="
;
String sqlstr="insert into ip (ip1,ip2,add1,add2) values('"+lineItems[0]+"','"+lineItems[1]+"','"+lineItems[2]+"','"+lineItems[3]+"')";
//stmt.executeUpdate(sqlstr);
// System.out.println(sqlstr);
// System.out.println(lineItems[1]);
System.out.println(lineItems[0]);
// System.out.println(lineItems[1]);
line=dataFile.readLine();
}
dataFile.close();
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}}
|