FileIntoDB

来源:互联网 发布:easyui tree加载数据 编辑:程序博客网 时间:2024/05/16 10:15
package JDBC;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class FileIntoDB {

private static BufferedReader br;

public static void main(String argsp[]) {
Connection con  = null;
Statement stat = null;
PreparedStatement ps=null;
ResultSet rs = null;
String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbURL="jdbc:sqlserver://127.0.0.1:1433; DatabaseName=xqd";
String userName="sa";
String psw="sa";
String sql="insert into tel values(?,?)";

try {
Class.forName(driverName);
con =DriverManager.getConnection(dbURL,userName,psw);
stat = con.createStatement();
ps=con.prepareStatement(sql);


File file = new File("d:\\test.txt");
br= new BufferedReader(new FileReader(file));
int i=0;

while(br.readLine()!=null){
int index=1;
ps.setInt(index++, i);
ps.setString(index++, br.readLine());
i++;
ps.addBatch();
}
ps.executeBatch();
con.commit();

} catch (Exception e) {
try {
con.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}finally{
try {
if(br!=null)br.close();
if(con!=null) con.close();
if(ps!=null)ps.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
原创粉丝点击