繁体   English   中英

使用Java读取文本文件以插入特定表

[英]Read text file to insert specific table by using java

我有此代码..此程序已读取文本文件,但无法插入..pls,建议我这是我的文本文件

1   2016-07-13 14:51:53 1   255 1   0
1   2016-07-13 14:52:42 1   255 1   0
1   2016-07-13 14:52:51 1   255 1   0
1   2016-07-13 14:53:06 1   255 1   0
1   2016-07-13 14:53:10 1   255 1   0
3   2016-07-16 16:07:34 1   255 1   0
4   2016-07-16 16:08:50 1   255 1   0
5   2016-07-16 16:09:33 1   255 1   0
4   2016-07-16 16:09:57 1   255 1   0

我只想插入第一列,第二列其余的列,我不需要

package com.om.whms.examples;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOExcep`
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Scanner;

public class Hello {

  public static void main(String[] args) {



        BufferedReader br = null;
        String strLine = "";
        try {
            br = new BufferedReader( new 
            FileReader("N:/Attendance/BiometricAttendance.txt"));
            Connection connection = DBConnection.getConnection();
            System.out.println("connection...");
            while( (strLine = br.readLine()) != null){
                strLine = strLine.trim().replaceAll("( )+", ",");
                strLine += "\n";
                System.out.println(strLine);
                if(!(strLine.startsWith("0") || strLine.startsWith("1") || 
                strLine.startsWith("2") || strLine.startsWith("3") ||
                        strLine.startsWith("4") || strLine.startsWith("5") 
             || strLine.startsWith("6") || strLine.startsWith("7") ||
                        strLine.startsWith("8") || strLine.startsWith("9") ) 
               ){ // Lines starting with number 0.
                    continue;
                }    
                System.out.println("connection..."+strLine);
                /*String query = "Insert into attendence values 
               ("+strLine+");";*/

                String query ="insert into attendence "
                        + " (biometric_id, date, log_time)" + " values ( 1, 
             2017-07-13, 14:51:53)";

                    PreparedStatement pstmt = 
                     connection.prepareStatement(query);
                    pstmt.executeUpdate();

                    br.close(); 
            }   
        } catch (FileNotFoundException e) {
            System.err.println("Unable to find the file: fileName");
        } catch (IOException e) {
            System.err.println("Unable to read the file: fileName");
        }
        catch(Exception e){System.err.println(e.getMessage());}
        }
     }
public static void main(String[] args) {

        BufferedReader br = null;
        String strLine = "";
        try {
            br = new BufferedReader(new FileReader("C:\\Users\\sutharm1\\Desktop\\test1.txt"));
            // Connection connection = DBConnection.getConnection();
            System.out.println("connection...");
            while ((strLine = br.readLine()) != null) {
                strLine = strLine.trim().replaceAll("( )+", ",");
                strLine += "\n";

                if (!(strLine.startsWith("0") || strLine.startsWith("1") || strLine.startsWith("2")
                        || strLine.startsWith("3") || strLine.startsWith("4") || strLine.startsWith("5")
                        || strLine.startsWith("6") || strLine.startsWith("7") || strLine.startsWith("8")
                        || strLine.startsWith("9"))) { // Lines starting with
                                                        // number 0.
                    continue;
                }
                // System.out.println("connection..."+strLine);

                String[] split = strLine.split(" ");

                String[] split1 = split[0].toString().split(",");
                /*System.out.println(split1[0]);
                System.out.println(split1[1]);
                System.out.println(split1[2]);*/

                System.out.println("biometric_id  " + split1[0] + "   date  : " +split1[1] + " log_time  "+split1[2]);

                Connection dbConnection = null;
                PreparedStatement preparedStatement = null;

                String insertTableSQL = "INSERT INTO DBUSER" + "(biometric_id, date, log_time) VALUES" + "(?,?,?)";

                try {
                    dbConnection = getDBConnection();
                    preparedStatement = dbConnection.prepareStatement(insertTableSQL);

                    preparedStatement.setString(1, split1[0]);
                    preparedStatement.setString(2, split1[1]);
                    preparedStatement.setString(3, split1[2]);

                    // execute insert SQL stetement
                    preparedStatement.executeUpdate();

                    System.out.println("Record is inserted into DBUSER table!");

                } catch (SQLException e) {

                    System.out.println(e.getMessage());

                } finally {

                    if (preparedStatement != null) {
                        preparedStatement.close();
                    }

                    if (dbConnection != null) {
                        dbConnection.close();
                    }

                }
            }
        } catch (FileNotFoundException e) {
            System.err.println("Unable to find the file: fileName");
        } catch (IOException e) {
            System.err.println("Unable to read the file: fileName");
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }

    private static Connection getDBConnection() {

        Connection dbConnection = null;

        try {

            Class.forName(DB_DRIVER);

        } catch (ClassNotFoundException e) {

            System.out.println(e.getMessage());

        }

        try {

            dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
            return dbConnection;

        } catch (SQLException e) {

            System.out.println(e.getMessage());

        }

        return dbConnection;

    }

通过使用此代码解决了...感谢您的支持,打包com.om.whms.examples;

import java.io.BufferedReader;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.PreparedStatement;

public class BiometricLogs {
public static void readBiometricFile(String fileName){
    BufferedReader br = null;
    try{
        br = new BufferedReader( new FileReader(fileName));
        Connection connection = DBConnection.getConnection();
        String strLine = "";
        while((strLine = br.readLine()) != null){
            String dateStr = strLine.substring(10, 20);
            String timeStr = strLine.substring(21, 30).trim();
            String idStr = (strLine.substring(0, 9)).trim();
            //System.out.println(idStr + "$" + dateStr + "$" + timeStr);
             String sql ="insert into attendence (biometric_id, date, 
       log_time)"
                     + " values ( "+idStr+", '"+dateStr+"', 
         '"+timeStr+"');";
             System.out.println(sql);
             try{
             PreparedStatement pstmt = connection.prepareStatement(sql);
                pstmt.executeUpdate();
             }catch(Exception e){
                 System.out.println(e.getMessage());
             }
        }

    }catch(Exception e){
        System.out.println(e.getMessage());
        e.printStackTrace();
    }


   }

   public static void main(String[] args) {
    // TODO Auto-generated method stub
    readBiometricFile("N:\\Attendance\\BiometricAttendance.txt");

}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM