当前位置:   article > 正文

把数据库文件写入csv文件和从csv文件中导入数据到数据库中_为什么要将数据写入到csv文件中再导入数据库

为什么要将数据写入到csv文件中再导入数据库
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.sql.Connection;
  9. import java.sql.PreparedStatement;
  10. import java.sql.ResultSet;
  11. import java.sql.ResultSetMetaData;
  12. import java.sql.SQLException;
  13. import java.sql.Statement;
  14. import java.util.StringTokenizer;
  15. import java.util.Vector;
  16. import javax.swing.JOptionPane;
  17. public class importAndExportManager {
  18. public static void main(String[] args) {
  19. //new connectDB().setDbName("mysql");
  20. //new importAndOutportManager().importDate("borrow.csv");;
  21. //System.out.println("end");
  22. }
  23. private Connection conn=connectDB.getConnection();
  24. private Statement stmt;
  25. private PreparedStatement pstmt;
  26. private ResultSetMetaData rsmd=null;
  27. private ResultSet rs=null;
  28. /*
  29. * 获得数据库文件的内容 存放在ResultSet 的对象中
  30. */
  31. public void getDate(String DBname){
  32. String sql="select * from library."+DBname;
  33. try {
  34. stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
  35. rs=stmt.executeQuery(sql);
  36. rsmd=rs.getMetaData();
  37. } catch (SQLException e) {
  38. // TODO Auto-generated catch block
  39. e.printStackTrace();
  40. JOptionPane.showMessageDialog(null, e.getMessage());
  41. }
  42. }
  43. public void exportDate(String DBname) throws SQLException{
  44. int row=0;
  45. int column=0;
  46. Object [][]booklist=null; //数据内容
  47. String tableName[]=null; //标签内容
  48. getDate(DBname);
  49. column=rsmd.getColumnCount();
  50. while (rs.next()) {
  51. row++;
  52. }
  53. booklist=new Object[row][column];
  54. tableName=new String[column];
  55. for (int i = 1; i < tableName.length+1; i++) {
  56. tableName[i-1]=rsmd.getColumnLabel(i);
  57. }
  58. for (int i=0;i<row;i++){
  59. rs.previous();
  60. for (int j = 0; j < column; j++) {
  61. booklist[i][j]=rs.getObject(j+1);
  62. }
  63. }
  64. //写入csv文件中
  65. File csv = new File("E:/Javaproject/library/"+DBname+".csv");
  66. try {
  67. BufferedWriter bw = new BufferedWriter(new FileWriter(csv, false));
  68. for (int i = 0; i < tableName.length; i++) {
  69. bw.write(tableName[i]);
  70. if (i!=tableName.length-1) {
  71. bw.write(",");
  72. }
  73. }
  74. bw.write("\r\n");
  75. for (int i=0;i<row;i++){
  76. for (int j = 0; j < column; j++) {
  77. bw.write(booklist[i][j].toString());
  78. if (j!=column-1) {
  79. bw.write(",");
  80. }
  81. }
  82. bw.write("\r\n");
  83. }
  84. bw.close();
  85. } catch (IOException e) {
  86. // TODO Auto-generated catch block
  87. e.printStackTrace();
  88. }
  89. }
  90. /*
  91. * 从CSV文件中读取内容,存入一个Vector<Object []>对象中。
  92. */
  93. public void importDate(String inputFilePath){
  94. Vector<Object []> date = null;
  95. Object oneLineDate[];
  96. Vector<String>label=new Vector<String>();
  97. int column=0;
  98. File file=new File(inputFilePath);
  99. try {
  100. BufferedReader br=new BufferedReader(new FileReader(file));
  101. String line="";
  102. line=br.readLine();
  103. StringTokenizer st = new StringTokenizer(line, ",");
  104. while (st.hasMoreElements()) {
  105. label.add(st.nextToken());//得到标签
  106. column++; //利用标题行计算出总共有多少列行
  107. }
  108. oneLineDate=new Object[column];
  109. date=new Vector<Object[]>();
  110. for (int i=0;(line=br.readLine())!=null;i++) {
  111. StringTokenizer st1 = new StringTokenizer(line, ",");
  112. for(int j=0;st1.hasMoreElements();j++){
  113. oneLineDate[j]=st1.nextElement();
  114. }
  115. date.add(i,oneLineDate);
  116. oneLineDate=new Object[column];
  117. }
  118. br.close();
  119. } catch (FileNotFoundException e) {
  120. // TODO Auto-generated catch block
  121. e.printStackTrace();
  122. } catch (IOException e) {
  123. // TODO Auto-generated catch block
  124. e.printStackTrace();
  125. }
  126. insertDate(label,date, file.getName());
  127. }
  128. /*
  129. *
  130. */
  131. public void insertDate(Vector<String>label,Vector<Object []> date ,String tableName){
  132. String table=tableName.substring(0, tableName.length()-4);
  133. //System.out.println(table);
  134. StringBuffer sql=new StringBuffer("insert into library."+table+" values(");
  135. //利用标签的个数 ,形成这种形式:"insert into library."+table+" values(?,?,?,?)
  136. for (int i = 0; i < label.size(); i++) {
  137. sql.append("?");
  138. if (i!=label.size()-1) {
  139. sql.append(',');
  140. }
  141. }
  142. sql.append(')');
  143. //向数据库中写入数据
  144. try {
  145. pstmt=conn.prepareStatement(sql.toString());
  146. for (int i = 0; i < date.size(); i++) {
  147. for (int j = 0; j < date.get(i).length; j++) {
  148. pstmt.setObject(j+1, date.get(i)[j]);
  149. }
  150. pstmt.executeUpdate();
  151. }
  152. } catch (SQLException e) {
  153. // TODO Auto-generated catch block
  154. e.printStackTrace();
  155. }
  156. }
  157. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/606865
推荐阅读
相关标签
  

闽ICP备14008679号