赞
踩
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.ResultSetMetaData;
- import java.sql.SQLException;
- import java.sql.Statement;
-
- import java.util.StringTokenizer;
- import java.util.Vector;
-
- import javax.swing.JOptionPane;
-
-
- public class importAndExportManager {
-
- public static void main(String[] args) {
- //new connectDB().setDbName("mysql");
- //new importAndOutportManager().importDate("borrow.csv");;
- //System.out.println("end");
- }
- private Connection conn=connectDB.getConnection();
- private Statement stmt;
- private PreparedStatement pstmt;
-
- private ResultSetMetaData rsmd=null;
- private ResultSet rs=null;
-
- /*
- * 获得数据库文件的内容 存放在ResultSet 的对象中
- */
- public void getDate(String DBname){
-
- String sql="select * from library."+DBname;
- try {
- stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
- rs=stmt.executeQuery(sql);
- rsmd=rs.getMetaData();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- JOptionPane.showMessageDialog(null, e.getMessage());
- }
- }
-
- public void exportDate(String DBname) throws SQLException{
- int row=0;
- int column=0;
- Object [][]booklist=null; //数据内容
- String tableName[]=null; //标签内容
-
- getDate(DBname);
- column=rsmd.getColumnCount();
- while (rs.next()) {
- row++;
- }
- booklist=new Object[row][column];
- tableName=new String[column];
-
- for (int i = 1; i < tableName.length+1; i++) {
- tableName[i-1]=rsmd.getColumnLabel(i);
- }
-
- for (int i=0;i<row;i++){
- rs.previous();
- for (int j = 0; j < column; j++) {
- booklist[i][j]=rs.getObject(j+1);
- }
- }
-
- //写入csv文件中
- File csv = new File("E:/Javaproject/library/"+DBname+".csv");
- try {
- BufferedWriter bw = new BufferedWriter(new FileWriter(csv, false));
- for (int i = 0; i < tableName.length; i++) {
- bw.write(tableName[i]);
- if (i!=tableName.length-1) {
- bw.write(",");
- }
- }
- bw.write("\r\n");
-
- for (int i=0;i<row;i++){
- for (int j = 0; j < column; j++) {
- bw.write(booklist[i][j].toString());
- if (j!=column-1) {
- bw.write(",");
- }
- }
- bw.write("\r\n");
- }
-
- bw.close();
-
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
- /*
- * 从CSV文件中读取内容,存入一个Vector<Object []>对象中。
- */
- public void importDate(String inputFilePath){
- Vector<Object []> date = null;
- Object oneLineDate[];
- Vector<String>label=new Vector<String>();
- int column=0;
- File file=new File(inputFilePath);
- try {
- BufferedReader br=new BufferedReader(new FileReader(file));
- String line="";
- line=br.readLine();
- StringTokenizer st = new StringTokenizer(line, ",");
- while (st.hasMoreElements()) {
- label.add(st.nextToken());//得到标签
- column++; //利用标题行计算出总共有多少列行
- }
- oneLineDate=new Object[column];
- date=new Vector<Object[]>();
- for (int i=0;(line=br.readLine())!=null;i++) {
- StringTokenizer st1 = new StringTokenizer(line, ",");
- for(int j=0;st1.hasMoreElements();j++){
- oneLineDate[j]=st1.nextElement();
- }
- date.add(i,oneLineDate);
- oneLineDate=new Object[column];
- }
- br.close();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- insertDate(label,date, file.getName());
- }
-
- /*
- *
- */
- public void insertDate(Vector<String>label,Vector<Object []> date ,String tableName){
- String table=tableName.substring(0, tableName.length()-4);
- //System.out.println(table);
- StringBuffer sql=new StringBuffer("insert into library."+table+" values(");
- //利用标签的个数 ,形成这种形式:"insert into library."+table+" values(?,?,?,?)
- for (int i = 0; i < label.size(); i++) {
- sql.append("?");
- if (i!=label.size()-1) {
- sql.append(',');
- }
- }
- sql.append(')');
- //向数据库中写入数据
- try {
- pstmt=conn.prepareStatement(sql.toString());
- for (int i = 0; i < date.size(); i++) {
- for (int j = 0; j < date.get(i).length; j++) {
- pstmt.setObject(j+1, date.get(i)[j]);
- }
- pstmt.executeUpdate();
- }
-
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。