62,623
社区成员
发帖
与我相关
我的任务
分享
import java.sql.*;
public class ConnectionFactory {
private static ConnectionFactory ref = new ConnectionFactory();
private static final String DRIVER = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
private static final String URL_LOCAL = "jdbc:sqlserver://localhost:1433;DatabaseName=test";
private static final String USER_NAME = "test";
private static final String USER_PWD = "password";
private ConnectionFactory(){
try{
Class.forName(DRIVER);
}catch(ClassNotFoundException cnfe){
cnfe.printStackTrace();
}
}
public static Connection getConnection(int mode){
try{
switch(mode){
case 0:
return DriverManager.getConnection(URL_LOCAL,USER_NAME,USER_PWD);
case 1:
// todo
default:
return null;
}
}catch(SQLException sqle){
sqle.printStackTrace();
return null;
}
}
public static void close(ResultSet rst){
if(rst != null){
try{
rst.close();
}catch(SQLException sqle){
sqle.printStackTrace();
}
}
}
public static void close(Statement stmt){
if(stmt != null){
try{
stmt.close();
}catch(SQLException sqle){
sqle.printStackTrace();
}
}
}
public static void close(Connection conn){
if(conn != null){
try{
conn.close();
}catch(SQLException sqle){
sqle.printStackTrace();
}
}
}
}
import java.io.*;
import java.sql.*;
public class TransferItems {
// create table info(
// Name varchar(800),
// Status varchar(800),
// URL varchar(800),
// Phase varchar(800),
// Category varchar(800),
// Reference varchar(800),
// dis varchar(3000)
// )
private String name;
private String status;
private String url;
private String phase;
private String category;
private String reference;
private String dis;
/**
* 填充一下
*
* */
private void fill(String name,String status,String url,String phase,
String category,String reference,String dis){
this.name = name;
this.status = status;
this.url = url;
this.phase = phase;
this.category = category;
this.reference = reference;
this.dis = dis;
}
/**
* 清理一下
*
* */
private void clean(){
this.name = "";
this.status = "";
this.url = "";
this.phase = "";
this.category = "";
this.reference = "";
this.dis = "";
}
/**
* 从
*
* */
public void doSomething(String filePath){
File f = new File(filePath);
if (!f.isFile()){
System.out.println("文件不存在!");
return;
}
try{
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String tmpLine = ""; // 临时存放行内容
boolean isBeginFlag = true; // 标志 开始 ===================
while( ((tmpLine = br.readLine())) != null ){
if(!tmpLine.startsWith("=======================") && !"".equals(tmpLine) ){
String strPrefix = tmpLine.substring(0,tmpLine.indexOf(":")>=0?tmpLine.indexOf(":")+1:1);
// System.out.println(strPrefix);
if (strPrefix.compareToIgnoreCase("Name:") == 0) {
tmpLine = tmpLine.substring(tmpLine.indexOf(strPrefix) + strPrefix.length());
name += tmpLine;
}
else if(strPrefix.compareToIgnoreCase("Status:") == 0) {
tmpLine = tmpLine.substring(tmpLine.indexOf(strPrefix) + strPrefix.length());
status += tmpLine;
}
else if(strPrefix.compareToIgnoreCase("URL:") == 0) {
tmpLine = tmpLine.substring(tmpLine.indexOf(strPrefix) + strPrefix.length());
url += tmpLine;
}
else if(strPrefix.compareToIgnoreCase("Phase:") == 0) {
tmpLine = tmpLine.substring(tmpLine.indexOf(strPrefix) + strPrefix.length());
phase += tmpLine;
}
else if(strPrefix.compareToIgnoreCase("Category:") == 0) {
tmpLine = tmpLine.substring(tmpLine.indexOf(strPrefix) + strPrefix.length());
category += tmpLine;
}
else if(strPrefix.compareToIgnoreCase("Reference:") == 0) {
tmpLine = tmpLine.substring(tmpLine.indexOf(strPrefix) + strPrefix.length());
reference += tmpLine;
}
else{
dis += tmpLine;
}
}
else if(tmpLine.startsWith("=======================")){
// tmpLine = "分隔符";
if(isBeginFlag){ // 如果是第一次的 ===================== 就跳过,以后遇到代表提交
isBeginFlag = false;
clean(); // 清理
continue;
}
fill(name,status,url,phase,category,reference,dis); // 准备
doInsert(); // 插入
clean(); // 清理
}
// 显示文件内容
System.out.println(tmpLine);
}
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException ioe){
ioe.printStackTrace();
}
}
/**
* 插入
*
* */
private void doInsert(){
Connection conn = null;
PreparedStatement pstmt = null;
String strSQL = "INSERT INTO info VALUES(?,?,?,?,?,?,?)";
try {
conn = ConnectionFactory.getConnection(0);
pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1, this.name);
pstmt.setString(2, this.status);
pstmt.setString(3, this.url);
pstmt.setString(4, this.phase);
pstmt.setString(5, this.category);
pstmt.setString(6, this.reference);
pstmt.setString(7, this.dis);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally{
ConnectionFactory.close(pstmt);
ConnectionFactory.close(conn);
}
}
public static void main(String[] args){
new TransferItems().doSomething("C:/a.txt");
}
}