1,读取txt文件寸入sqlserver2000数据库
package timer;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;
import db.DbConn;
public class Test {
public static DbConn DB = new DbConn();
public static void main(String[] args) throws Exception {
int num = 5;
int i = 0;
int b = 0;
int j = 0;
chinese ch = new chinese();
try {
String aa = "d:/c.txt";
String date = "";
int a;
Connection con = DB.getConnection();
File file = new File(aa);
InputStreamReader read = new InputStreamReader(new FileInputStream(file),"unicode");//这句是关键
BufferedReader br = new BufferedReader(read);
String sql = "insert into c(a) values(?)";
PreparedStatement pre = null;
pre = con.prepareStatement(sql);
List list = new ArrayList();
while(( date =br.readLine())!= null){
System.out.println(date);
list.add(date);
}
int total = list.size();
while (i < total) {
for (; i < list.size(); i++) {
String d = list.get(i).toString();
pre.setString(1, d);
pre.addBatch();
j++;
if (j % num == 0) {
pre.executeBatch();
pre.clearParameters();
pre.clearBatch();
b = total - j;
}
}
if (b < num) {
System.out.println(b);
pre.executeBatch();
}
}
pre.close();
System.out.println("over-----");
con.close();
br.close();
read.close();
} catch (FileNotFoundException e) {
System.out.println("找不到文件.....");
} catch (IOException ioe) {
System.out.println("IO异常.....");
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("不能插入......");
System.out.println(ex.getMessage());
System.out.println(ex.getStackTrace());
System.out.println(ex.getLocalizedMessage());
}
}
}
2.导出txt或者excle时候为文件起中文名称
String name = "地市信息发送数量统计";
String zi = "至";
String name1 = new String(name.getBytes("GBK"), "ISO8859_1");
String zi1 = new String(zi.getBytes("GBK"), "ISO8859_1");
将你要给文件起的名字进行这样的转换就可以了
package timer;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;
import db.DbConn;
public class Test {
public static DbConn DB = new DbConn();
public static void main(String[] args) throws Exception {
int num = 5;
int i = 0;
int b = 0;
int j = 0;
chinese ch = new chinese();
try {
String aa = "d:/c.txt";
String date = "";
int a;
Connection con = DB.getConnection();
File file = new File(aa);
InputStreamReader read = new InputStreamReader(new FileInputStream(file),"unicode");//这句是关键
BufferedReader br = new BufferedReader(read);
String sql = "insert into c(a) values(?)";
PreparedStatement pre = null;
pre = con.prepareStatement(sql);
List list = new ArrayList();
while(( date =br.readLine())!= null){
System.out.println(date);
list.add(date);
}
int total = list.size();
while (i < total) {
for (; i < list.size(); i++) {
String d = list.get(i).toString();
pre.setString(1, d);
pre.addBatch();
j++;
if (j % num == 0) {
pre.executeBatch();
pre.clearParameters();
pre.clearBatch();
b = total - j;
}
}
if (b < num) {
System.out.println(b);
pre.executeBatch();
}
}
pre.close();
System.out.println("over-----");
con.close();
br.close();
read.close();
} catch (FileNotFoundException e) {
System.out.println("找不到文件.....");
} catch (IOException ioe) {
System.out.println("IO异常.....");
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("不能插入......");
System.out.println(ex.getMessage());
System.out.println(ex.getStackTrace());
System.out.println(ex.getLocalizedMessage());
}
}
}
2.导出txt或者excle时候为文件起中文名称
String name = "地市信息发送数量统计";
String zi = "至";
String name1 = new String(name.getBytes("GBK"), "ISO8859_1");
String zi1 = new String(zi.getBytes("GBK"), "ISO8859_1");
将你要给文件起的名字进行这样的转换就可以了