Please read the content of this article first: Http://wenku.baidu.com/link?url=hFpkoQMUT-I4YxR6bLQeia45H-Qv4jz3VTrxpog_ Zxoohooslfdnxc7jagj1k04kereerclaaz8n8-ymxqpmsg5mjps6aunsy-fr8ecqhkc
In this article, the author has written very well, but I am in the process of importing data into SQL Server, I did not write such a complicated program. But the function of importing data is realized. Here we go.
I use the development platform is myeclipse, but from the online view of the data, the eclipse operation process seems to be the same.
1. Database preparation: SQL Server database
(1) Create Dataitems database:
Database, new database, database name: Dataitems
(2) CREATE TABLE DataItem:
Create a new query, copy the following code into the new. sql file, and press F5 to execute.
CREATE TABLE DataItem (COM1 varchar () not null,com2 varchar (a) not null,com3 varchar (a) not null,com4 varchar (#) Not n ULL,COM5 varchar (5) Not null,com6 varchar (a) not null,com7 varchar (a) not null,com8 varchar (a) not null,com9 varchar 0) Not NULL,COM10 varchar (a) not NULL,COM11 varchar (a) not null,com12 varchar (a) not null,com13 varchar (a) Not Null,co M14 varchar (50) not null,com15 varchar (a) not null,com16 varchar (a) not null,com17 varchar (a) not null,com18 varchar ) Not null,com19 varchar (a) not NULL,COM20 varchar (a) not null,com21 varchar (a) not null,com22 varchar (a) Not null,com varchar (50) not null,com24 varchar (a) not null,com25 varchar (NULL,COM26) not null,com27 varchar Not null,com28 varchar (a) not null,com29 varchar (a) not null,com30 varchar (a) not null,com31 varchar (a) Not null,com3 2 varchar (+) not null,com33 varchar (a) not null,com34 varchar (a) not null,com35 varchar (a) not null,com36 varchar (50) Not NULL,COM37 varchar (50) Not null,com38 varchar (a) not null,com39 varchar (a) not null,com40 varchar (a) not null,com41 varchar (a) Not Null,co M42 varchar () NOT NULL)
At this point, we created the Dataitems database and created the DataItem table.
2. Open Myeclipse->file->new->java Project, fill out the item name in project names, I wrote trust-> OK
3. Right click on the src->new->package under the Trust project, fill in the name of the package, I wrote db.xuss-> OK
4, right click Db.xuss Package->new->class, in name fill in the package name, I write is lala-> OK
5, open the Lala.java, the following code copy the past (note if the user name, password, port and you do not match, to change to your own):
Package Db.xuss;import Java.io.bufferedreader;import Java.io.bufferedwriter;import java.io.file;import Java.io.filereader;import java.io.filewriter;import java.sql.*;p ublic class Lala {public static void main (string[] args ) throws exception{try {file F=new file ("E:/kddcup.data"); System.out.println ("Kddcup.data exits\t" +f.exists ()); BufferedReader br=new BufferedReader (new FileReader (f)); File F1=new file ("E:/data1.txt"); BufferedWriter bw=new BufferedWriter (new FileWriter (F1)); String s = br.readline (); String drivername = "Com.microsoft.sqlserver.jdbc.SQLServerDriver"; Load JDBC driver String dburl = "jdbc:sqlserver://localhost:1433; Databasename=dataitems "; Connect server and database Test String userName = "sa"; Default User name String userpwd = "123456"; Password Connection conn=null; try {class.forname (drivername); conn = Drivermanager.getconnection (Dburl, UserName, userpwd); System.out.println ("Connection successful!"); If the connection succeeds console output connectionsuccessful! } catch (Exception e) {e.printstacktrace (); } string[] STRs; String Com1=null; String Com2=null; String Com3=null; String Com4=null; String Com5=null; String Com6=null; String Com7=null; String Com8=null; String Com9=null; String Com10=null; String Com11=null; String Com12=null; String Com13=null; String Com14=null; String Com15=null; String Com16=null; String Com17=null; String Com18=null; String Com19=null; String Com20=null; String Com21=null; String Com22=null; String Com23=null; String Com24=null; String Com25=null; String Com26=null; String Com27=null; String Com28=null; String Com29=null; String Com30=null; String Com31=null; String Com32=null; String Com33=null; String Com34=null; String Com35=null; String Com36=null; String Com37=null; String Com38=null; String Com39=null; String Com40=null; String Com41=null; String com42=nUll PreparedStatement PST = NULL; String sql = "INSERT into DataItem values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?, ?,?,?,?,?)"; while (S!=null) {strs=s.split (","); Com1=strs[0]; COM2=STRS[1]; COM3=STRS[2]; COM4=STRS[3]; COM5=STRS[4]; COM6=STRS[5]; COM7=STRS[6]; COM8=STRS[7]; COM9=STRS[8]; COM10=STRS[9]; COM11=STRS[10]; COM12=STRS[11]; COM13=STRS[12]; COM14=STRS[13]; COM15=STRS[14]; COM16=STRS[15]; COM17=STRS[16]; COM18=STRS[17]; COM19=STRS[18]; COM20=STRS[19]; COM21=STRS[20]; COM22=STRS[21]; COM23=STRS[22]; COM24=STRS[23]; COM25=STRS[24]; COM26=STRS[25]; COM27=STRS[26]; COM28=STRS[27]; COM29=STRS[28]; COM30=STRS[29]; COM31=STRS[30]; COM32=STRS[31]; COM33=STRS[32]; COM34=STRS[33]; COM35=STRS[34]; COM36=STRS[35]; COM37=STRS[36]; COM38=STRS[37]; COM39=STRS[38]; COM40=STRS[39]; COM41=STRS[40]; Com42=strs[41].rEplace (".", ""); PST = conn.preparestatement (SQL); Pst.setstring (1,COM1); Pst.setstring (2,COM2); Pst.setstring (3,COM3); Pst.setstring (4,COM4); Pst.setstring (5,COM5); Pst.setstring (6,COM6); Pst.setstring (7,COM7); Pst.setstring (8,COM8); Pst.setstring (9,COM9); Pst.setstring (10,COM10); Pst.setstring (11,COM11); Pst.setstring (12,COM12); Pst.setstring (13,COM13); Pst.setstring (14,COM14); Pst.setstring (15,COM15); Pst.setstring (16,COM16); Pst.setstring (17,COM17); Pst.setstring (18,COM18); Pst.setstring (19,COM19); Pst.setstring (20,COM20); Pst.setstring (21,COM21); Pst.setstring (22,COM22); Pst.setstring (23,COM23); Pst.setstring (24,COM24); Pst.setstring (25,COM25); Pst.setstring (26,COM26); Pst.setstring (27,COM27); Pst.setstring (28,COM28); Pst.setstring (29,COM29); Pst.setstring (30,COM30); Pst.setstring (31,COM31); Pst.setstring (32,com32); Pst.setstring (33,COM33); Pst.setstring (34,COM34); Pst.setstring (35,COM35); Pst.setstring (36,COM36); Pst.setstring (37,COM37); Pst.setstring (38,COM38); Pst.setstring (39,COM39); Pst.setstring (40,COM40); Pst.setstring (41,COM41); Pst.setstring (42,COM42); int row = Pst.executeupdate (); Pst.close (); s = Br.readline (); } conn.close (); }catch (Exception ex) {System.out.println ("Error:" + ex.tostring ()); } }}
The code is very rough, please don't mind.
5. It is still not working, because the fewer jar packages. The following is the introduction of the jar package.
If you do not have a link sqlserver2005 jar package, you need to download, you can download from my csdn resources free: http://download.csdn.net/detail/thisnameokbu/8397285
(1) After downloading, unzip first. After unpacking, generate the Sqljdbc_1.1.1501.101_chs folder and copy the sqljdbc_1.1 folder under the folder to C:\temp.
(2) Then we need to set "environment variables". (My Computer operating system WinXP) Setting method: My Computer----right-click "Properties", advanced environment variable----system variable found CLASSPATH variable, double-click to edit, in the last add ; C:\temp\sqljdbc_1.1\chs\sqljdbc.jar(remember that each path in the classpath must be used ; ), restart your computer (but before restarting your computer, you need to record the URL of this blog ...). -_-|| )
(3) Back to MyEclipse, we need to add the jar package. Project->properties->java buid path->libraries->add External jar-> Find C:\temp\sqljdbc_1.1\chs\ Sqljdbc.jar Select->ok
6. Open the Lala.java file, click the flag indicated below, run
The console console appears as shown in the information. Data is imported into the database for a long time and needs to be waited patiently. Wait until the arrows turn gray to show that the program has been executed.
After the data is finished, there are 4898431 rows of data in the DataItem table. Can be obtained from the SQL statement: SELECT COUNT (*) as Num from DataItem. As shown in the following:
(If there's anything you don't understand, you can email me: [email protected])
KDD Data Import sqlserver2005 Database