- Visual C++源码
- Visual Basic源码
- C++ Builder源码
- Java源码
- Delphi源码
- C/C++源码
- PHP源码
- Perl源码
- Python源码
- Asm源码
- Pascal源码
- Borland C++源码
- Others源码
- SQL源码
- VBScript源码
- JavaScript源码
- ASP/ASPX源码
- C#源码
- Flash/ActionScript源码
- matlab源码
- PowerBuilder源码
- LabView源码
- Flex源码
- MathCAD源码
- VBA源码
- IDL源码
- Lisp/Scheme源码
- VHDL源码
- Objective-C源码
- Fortran源码
- tcl/tk源码
- QT源码
IPInfoToDB.java
资源名称:Java.rar [点击查看]
上传用户:liming9091
上传日期:2014-10-27
资源大小:3376k
文件大小:2k
源码类别:
Java编程
开发平台:
Java
- package tsinghuaip;
- import java.sql.*;
- import java.util.*;
- import java.io.*;
- public class IPInfoToDB {
- private String strTxtFileName; //IP地址文本文件名
- private Connection conn = null;
- private Statement stmt = null;
- public IPInfoToDB() {
- strTxtFileName = new String();
- }
- //设置文本文件名
- public void SetTxtFileName(String strFileName) {
- strTxtFileName = strFileName;
- }
- public void SaveIPToDB() throws Exception {
- String strSeparator = "|"; //the separator of the text file field
- String strTmp = "";
- //进行数据库得连接
- Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
- String url = "jdbc:microsoft:sqlserver://localhost:1433;" +
- "DatabaseName=CampusIP";
- conn = DriverManager.getConnection(url, "sa", "");
- stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
- ResultSet.CONCUR_UPDATABLE);
- //从文本文件中读取数据
- BufferedReader inTxt = new BufferedReader(new FileReader(
- strTxtFileName));
- while ( (strTmp = inTxt.readLine()) != null) {
- StringTokenizer strToken = new StringTokenizer(strTmp, "|");
- String arrTmp[];
- arrTmp = new String[3];
- for (int i = 0; i < 3; i++)
- arrTmp[i] = new String("");
- int index = 0;
- while (strToken.hasMoreElements()) {
- strTmp = (String) strToken.nextElement();
- strTmp = strTmp.trim();
- arrTmp[index++] = strTmp;
- }
- //下面就是将这些数据写进数据库
- String SQL =
- "insert IPInfo(STARTIP,ENDIP,LOCAL) "
- + " values('" + arrTmp[0] + "', '"
- + arrTmp[1] + "','"
- + arrTmp[2] + "')";
- stmt.execute(SQL);
- }
- stmt.close();
- conn.close();
- }
- }