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());
}
}
}
-------------------------------------------
package timer;
//主要用来转换成汉字 防止出现乱码
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class chinese {
public List chinesezhuanhuan(String file)
{
List list = new ArrayList();
try
{
InputStream r = new FileInputStream (file);
ByteArrayOutputStream byteout = new ByteArrayOutputStream();
byte tmp [] = new byte [1024];
byte context [];
int i = 0 ;
while ((i = r.read(tmp)) !=- 1 )
{
byteout.write(tmp);
}
context = byteout.toByteArray();
String str = new String(context,"unicode" );
// 分隔行
String stra [] = str.split( "\n" );
for ( int n = 0 ;n < stra.length;n ++ )
{
list.add(stra[n]);
}
System.out.println(list.size());
} catch (Exception e)
{
e.printStackTrace();
list = null;
}
return list;
}
}
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());
}
}
}
-------------------------------------------
package timer;
//主要用来转换成汉字 防止出现乱码
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class chinese {
public List chinesezhuanhuan(String file)
{
List list = new ArrayList();
try
{
InputStream r = new FileInputStream (file);
ByteArrayOutputStream byteout = new ByteArrayOutputStream();
byte tmp [] = new byte [1024];
byte context [];
int i = 0 ;
while ((i = r.read(tmp)) !=- 1 )
{
byteout.write(tmp);
}
context = byteout.toByteArray();
String str = new String(context,"unicode" );
// 分隔行
String stra [] = str.split( "\n" );
for ( int n = 0 ;n < stra.length;n ++ )
{
list.add(stra[n]);
}
System.out.println(list.size());
} catch (Exception e)
{
e.printStackTrace();
list = null;
}
return list;
}
}