This is what I met when I went to the soft link power interview today. at that time, although I knew it was implemented through a tool provided by the database, I did not know how to do it because I had never used it before. the Java program method is used later.
Contains many rows of data, each row of data is a student information, each field is separated by "|". This data file must be loaded into the student information table.
1. Let's talk about how to use Java.
A. Generate a file stream
Fileinputstream studentinputstream = new fileinputstream ("studentdata.txt ");
B. encapsulate it in a bufferreader.
Bufferreader studentbfreader = new bufferreader (New inputstreanreader (studentinputstream ))
C. Set a loop and read a row in the file through bufferreader. Write data to the database in the loop.
Obtain the database connection and generate a preparedstatement.
Preparedstatement pstmt = con. preparestatement ("insert into studentinfo (Num, name, classnum, tel, address) values (?,?,?,?,?) ");
String student _ = NULL
While (student _ = studentbfreader. Readline ()! = NULL ){
String [] studentinfoarray = student _. Split ("| ");
Pstmt. setstring (1, studentinfoarray [0]);
...
Pstmt. excute ();
}
Obviously, writing Java programs is troublesome. Then let's look at the second method-using database tools.
2. SQL loader in Oracle
Oracle SQL loader is used to load the content in data files on the operating system to the Oracle database.
To use SQL loader, you must first create a control file that tells SQL loader the data loading policy after SQL loader is started.
Create a new studentdataload. CTL file and write
Load data
Infile 'studentdata.txt'
Into Table studentinfo
Fields terminated by '|'
(Num, name, classnum, tel, address)
Then, start SQL loader on the console, write the username and password (for loading data), and control the file name.
Sqlldr userid = username/password @ servicename control = studentdata. CTL
The SQL loader method is much simpler.