<%@ page language="java" import="java.sql.*,java.io.FileReader,java.io.*,java.util.*;
" 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"));
//將要添加的所有記錄的信息編輯進(jìn)該text文件中,各屬性間以空格分開,每行一條記錄
while ((info = in.readLine()) != null) {
StringTokenizer st = new StringTokenizer(info);
while(st.hasMoreElements()){
traininfo[index]=st.nextToken(); //默認(rèn)以空格作為分段符
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();}
}
}
%> 本文出自:億恩科技【1tcdy.com】
服務(wù)器租用/服務(wù)器托管中國五強(qiáng)!虛擬主機(jī)域名注冊頂級提供商!15年品質(zhì)保障!--億恩科技[ENKJ.COM]
|