" contentType="text/html;charset=gb2312"%>
String info="";
String traininfo[]=new String[20]; //存储每条记录的信息
int index=0;
Connection con=null;
PreparedStatement psm=null;
Statement sm=null;
try
{
Class.forName("com.MySQL.jdbc.Driver").newInstance();
String url="jdbc:mysql://localhost/trainsystem";
con=DriverManager.getConnection(url,"root","");
BufferedReader in = new BufferedReader(new FileReader("C:\\Users\\wangxing\\Workspaces\\MyEclipse 8.5 M1\\test1\\WebRoot\\traininfo.txt"));
//将要添加的所有记录的信息编辑进该text文件中,各属性间以空格分开,每行一条记录
while ((info = in.readLine()) != null) {
StringTokenizer st = new StringTokenizer(info);
while(st.hasMoreElements()){
traininfo[index]=st.nextToken(); //默认以空格作为分段符
index++;
}
sm=con.createStatement();
psm=con.prepareStatement("insert into traininfo(trainno,start,time1,arrivel,time2,time,waylength,p1,p2,p3,p4,totalsell,stopno,fullroute) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?);");
psm.setString(1,traininfo[0]);
psm.setString(2,traininfo[1]);
psm.setString(3,traininfo[2]);
psm.setString(4,traininfo[3]);
psm.setString(5,traininfo[4]);
psm.setString(6,traininfo[5]);
psm.setString(7,traininfo[6]);
psm.setString(8,traininfo[7]);
psm.setString(9,traininfo[8]);
psm.setString(10,traininfo[9]);
psm.setString(11,traininfo[10]);
psm.setString(12,traininfo[11]);
psm.setString(13,traininfo[12]);
psm.setString(14,traininfo[13]);
psm.executeUpdate();
for(int i=0;i<=19;i++)
traininfo[i]=null;
index=0;
}
in.close();
}
catch(Exception e){
System.out.print(e.getMessage());
}
finally
{
if(psm!=null)
{
try {psm.close();}catch(Exception e){ e.printStackTrace();}
}
if(sm!=null)
{
try {sm.close();}catch(Exception e){ e.printStackTrace();}
}
if(con!=null)
{
try {con.close();}catch(Exception e){ e.printStackTrace();}
}
}
%>