原 用java将本地文件中的数据插入mysql数据库

package com.test.demo.test;

import java.io.*;

import java.sql.*;

import java.util.ArrayList;

import java.util.List;

public class ReadFile {

//创建数据库连接

    static Connectionconn =null;

    //创建预编译语句对象

    static Statementpstmt =null;

    //创建一个结果集

    static ResultSetresult =null;

    /**

* 初始化数据库连接驱动

*/

    private static void initJdbc() {

try {

Class.forName("com.mysql.jdbc.Driver"); //加载驱动程序

            String url ="jdbc:mysql://localhost:3306/mock?useUnicode=true&characterEncoding=utf-8"; //数据库连接信息

            String user ="root"; //数据库用户名

            String pass ="666666"; //数据库密码

            conn = DriverManager.getConnection(url, user, pass); //获取连接

        }catch (ClassNotFoundException e) {

e.printStackTrace();

        }catch (SQLException e) {

e.printStackTrace();

        }

}

private static String[][]writeTODat(String path) {

File file =new File(path);

        List list =new ArrayList();

        String[][] strings =null;

        try {

FileReader fileReader =new FileReader(file);

            BufferedReader reader =new BufferedReader(fileReader);

            String line =null;

            while ((line = reader.readLine()) !=null) {

System.out.println(line);

                list.add(line);

            }

reader.close();

        }catch (FileNotFoundException e) {

e.printStackTrace();

        }catch (IOException e) {

e.printStackTrace();

        }

strings =new String[list.size()][3];

        for (int i =0; i < list.size(); i++) {

String[] st = list.get(i).split(";");

            for (int j =0; j < st.length; j++) {

strings[i][j] = st[j];

                //System.out.println(strings[i][j]);

            }

}

System.out.println("load data success!");

        return strings;

    }

//关闭数据库连接

    public static void closeCon(Connection con){

if(con !=null){

try{

con.close();

            }catch(Exception e){

e.printStackTrace();

            }

}

}

//插入数据

    public static boolean insertIntoTable(String table,String[][] str){

initJdbc();

        try{

String sql ="insert into " + table +" (" +"tablename,columnname,columnlength" +")"

                    +" values " +"(?,?,?)";

            PreparedStatement pstmt =conn.prepareStatement(sql);

            System.out.println(sql);

            for(int i=0;i

pstmt.setString(1,str[i][0]);

                System.out.println(str[i][0]);

                for(int j=1;j

System.out.println(str[i][j]);

                    pstmt.setString(j+1,str[i][j]);

                }

pstmt.execute();

            }

System.out.println("insert data success!");

            closeCon(conn);

return true;

        }catch(SQLException e){

e.printStackTrace();

            System.out.println("insert data fail!");

            closeCon(conn);

return false;

        }

}

public static void main(String[] args) {

String path ="/Users/jinhongmei/1/table.txt";

        //将文件数据读入二维数组

        String[][] strings =writeTODat(path);

        for(int i=0;i

for (int j =0; j < strings[i].length; j++) {

System.out.print(strings[i][j] +'\t');

            }

System.out.println();

        }

String table="test";

        //创建数据库连接,并向数据库中插入数据

        insertIntoTable(table,strings);

    }

}

你可能感兴趣的:(原 用java将本地文件中的数据插入mysql数据库)