Read text file to insert specific table by using java - java

i have this code ..this program is read text file but unable to insert ..pls suggest me
this is my text file
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
i want to insert only 1 column and second column rest of columns i dont need
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;
}

ya solved by using this code...thanks to support u guys, package
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");
}
}

Related

charAt(0) of String not returning expected value

I have a program that is reading a text file and inserting certain specifics into a SQLite DB. Each line on the text file starts with a V, D, A, N, T, I, or O. For example, a line would be
I,Sam,Jones,,123,1,Liz Jones,334-555-1234,INS123,INS Company, Smith, Broken Arm,1-1-2019, 1-3-2019
The issue I am having is reading the first line of the text file in my main method and the result is my else statement print line. I am storing the first character in char c and, when debugging, I can see the first "I" is being stored as "c = \uFEF". Every subsequent line works as designed, and I cannot figure out why.
I have tried initializing the variable first and then setting its value, I have tried using Scanner instead of BufferedReader. I have tried inserting a blank line at the beginning of the file.
import java.sql.*;
import java.io.*;
public class InsertData {
private static Connection connect(){
String url = "jdbc:sqlite:C://sqlite/Hospital.db";
Connection conn = null;
try {
conn = DriverManager.getConnection(url);
conn.createStatement().executeUpdate("PRAGMA foreign_keys = ON;");
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return conn;
}
public static void main(String[] args) throws IOException {
connect();
String patientFile = "Person.txt";
String treatmentFile = "treatment.txt";
String additionalDoctorsFile = "additional_doctors.txt";
try {
BufferedReader br = new BufferedReader(new FileReader(patientFile));
String line;
while((line = br.readLine()) != null) {
char c;
c = line.charAt(0);
if ( c == 'V') {
System.out.println("Inserting Volunteer to DB:\n" + line + "\n");
insertVolunteer(line);
}
else if (c == 'I') {
System.out.println("Inserting Inpatients to DB:\n" + line + "\n");
insertPatient(line);
insertAdmittedPatient(line);
}
else if (c == 'O') {
System.out.println("Inserting Outpatients to DB:\n" + line + "\n");
insertPatient(line);
}
else if (c == 'D') {
System.out.println("Inserting Doctor to DB:\n" + line + "\n");
insertDoctor(line);
}
else if (c == 'A') {
System.out.println("Inserting Administrator to DB:\n" + line + "\n");
insertAdministrator(line);
}
else if (c == 'N') {
System.out.println("Inserting Nurse to DB:\n" + line + "\n");
insertNurse(line);
}
else if (c == 'T') {
System.out.println("Inserting Technician to DB:\n" + line + "\n");
insertTechnician(line);
}
else {
System.out.println("Person input entered incorrectly formatted: " + line + "\n" +
"Record not being written to DB!\n");
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
BufferedReader br = new BufferedReader(new FileReader(treatmentFile));
String line;
while((line = br.readLine()) != null) {
String[] str = line.trim().split(",");
String sql = "INSERT INTO Treatment(patientLastname, doctorLastname, treatmentType, treatmentName, timestamp) VALUES (?,?,?,?,?);";
try (Connection conn = connect()) {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1,str[0]);
ps.setString(2, str[1]);
char t = str[2].trim().charAt(0);
if (t == 'M') {
ps.setString(3, "Medication");
}
else if (t == 'P') {
ps.setString(3, "Procedure");
}
else {
ps.setString(3, "N/A");
System.out.println("Treatment is neither a Procedure or Medication, unable to write to DB");
}
ps.setString(4, str[3]);
ps.setString(5, str[4]);
ps.executeUpdate();
ps.close();
System.out.println("Inserting Treatments to DB:\n" + line + "\n");
}
catch (SQLException e) {
e.printStackTrace();
}
}
BufferedReader br2 = new BufferedReader(new FileReader(additionalDoctorsFile));
String line2;
while((line2 = br2.readLine()) != null) {
String[] str2 = line2.trim().split(",");
String sql = "INSERT INTO AdditionalDoctors(firstname, lastname) VALUES (?,?);";
try (Connection conn = connect()) {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1,str2[0]);
ps.setString(2, str2[1]);
ps.executeUpdate();
ps.close();
System.out.println("Inserting Additional Doctors to DB:\n" + line2 + "\n");
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
System.out.println("All data inserted!");
}
public static void insertAdmittedPatient (String lineIn) {
String admittedPatient = lineIn;
String[] str = admittedPatient.trim().split(",");
String sql = "INSERT INTO AdmittedPatient(patientid, firstname, lastname, doctorLastname, admissionDate, dischargeDate) VALUES (?,?,?,?,?,?);";
try (Connection conn = connect()) {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1,str[4]);
ps.setString(2, str[1]);
ps.setString(3, str[2]);
ps.setString(4, str[10]);
ps.setString(5, str[12]);
ps.setString(6, str[13]);
ps.executeUpdate();
ps.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
public static void insertPatient (String lineIn) {
String inPatient = lineIn;
if (inPatient.charAt(0) == 'I') {
String[] str = inPatient.trim().split(",");
int arr = str.length;
String sql = "INSERT INTO Patient(patientid, firstname, lastname, roomNumber, insurance, insuranceNumber, " +
"emergencyContact, emergencyPhone, diagnosis, treatment, admitDate, dischargeDate) VALUES (?,?,?,?,?,?,?,?,?,?,?,?);";
try (Connection conn = connect()) {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1,str[4]);
ps.setString(2, str[1]);
ps.setString(3, str[2]);
int room = Integer.parseInt(str[5]);
if ((room >= 1) && (room <= 20)) {
ps.setString(4, str[5]);
}
else {
System.out.println("Invalid room number. Must be between 1-20\n" +
"Inserting 'Invalid' to DB");
ps.setString(4, "Invalid");
}
ps.setString(5,str[9]);
ps.setString(6, str[8]);
ps.setString(7,str[6]);
ps.setString(8, str[7]);
ps.setString(9, str[11]);
ps.setString(10, "inpatient");
ps.setString(11, str[12]);
ps.setString(12, str[13]);
ps.executeUpdate();
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
else {
String[] str = inPatient.trim().split(",");
String sql = "INSERT INTO Patient(patientid, firstname, lastname, roomNumber, insurance, insuranceNumber, " +
"emergencyContact, emergencyPhone, diagnosis, treatment, admitDate, dischargeDate) VALUES (?,?,?,?,?,?,?,?,?,?,?,?);";
try (Connection conn = connect()) {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1,str[4]);
ps.setString(2, str[1]);
ps.setString(3, str[2]);
ps.setString(4, "N/A");
ps.setString(5,"N/A");
ps.setString(6, "N/A");
ps.setString(7,"N/A");
ps.setString(8, "N/A");
ps.setString(9, "N/A");
ps.setString(10, "outpatient");
ps.setString(11, "n/a");
ps.setString(12, "n/a");
ps.executeUpdate();
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void insertVolunteer(String lineIn) {
String volunteer = lineIn;
String[] str = volunteer.trim().split(",");
String sql = "INSERT INTO Volunteer(firstname, lastname) VALUES (?,?);";
try (Connection conn = connect()) {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, str[1]);
ps.setString(2, str[2]);
ps.executeUpdate();
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void insertDoctor(String lineIn) {
String doctor = lineIn;
String[] str = doctor.trim().split(",");
String sql = "INSERT INTO Doctor(firstname, lastname, consultantPriveleges, admittingPriveleges) VALUES (?,?,?,?);";
try (Connection conn = connect()) {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, str[1]);
ps.setString(2, str[2]);
if (str[3].charAt(0) == 'A') {
ps.setString(3, "False");
ps.setString(4, "True");
}
else {
ps.setString(3, "True");
ps.setString(4, "False");
}
ps.executeUpdate();
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void insertAdministrator(String lineIn) {
String admin = lineIn;
String[] str = admin.trim().split(",");
String sql = "INSERT INTO Administrator(firstname, lastname) VALUES (?,?);";
try (Connection conn = connect()) {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, str[1]);
ps.setString(2, str[2]);
ps.executeUpdate();
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void insertNurse(String lineIn) {
String admin = lineIn;
String[] str = admin.trim().split(",");
String sql = "INSERT INTO Nurse(firstname, lastname) VALUES (?,?);";
try (Connection conn = connect()) {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, str[1]);
ps.setString(2, str[2]);
ps.executeUpdate();
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void insertTechnician(String lineIn) {
String admin = lineIn;
String[] str = admin.trim().split(",");
String sql = "INSERT INTO Technician(firstname, lastname) VALUES (?,?);";
try (Connection conn = connect()) {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, str[1]);
ps.setString(2, str[2]);
ps.executeUpdate();
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
And the first couple lines in my Person.txt file:
I,Tiera,Keeling,,5FVEJN66YMK4HT0RBEG1AWSUXJZ0FR,1,Alexa Keeling,729-811-8718,Aetna,AETOXJ989E9ZQ,Neva,Lead Poisoning,06-05-2019,06-10-2019
I,Miner,Jigalev,,P9VA7GJQ7E0MUF5XZ33EYYEM2LRZXZ,1,Micky Jigalev,843-545-3757,Red Cross,REDJW18A5IN6L,Jonathon,Ghonorrea,12-06-2010,12-09-2010
I,Briana,Fortier,,EM4QQ43Z8NNA1HPWWT34KJEEP8X8GF,1,Adlai Fortier,306-826-5478,Red Cross,REDLX0UX1W80F,Fernanda,Sprained Ankle,04-20-2012,04-21-2012
I,Yaritza,Aglinskas,,GKMDBWICY4Z32K58EQDWWZ0N88G3A0,1,Maryam Aglinskas,194-887-3283,Blue Shield,BLURZQL71DYX3,Sean,UTI,07-06-2010,07-10-2010
I,Latesha,Guadarrama,,MYRQ4VZU6ZGRD5NKPR3UWUCTYH3WZA,1,Tristen Guadarrama,237-775-1416,Red Cross,REDJKQ5UKF4FJ,Maurine,Poison Ivy,03-04-2018,03-14-2018
I,Jeffrey,Kane,,URC9D8OKUD1O6HEL19JW32AK7HO99O,1,Christen Kane,723-052-0159,Red Cross,REDSO7A2XJAEP,Mariel,Sinus Infection,07-15-2010,07-18-2010
I,Edd,Motoori,,AE2I2EOEHDXP436BCQN5CNOBYFVNJ6,1,Gilmer Motoori,371-640-2423,Aetna,AET5AZ7BEW2VX,Sincere,Dislocated Shoulder,03-20-2013,04-09-2013
I,Tiera,Keeling,,5FVEJN66YMK4HT0RBEG1AWSUXJZ0FR,1,Alexa Keeling,729-811-8718,Aetna,AETOXJ989E9ZQ,Neva,Lead Poisoning,06-05-2019,06-10-2019
I,Clive,Hiraoka,,0B18WK7PGCZK1C1NP1M9EAL55QABGG,1,Coby Hiraoka,840-607-8495,Aetna,AETXVFTRXQP2I,Corrie,Pulmonary Edema,05-28-2015,06-05-2015
I,Leoma,Adamo,,OFK9WMFEDJL1VB4PHSZ7S0FVE8DZ80,1,Tawny Adamo,730-337-9384,Aetna,AETLS5R8N8BDH,Annabel,Gun Shot,05-23-2013,06-13-2013
I,Melvin,Shahmametiev,,D82CEVQOULDEVDZCIXR82I2XXRXSPS,1,Ewin Shahmametiev,363-415-5748,Aetna,AETH6EEJU569K,Fernanda,Shark Bite,10-12-2012,10-14-2012
I,Florine,Evstifeev,,C5JFC4ZR6B7FOZ2MJF9MYGDVKY206X,1,Che Evstifeev,921-725-5356,Red Cross,REDY2CM6U6RWG,Maurine,Glaucoma,11-26-2016,12-11-2016
I,Richard,Nesvetaev,,NWEKL2CC1XNT79JDDFW8U2L3W8ERUQ,1,Malissa Nesvetaev,131-044-3236,Red Cross,REDWXUDH1EKRL,Annabel,Broken Leg,09-09-2013,09-24-2013
I,Cristen,Mihin,,XDBKTIYYSS6KNJ330NIX6HP3AZ4CTI,1,Mustafa Mihin,510-580-9174,Aetna,AET4FB0B0PZYB,Orvel,Trichinosis,05-16-2018,06-06-2018
I,Willard,Chauhan,,G8N1EF72MTWNY1JX52FAD5XVMEA1PR,1,Donia Chauhan,515-886-1297,Orange Cross,ORAX39RLBR93E,Warner,Heat Exhaustion,08-18-2018,09-06-2018
I,Georgeanna,Dover,,HDNDBYK9NUW349P9PBDGS8IGSTJW3Q,1,Hal Dover,433-308-4147,Green Cross,GREFYQQV2BIFZ,Mariann,Sun Burn,09-14-2012,10-03-2012
I,Deirdre,Woods,,ABP26C1H9UJV8136OO0Q0RL8DMJA2F,2,Aline Woods,708-426-3481,Red Cross,REDODODAIBLG7,Skip,Swine Flu,06-30-2018,07-20-2018
I,Micaela,Rapp,,EYRR3C2FOQV8S4BG0U7C6GQREMEKHS,1,Brea Rapp,413-458-6973,Aetna,AETKRGM4BEH85,Cayla,Tennis Elbow,11-05-2018,11-16-2018
I,Daren,Mao,,413CKEOQIGWLQXMMOMRJ3F0V4RH0I0,1,Kellen Mao,648-648-9784,Orange Cross,ORAQQE2VH9TQL,Suzie,Vitamin D Deficiency,06-24-2019,07-08-2019
I,Rickie,Kui,,5IWJ8147Z0MU8WZPPFLFLJCH5QIJC3,1,Kaylee Kui,784-354-0081,Green Cross,GREYLIUI532S5,Maurine,Depression,01-16-2018,01-17-2018
I,Dominic,Hagurov,,L55XCTOV9LTCIM36UWZ51AY7QI7O6W,1,Dwan Hagurov,969-614-8234,Aetna,AET0RKCRVCEHY,Skip,Broken Foot,04-19-2019,05-01-2019
I,Chantel,Blumenthal,,HZP1Z7H027CBMO9SXFJH7Y1ORTKDRM,1,Leda Blumenthal,971-142-8030,Orange Cross,ORAL2LG9ELABS,Fernanda,Ebola,02-10-2016,02-22-2016
I,Lettie,Eustis,,PHY0ECLVWHRP56OR5FWOGA8WPQIDAR,2,Kendrick Eustis,320-447-8781,Red Cross,REDN4V5IGOTVI,Neva,Altitude Sickness,05-29-2018,06-01-2018
O,Benjamen,Otmahov,,4I5GD8O068NFT8WQHM2YDA1G7SSIEC,,,,,,,,,
O,Leanna,Abramo,,LB2OAO9WAZBROTAZGLSZW555C3FWQ6,,,,,,,,,
O,Chauncy,Masih,,HYGDWYV9MDF63YMMU1NEM3CTAT8FVR,,,,,,,,,
O,Philomena,Zhmaev,,VZ5LF33XYGEN0JW2YWPW0V60XASX24,,,,,,,,,
O,Whit,Samuel,,DKTM0E0X9YHS56MWZ7OVYQGXNGLMP6,,,,,,,,,
O,Tyron,Niftrik,,GE7H7C0WRFSOLRMVJI7W6L5JE0KIRN,,,,,,,,,
O,Kasie,Georgeakopoulos,,HKXZ48V06NQPCU0BCZ7COLZJO7BY14,,,,,,,,,
O,Emmalee,Rapson,,JDXQM0I50NF36QQH6CG3DT3WTWEJRF,,,,,,,,,
O,Abigayle,Bertolini,,DRD3JQA7JXU8ZMT8HXOC43YVS1BE1P,,,,,,,,,
O,Marlene,Persov,,WDY33S66HNSZ4FJQSU2DE9CSM8ORBZ,,,,,,,,,
O,Reason,Nield,,L461W9IA3SP07FTG1UD55P3VKCAOEP,,,,,,,,,
O,Edson,Ivanov,,WI7OS8E6AGLR2CMVB0IJ1LNXGSK7IV,,,,,,,,,
O,Catharine,Chalupka,,YNJK0SVT7EC6EBQ7IWJ7AICMH86LG3,,,,,,,,,
O,Codey,Heywood,,UR0MAT4KTJ6K9R2K78RIS4TPF4GTT9,,,,,,,,,
O,Shawnna,Dopiro,,BD3XFNV8L2L1971F6QOSAUVVGCB6CY,,,,,,,,,
O,Shelton,Ruzsky,,OYQIYQEG8K2BVQG5AOQB8AP0R50K1O,,,,,,,,,
O,Coby,Oesterreicher,,7DNEEAWMZ9OHAT28RDUCOP2W3HH4AT,,,,,,,,,
O,Frieda,Karteshkin,,0D1RMQ7DQ6XC4JIEXT0FJK94LSW8OZ,,,,,,,,,
O,Mercedes,Abolins,,6RBHYMWZ3F87Y5HJH2B32NLEJMDU9B,,,,,,,,,
O,Suzanna,Rohlin,,M3WOZ4OO76F6ROG1D9C1498T7JCJV5,,,,,,,,,
O,Hilton,Aldworth,,W86SNM0P1CQDFCLZICGG1HQZRC1J7A,,,,,,,,,
O,Elfie,Judovich,,939GD4FLO9GRD0VN5KFMYFN8HO5G85,,,,,,,,,
O,Mintie,Yaimov,,NC628KXAW0PGE3C90VPPC6M68JE09H,,,,,,,,,
O,Clemma,Tovey,,HC1WYISJUQM08G6R703PYX1H31YEEM,,,,,,,,,
O,Johnpaul,Wizner,,SL4K5X73H28KK1LM07HL1AX3TD1CVK,,,,,,,,,
O,Barbara,Adrianov,,29OOCH5EE4WPWGMTV5BU322REOY73H,,,,,,,,,
O,Tobias,Dunne,,S8W38JEZMENNSL7XTWZF53636RKMI1,,,,,,,,,
O,Harding,Halatnikov,,6Q5D4243VISMIBG44WQSF88O8HRT8F,,,,,,,,,
O,Torrey,Kalitkin,,LRS7SRML51U05MLWRXSG8ZA9M959J8,,,,,,,,,
O,Demian,Dillon,,2Q1T1EMN4DFEGL90EKSJ46L11GFAH4,,,,,,,,,
O,Aubrey,Brooke,,ZFPUZZUR0XN5C1CD05FUGQAPW0RGJK,,,,,,,,,
You can use Apache IO's BOMInputStream to silently consume the BOM marker.
Buffered Reader br = new BufferedReader(
new InputStreamReader(
new BOMInputStream(
new FileInputStream(patientFile)))) ;
Created a new text file and copied the data over.

How Do I load data from a CSV file to a table using java? [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
I have a File called Customer_Info.csv which has sample data like :
CUST_ID FIRST_NAME LAST_NAME USER_TYPE REPORTS_TO_ID EMAIL FIRST_TIME_LOGGED_IN FIRST_TIME_LOGIN_DATE ADDRESS_LINE1 ADDRESS_LINE2 CITY STATE ZIP
10001 MICHELLE VILLEGAS3 Savings SRINATH MICHELLE.VILLEGAS3#NOBODY.COM Y 07-10-18 1050 WEST FIFTH STREET AZUSA IND 917023308
This is the table in which i need to store the data. I am storing everything in string format because it is just a staging table.
Create Table Customer_Info_ST
(
Cust_ID varchar(5),
First_Name varchar(15),
Last_Name varchar(15),
User_Type varchar(10),
Report_To_Id varchar(10),
Email varchar(30),
First_Time_Logged_In varchar(1),
First_Time_Login_Date varchar(11),
Address_Line1 varchar(30),
Address_Line2 varchar(30),
City varchar(15),
State varchar(3),
Zip varchar(12)
);
I have written the following code to store it in the table.
public class CustInfoDataLoader {
static String sql, tableName;
private static PreparedStatement pstmt;
private static Statement stmt;
private static final String filePath = "Customer_Info.csv";
public static void main(String[] args) throws SQLException, FileNotFoundException, IOException, ParseException {
readCsv("Customer_info_ST"); //Line 24 in my code
//readCsvUsingLoad();
}
private static void readCsv(String tableName) throws FileNotFoundException, IOException, SQLException, ParseException {
CSVReader csvreader = null;
try{
Reader reader = Files.newBufferedReader(Paths.get(filePath));
csvreader = new CSVReaderBuilder(reader).withSkipLines(1).build();
sql = "insert into ? values (?,?,?,?,?,?,?,?,?,?,?,?,?)";
ManageDBResource.createConnectionToDB();
pstmt = ManageDBResource.conn.prepareStatement(sql);
String[] rowData = null;
while((rowData = csvreader.readNext()) != null) {
pstmt.setString(1, tableName);
int i = 2;
String state = rowData[11];
for (String data : rowData) {
if(i == 9 && data != null && state != "IND") {
java.text.SimpleDateFormat source = new java.text.SimpleDateFormat("MM-dd-yyyy");
java.util.Date date = source.parse(data);
java.text.SimpleDateFormat target = new java.text.SimpleDateFormat("yyyy-MM-dd");
data = target.format(date).toString();
}
pstmt.setString(i++,data);
//System.out.print(data + " ");
}
int result = pstmt.executeUpdate();
//System.out.println();
if(result == 1) {
System.out.println("Data loaded Successfully.");
}
}
}
catch (Exception e) {
System.out.println(e.getMessage());
}
finally {
pstmt.close(); //Line 69 in my code file
csvreader.close();
}
}
and I get the following error:
Exception in thread "main" java.lang.NullPointerException
at CustInfoDataLoader.readCsv(CustInfoDataLoader.java:69)
at CustInfoDataLoader.main(CustInfoDataLoader.java:24)
Can you close PreparedStatement after you executeUpdate()?
Like mentioned in the above example:
String sql = " INSERT INTO TABLE_(name,email,phone,id) VALUES(?,?,?,?) ";
try {
BufferedReader bReader = new BufferedReader(new
FileReader("1000rows.csv"));
String line = "";
while ((line = bReader.readLine()) != null) {
try {
if (line != null)
{
String[] array = line.split(",+");
for(String result:array)
{
System.out.println(result);
//Create prepared Statement here and set them and execute them
PreparedStatement ps = yourConnecionObject.createPreparedStatement(sql);
ps.setString(1,str[0]);
ps.setString(2,str[1]);
ps.setString(3,str[2]);
ps.setString(4,strp[3])
ps.excuteUpdate();
ps. close()
//Assuming that your line from file after split will follow that sequence
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
finally
{
if (bReader == null)
{
bReader.close();
}
}
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}

Exporting database to csv file with java

so i found this code on the internet, basically what supposedly it can do is backup all the tables from a db, my question is on this line:
res = st.executeQuery("select * from xcms." + tableName);
i get the following excpetion exception: SQLException information
what does xcms. stands for? what else can i put here?
res = st.executeQuery("select * from " + tableName);
btw if i erase xcms. and put it like this ^, i can save only the first table not all the tables, thx
the sourcecode webpage:
https://gauravmutreja.wordpress.com/2011/10/13/exporting-your-database-to-csv-file-in-java/#comment-210
public static void main(String[] args) {
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String db = "gg";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "";
FileWriter fw;
try {
Class.forName(driver);
con = DriverManager.getConnection(url + db, user, pass);
Statement st = con.createStatement();
ResultSet res = st.executeQuery("SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = 'gg'");
List<String> tableNameList = new ArrayList<String>();
while (res.next()) {
tableNameList.add(res.getString(1));
}
String filename = "C:\\Users\\Angel Silva\\Documents";
for (String tableName : tableNameList) {
int k = 0;
int j = 1;
System.out.println(tableName);
List<String> columnsNameList = new ArrayList<String>();
res = st.executeQuery("select * from " + tableName);
int columnCount = getColumnCount(res);
try {
fw = new FileWriter("C:\\Users\\Angel Silva\\Documents\\popo1121.csv");
for (int i = 1; i <= columnCount; i++) {
fw.append(res.getMetaData().getColumnName(i));
fw.append(",");
}
fw.append(System.getProperty("line.separator"));
while (res.next()) {
for (int i = 1; i <= columnCount; i++) {
if (res.getObject(i) != null) {
String data = res.getObject(i).toString();
fw.append(data);
fw.append(",");
} else {
String data = "null";
fw.append(data);
fw.append(",");
}
}
fw.append(System.getProperty("line.separator"));
}
fw.flush();
fw.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
con.close();
} catch (ClassNotFoundException cnfe) {
System.err.println("Could not load JDBC driver");
cnfe.printStackTrace();
}catch (SQLException sqle) {System.err.println("SQLException information");}
}
public static int getRowCount(ResultSet res) throws SQLException {
res.last();
int numberOfRows = res.getRow();
res.beforeFirst();
return numberOfRows;
}
public static int getColumnCount(ResultSet res) throws SQLException {
return res.getMetaData().getColumnCount();
}
}
This is what I used:
public void sqlToCSV (String query, String filename){
log.info("creating csv file: " + filename);
try {
FileWriter fw = new FileWriter(filename + ".csv");
if(conn.isClosed()) st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
int cols = rs.getMetaData().getColumnCount();
for(int i = 1; i <= cols; i ++){
fw.append(rs.getMetaData().getColumnLabel(i));
if(i < cols) fw.append(',');
else fw.append('\n');
}
while (rs.next()) {
for(int i = 1; i <= cols; i ++){
fw.append(rs.getString(i));
if(i < cols) fw.append(',');
}
fw.append('\n');
}
fw.flush();
fw.close();
log.info("CSV File is created successfully.");
conn.close();
} catch (Exception e) {
log.fatal(e);
}
}
The xms stands for the Database name, in your Connection in the java program you already are establishing the connection to the Database:
(DriverManager.getConnection(url + db, user, pass);
The string db is the name of the DB to connect to.
So no need to have the xms. .just for example use this query:
"SELECT * FROM "+tableName+";"
This is executed in the database you are connected to, for example ggin your code.
For writting the CSV file chillyfacts.com/export-mysql-table-csv-file-using-java/ may help!
SELECT * FROM <MENTION_TABLE_NAME_HERE> INTO OUTFILE <FILE_NAME> FIELDS TERMINATED BY ',';
Example :
SELECT * FROM topic INTO OUTFILE 'D:\5.csv' FIELDS TERMINATED BY ',';
use opencsv dependency to export SQL data to CSV using minimal lines of code.
import com.opencsv.CSVWriter;
import java.io.FileWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class CsvWriter {
public static void main(String[] args) throws Exception {
CSVWriter writer = new CSVWriter(new FileWriter("filename.csv"), '\t');
Boolean includeHeaders = true;
Statement statement = null;
ResultSet myResultSet = null;
Connection connection = null;
try {
connection = //make database connection here
if (connection != null) {
statement = connection.createStatement();
myResultSet = statement.executeQuery("your sql query goes here");
writer.writeAll(myResultSet, includeHeaders);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}

SQL executeBatch slow processing

Basically i have to read a csv file and perform some validation.
If duplicate record is found i've to delete the previous record and insert the latest 1.
The file contains about 100k records. I'm not sure what I'm doing wrongly but it's taking way too long to load the data.
public static ArrayList<BootstrapMessage> loadLocation(File file) {
ArrayList<BootstrapMessage> errors = new ArrayList<BootstrapMessage>();
CSVReader reader = null;
Connection conn = null;
Connection conn2 = null;
PreparedStatement pstmt = null;
PreparedStatement ps = null;
try {
conn = ConnectionManager.getConnection();
conn2 = ConnectionManager.getConnection();
conn.setAutoCommit(false);
pstmt = conn.prepareStatement(INSERT_LOCATION);
ps = conn2.prepareStatement("delete from location where `timestamp` = ? AND mac_address = ?");
reader = new CSVReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
reader.readNext();//header
String[] record = reader.readNext();
int counter = 2;//starting from line 2. Line 1 is header
int validRecords = 0;
while (record != null) {
ArrayList<String> message = null;
//try {
message = ValidatorUtil.validateLocation(record, file.getName(), counter);
if (message != null) {//contains error
errors.add(new BootstrapMessage(file.getName(), counter, message));
} else {//valid record
String key = record[0] + record[1];
if (locations.containsKey(key)) {//duplicate found.
pstmt.executeBatch();
message = new ArrayList<String>();
message.add("duplicate row");
errors.add(new BootstrapMessage(file.getName(), locations.get(key), message));
//delete record from database
ps.setTimestamp(1, Timestamp.valueOf(record[0]));
ps.setString(2, record[1]);
ps.executeUpdate();
//inserting the latest record
pstmt.setTimestamp(1, Timestamp.valueOf(record[0]));
pstmt.setString(2, record[1]);
pstmt.setInt(3, Integer.parseInt(record[2]));
pstmt.addBatch();
if (validRecords % 2000 == 0) {
pstmt.executeBatch();
}
} else {
pstmt.setTimestamp(1, Timestamp.valueOf(record[0]));
pstmt.setString(2, record[1]);
pstmt.setInt(3, Integer.parseInt(record[2]));
pstmt.addBatch();
validRecords++;
if (validRecords % 2000 == 0) {
pstmt.executeBatch();
}
}
}
if (validRecords > 0) {
pstmt.executeBatch();
conn.commit();
}
record = reader.readNext();
counter++;
}
System.out.println("valid location records = " + validRecords);
//numOfValidRecords.put(fileName, validRecords);
if (!errors.isEmpty()) {
return errors;
}
} catch (FileNotFoundException ex) {
Logger.getLogger(LocationDAO.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(LocationDAO.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(LocationDAO.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException ex) {
Logger.getLogger(LocationDAO.class.getName()).log(Level.SEVERE, null, ex);
}
}
ConnectionManager.close(conn2, ps);
ConnectionManager.close(conn, pstmt);
}
return null;
}
Why don't you use native database loaders to do the job?
Or I would first insert all the records into staging and then do the duplicate removals by using the database tools, either SQL or some database procedure. This way it has to be faster.

Java - Import .csv to database - Exclude last row

I'm importing database values from a .csv file. However, the last row in the .csv is actually the totals of some columns, and my importer updates the database with that being the last row and that is undesirable. And that messes up the queries that I'll do later with it. Please help how can I prevent this?
My code of my .csv importer:
try
{
BufferedReader br=new BufferedReader(new FileReader("v.csv"));
String line;
while((line=br.readLine())!=null)
{
String[]value = line.split(",");
String sql = "INSERT into main ([Ticket #], Status, Priority, Department, [Account Name]) "
+ "values ('"+value[0]+"','"+value[1]+"','"+value[2]+"','"+value[3]+"','"+value[4]+"')";
System.out.println(sql);
PreparedStatement pst = null;
try
{
pst = db.prepareStatement(sql);
pst.executeUpdate();
}finally{
if(pst != null){
pst.close();
}
}
}
br.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, e);
}
Try this:
try
{
BufferedReader br=new BufferedReader(new FileReader("v.csv"));
ArrayList<String> lines = new ArrayList<String>();
String line;
while((line=br.readLine())!=null)
{
lines.Add(line);
}
br.close();
if (lines.size() > 1) { // be sure that the last row exists (prevents indexoutexception)
for (int i = 0; i < lines.size() - 1; i++) { // the - 1 excludes the last row
String[]value = lines.get(i).split(",");
String sql = "INSERT into main ([Ticket #], Status, Priority, Department, [Account Name]) "
+ "values ('"+value[0]+"','"+value[1]+"','"+value[2]+"','"+value[3]+"','"+value[4]+"')";
System.out.println(sql);
PreparedStatement pst = null;
try
{
pst = db.prepareStatement(sql);
pst.executeUpdate();
} finally {
if(pst != null) {
pst.close();
}
}
}
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, e);
}
If possible you can use the Apache commons IO ReversedLinesFileReader and read the CSV from the end. Then you can skip the first line.
Try this:
try
{
BufferedReader br=new BufferedReader(new FileReader("v.csv"));
String line;
String firstFlag =true;
while((line=br.readLine())!=null)
{
if (!firstFlag){
String sql = "INSERT into main ([Ticket #], Status, Priority, Department, [Account Name]) "
+ "values ('"+value[0]+"','"+value[1]+"','"+value[2]+"','"+value[3]+"','"+value[4]+"')";
System.out.println(sql);
PreparedStatement pst = null;
try
{
pst = db.prepareStatement(sql);
pst.executeUpdate();
}finally{
if(pst != null){
pst.close();
}
}
}
String[]value = line.split(",");
firstFlag=false;
}
br.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, e);
}

Categories