码迷,mamicode.com
首页 > 数据库 > 详细

java读取本地txt文件并插入数据库

时间:2017-03-06 17:34:30      阅读:273      评论:0      收藏:0      [点我收藏+]

标签:成功   path   文本   fileread   commit   resultset   []   char   connect   

  1. package com.cniia.ny.web.control.configManage;


  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.PreparedStatement;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. import java.util.Scanner;


  14. public class InsertIntoDB {
  15.         //读取文本操作
  16.         public static String[] writeToDat(String path) {
  17.                   File file = new File(path);
  18.                   List<String> list = new ArrayList<String>();
  19.                   String []strings = null;
  20.                   try {
  21.                    BufferedReader bw = new BufferedReader(new FileReader(file));
  22.                    String line = null;
  23.                    //因为不知道有几行数据,所以先存入list集合中
  24.                    while((line = bw.readLine()) != null){
  25.                     list.add(line);
  26.                    }
  27.                    bw.close();
  28.                   } catch (IOException e) {
  29.                    e.printStackTrace();
  30.                   }
  31.                   //确定数组长度
  32.                   strings = new String[list.size()];
  33.                   for(int i=0;i<list.size();i++){
  34.                    String s = (String) list.get(i);
  35.                    strings[i] = s;
  36.                   }
  37.                   return strings;
  38.                  }
  39.         //连接数据库
  40.         public static Connection getConnection(String data,String user,String pwd){
  41.                
  42.                 Connection conn = null;
  43.                 try {
  44.                         Class.forName("com.mysql.jdbc.Driver");
  45.                         conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/"+ data +"?characterEncoding=UTF-8",user , pwd);
  46.                 } catch (Exception e) {
  47.                         e.printStackTrace();
  48.                 }
  49.                 return conn;
  50.         }
  51.         //插入数据库,只能为一个字段
  52.         public static boolean insertInto(String data,String table,String field,String user,String pwd,String []str){
  53.                 try {
  54.                         Connection conn = getConnection(data,user,pwd);
  55.                         conn.setAutoCommit(false);
  56.                         String sql = "INSERT INTO"+ table + "("+ field +") VALUES (?);";
  57.                         PreparedStatement pstmt = conn.prepareStatement(sql);
  58.                         for (int i = 0; i < str.length; i++) {
  59.                                 pstmt.setString(1, str[i]);
  60.                                 pstmt.executeUpdate();
  61. //                        conn.commit();
  62.                         }
  63.                         conn.commit();
  64.                         return true;
  65.                 } catch (SQLException e) {
  66.                         // TODO Auto-generated catch block
  67.                         e.printStackTrace();
  68.                         return false;
  69.                 }
  70.                
  71.         }
  72.         public static String[] query(String data,String table,String field,String user,String pwd){
  73.                 try {
  74.                         String []str = null;
  75.                         int i = 0;
  76.                         Connection conn = getConnection(data,user,pwd);
  77.                         String sql = "select "+ field +" from" + table +";" ;
  78.                         PreparedStatement pstmt = conn.prepareStatement(sql);
  79.                         ResultSet rs = pstmt.executeQuery();
  80.                         while (rs.next()) {
  81.                                 str[i] = rs.getString(field);
  82.                                 i++;
  83.                         }
  84.                         return str;
  85.                 } catch (SQLException e) {
  86.                         // TODO Auto-generated catch block
  87.                         e.printStackTrace();
  88.                         return null;
  89.                 }
  90.                
  91.         }
  92.         public  static void main(String[] args){
  93.                 String []str2 = null;
  94.                 String []str = null;
  95.                 String []str3 = null;
  96.                 boolean flage1 = true;
  97.                 boolean flage2 = true;
  98.                 boolean flage3 = true;
  99.                 System.out.println("请输入目标文件绝对路径:");
  100.                 Scanner sc = new Scanner(System.in);
  101.                 String path = sc.next();
  102.                 while(flage3){
  103.                         if (path!=null&&!path.equals("")) {
  104.                                 flage3 = false;
  105.                                 str = writeToDat(path);
  106.                         }else {
  107.                                 System.out.println("输入不能为空");
  108.                         }
  109.                 }
  110.                 System.out.println("请输入所需连接的数据库名,表名,字段名(只能为一个),用户名,密码,用“#”隔开:");
  111.                 String strA = sc.next();
  112.                 while(flage1){
  113.                         if (strA!=null&&!strA.equals("")) {
  114.                                 flage1 = false;
  115.                                 while(flage2){
  116.                                         if (strA.indexOf("#")>=0) {
  117.                                                 flage2 = false;
  118.                                                 str2 = strA.split("#");
  119.                                         }else {
  120.                                                 System.out.println("您输入的格式有错,请重新输入");
  121.                                         }
  122.                                 }
  123.                         }else {
  124.                                 System.out.println("输入不能为空");
  125.                         }
  126.                 }
  127.                 boolean b = insertInto(str2[0], str2[1], str2[2], str2[3], str2[4],str);
  128.                 if(b){
  129.                         System.out.println("插入成功!");
  130.                 }else {
  131.                         System.out.println("插入失败!");
  132.                 }
  133.                 str3 = query(str2[0], str2[1], str2[2], str2[3], str2[4]);
  134.                 for (int i = 0; i < str3.length; i++) {
  135.                         System.out.println(str3[i]);
  136.                 }
  137.         }

  138. }
复制代码

java读取本地txt文件并插入数据库

标签:成功   path   文本   fileread   commit   resultset   []   char   connect   

原文地址:http://www.cnblogs.com/qq3111901846/p/6510789.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!