<% @ 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]; // stores the information of each record
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 "));
// Edit the information of all records to be added to the text file. The attributes are separated by spaces and each record is recorded in one row.
While (info = in. readLine ())! = Null ){
StringTokenizer st = new StringTokenizer (info );
While (st. hasMoreElements ()){
Traininfo [index] = st. nextToken (); // space is used as the delimiter by default.
Index ++;
}
Sm = con. createStatement ();
Psm = con. prepareStatement ("insert into traininfo (trainno, start, time1, arrivel, time2, time, waylength, p1, p2, p3, p4, totalloud, 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]);
SMS. setString (10, traininfo [9]);
SMS. setString (11, traininfo [10]);
SMS. setString (12, traininfo [11]);
SMS. setString (13, traininfo [12]);
SMS. setString (14, traininfo [13]);
Psm.exe cuteUpdate ();
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 {SMS. 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 ();}
}
}
%>