zoukankan      html  css  js  c++  java
  • java读取本地txt文件并插入数据库

    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. }
    复制代码
    荣耀存于心,而非留于形我还在寻找,回家的路长路漫漫,唯剑做伴且随疾风前行,身后一许流星吾虽浪迹天涯, 却未迷失本心
  • 相关阅读:
    利用PHP SOAP实现WEB SERVICE
    PHP 简单的加密解密算法
    Java 简单的加密解密算法
    去掉php框架CI默认url中的index.php
    PHP 日期计算函数【差异天数】
    PHP生成迅雷、快车、旋风等软件的下载链接代码实例
    转:苹果iphone APP界面设计尺寸官方版
    iOS开发——密码存储之keychain的使用
    Simple iPhone Keychain Access
    View的简单说明
  • 原文地址:https://www.cnblogs.com/qq3111901846/p/6510789.html
Copyright © 2011-2022 走看看