Batch add record information to MySQL

Source: Internet
Author: User

<% @ 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 ();}
}
}
%>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.