求jsp把txt资料导入oracel的例子
txt 中的资料张三;男;20;;李四;女;19;大专;王五;男;23;本科;已能读取 txt 的资料,现在的问题是如何把 txt中的资料分组取出!求相关例子!tks!
import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;public class test { private void test(){ FileReader reader; try { reader = new FileReader("D:\\test.txt"); BufferedReader br = new BufferedReader(reader); String s1 = null; while((s1 = br.readLine()) != null) { String[] data=s1.split(";"); String realname=data[0]; //姓名 String sex=data[1]; //性别 String age=data[2]; //年龄 String education=data[3]; //学历 //写入数据库 省略 } br.close(); reader.close(); } catch (IOException e) { e.printStackTrace(); } }}
[解决办法]
支持ls的!!
做好加上去掉空格
trim()
[解决办法]
写了一段,希望多理解一下吧。理解了的东西才是自己的
public class Test { public List<PersonInfo> getPersonInfo(String path){ List<PersonInfo> persons = new ArrayList<PersonInfo>(); FileReader reader; try { reader = new FileReader("D:\\test.txt"); BufferedReader br = new BufferedReader(reader); String s1 = null; while((s1 = br.readLine()) != null) { String[] data=s1.split(";"); if(data.length != 4){ String[] strs = new String[4]; for(int i = 0; i < data.length; i++){ strs[i] = data[1]; } data = strs; } persons.add(new PersonInfo(data[0],data[1],Integer.parseInt(data[2]),data[3])); } br.close(); reader.close(); } catch (IOException e) { e.printStackTrace(); } return persons; } public void insertPersons(List<PersonInfo> persons){ Connection c = null; Statement st = null; try { Class.forName("");//加载驱动 c = DriverManager.getConnection("", "", "");//获取连接 List<String> sqls = getInsertSql(persons); st = c.createStatement(); int count = 1; c.setAutoCommit(false); for(String sql : sqls){ st.addBatch(sql); count++; if(count == 100){ st.executeBatch(); count = 1; } } c.commit(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally{ try { if(c != null){ c.close(); } if(st != null){ st.close(); } } catch (SQLException e) { e.printStackTrace(); } } } public List<String> getInsertSql(List<PersonInfo> persons){ List<String> sqls = new ArrayList<String>(); for(PersonInfo person : persons){ String sql = "insert into tab (name, sex, age, diploma) values (" + person.getName() + "," + person.getSex() + "," + person.getAge() + "," + person.getDiploma() + ")"; sqls.add(sql); } return sqls; }}class PersonInfo{ private String name; private String sex; private int age; private String diploma; public PersonInfo() { } public PersonInfo(String name, String sex, int age, String diploma) { this.name = name; this.sex = sex; this.age = age; this.diploma = diploma; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getDiploma() { return diploma; } public void setDiploma(String diploma) { this.diploma = diploma; }}