Import java. Io .*;
Import java. SQL .*;
Import java. util .*;
Public class texttodatabase {
/**
* @ Param ARGs
* BookProgramInvolved file IO, string separation stringtokenizer, JDBC, Database SQL statement
*/
Public static void main (string [] ARGs ){
Connection con = NULL;
Preparedstatement PSTM = NULL;
Filereader Fr = NULL;
Bufferedreader BR = NULL;
Try {
Class. forname ("com. MySQL. JDBC. Driver ");
Con = drivermanager. getconnection ("JDBC: mysql: // localhost: 3306/exercise1", "root", "root ");
PSTM = con. preparestatement ("insert into student1 (ID, name, age, gendar, score) values (?,?,?,?,?) ");
Fr = new filereader ("D: // exercise // student.txt ");
BR = new bufferedreader (FR );
For (INT I = 0; I <5; I ++ ){
String S = Br. Readline ();
Stringtokenizer ST = new stringtokenizer (s );
Int id = integer. parseint (St. nexttoken ());
String name = ST. nexttoken ();
Int age = integer. parseint (St. nexttoken ());
String gendar = ST. nexttoken ();
Int score = integer. parseint (St. nexttoken ());
PSTM. setint (1, ID );
PSTM. setstring (2, name );
PSTM. setint (3, age );
PSTM. setstring (4, gendar );
PSTM. setint (5, score );
Pcmd.exe cuteupdate ();
}
BR. Close ();
PSTM. Close ();
Con. Close ();
} Catch (classnotfoundexception e ){
E. printstacktrace ();
} Catch (sqlexception e ){
E. printstacktrace ();
} Catch (filenotfoundexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
}
}
}
Note: Create the student.txt file in the following format:
1 Michael 20 male 80
2 Lisi 24 female 86
3 wangwu 25 male 93
4 liuliu 36 female 89
5 zhaoqi 29 MALE 81