Java - Import .csv to database - Exclude last row - java

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);
}

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.

Read text file to insert specific table by using 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");
}
}

APOSTROPHE issue with java and SQL

I have code, where I have single quote or APOSTROPHE in my search
I have database which is having test table and in name column of value is "my'test"
When running
SELECT * from test WHERE name = 'my''test';
this works fine
If I use the same in a Java program I am not getting any error or any result
But If I give the name with only single quote then it works
SELECT * from test WHERE name = 'my'test';
Could you please help me out to understand.
Java code is
Connection con = null;
PreparedStatement prSt = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.
getConnection("jdbc:oracle:thin:#localhost:1521:orcl"
,"user","pwd");
String query = "SELECT * from "
+ "WHERE name = ? ";
prSt = con.prepareStatement(query);
String value = "my'mobile";
char content[] = new char[value.length()];
value.getChars(0, value.length(), content, 0);
StringBuffer result = new StringBuffer(content.length + 50);
for (int i = 0; i < content.length; i++) {
if (content[i] == '\'')
{
result.append("\'");
result.append("\'");
}
else
{
result.append(content[i]);
}
}
prSt.setObject(1, result.toString());
int count = prSt.executeUpdate();
System.out.println("===============> "+count);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally{
try{
if(prSt != null) prSt.close();
if(con != null) con.close();
} catch(Exception ex){}
}
You don't have to escape anything for the parameter of a PreparedStatement
Just use:
prSt = con.prepareStatement(query);
prSt.setString("my'mobile");
Additionally: if you are using a SELECT statement to retrieve data, you need to use executeQuery() not executeUpdate()
ResultSet rs = prst.executeQuery();
while (rs.next())
{
// process the result here
}
You might want to go through the JDBC tutorial before you continue with your project: http://docs.oracle.com/javase/tutorial/jdbc/index.html

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.sql.SQLException: [SQLITE_BUSY] The database file is locked

I am working in Eclipse with SQLite database, but I am facing the SQLITE_BUSY error. I've been reading for this problem for 2 days, and I have tried different things, such as closing PreparedStatement-s and Resultset-s every time or closing the connection. Even when i close a connection and open a new one, I get this error. Below is the code where i get the error. In this class there are several queries. The first ones being executed work fine, but when it comes to the last ones ( i mean the last being executed), the application crashes. I don't get it why this happens. I have seen almost all the posts for this problem, but still i couldn't fix it. Can anyone please help me? I really need to make it work. Thank you in advance!
My code:
//WHEN THIS METHOD IS CALLED I GET THE EXCEPTION
private void update(int a){
int index2=a;
try{String queryUpdate="Update Open_Orders set Quantity='"+mapTableProductQuantity.get(index2)+"' where Order_ID='"+orderID +"' and Table_ID='"+id_tavoline+"' and Product_ID='"+mapTableProductID.get(index2)+"'" ;
PreparedStatement pstUpdate = connection.prepareStatement(queryUpdate);
JOptionPane.showMessageDialog(null, "pst u be");
pstUpdate.execute();
JOptionPane.showMessageDialog(null, "u ekzekutua");
pstUpdate.close();
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
private void updateDatabase()
{ mapName_ID= new HashMap <String, Integer>();
try{String query="Select Product_Name, Product_ID from Products";
PreparedStatement pst= connection.prepareStatement(query);
ResultSet rsp= pst.executeQuery();
//int no=0;
while(rsp.next()){
mapName_ID.put(rsp.getString("Product_Name"), rsp.getInt("Product_ID"));
}
pst.close();
rsp.close();
}catch(Exception e){
}
if(busy==0)
{
try{String query="insert into Order_Table (Table_ID) values (?)";
//System.out.println("busy eshte "+busy);
PreparedStatement pstInsert = connection.prepareStatement(query);
pstInsert.setInt(1, id_tavoline);
pstInsert.execute();
pstInsert.close();}
catch(Exception e1){
JOptionPane.showMessageDialog(null, "e1 = " +e1);
}
setOrderID();
int totalRows=table.getRowCount();
for(int row=0; row<totalRows;row++)
{
try{
String prName=table.getModel().getValueAt(row, 0).toString();
int quantity = Integer.parseInt(table.getModel().getValueAt(row, 1).toString());
String queryInsert="Insert into Open_Orders (Order_ID, Table_ID, Product_ID, Quantity) values (?,?,?,?)";
PreparedStatement pstInsert1 = connection.prepareStatement(queryInsert);
pstInsert1.setInt(1, orderID);
pstInsert1.setInt(2, id_tavoline);
pstInsert1.setInt(3, mapName_ID.get(prName));
pstInsert1.setInt(3, quantity);
pstInsert1.execute();
pstInsert1.close();
//,id_tavoline, mapName_ID.get(""));
}catch(Exception e ){
JOptionPane.showMessageDialog(null, e);
}
}
}
else {
mapTableProductID= new HashMap<Integer,Integer>();
mapTableProductQuantity=new HashMap<Integer,Integer>();
mapDBProductID = new HashMap<Integer,Integer>();
mapDBProductQuantity = new HashMap<Integer,Integer>();
setOrderID();
int totalRows=table.getRowCount();
int i=0;
for(int row=0; row<totalRows;row++)
{
String name=table.getModel().getValueAt(row, 0).toString();
System.out.println("name= " +name);
mapTableProductID.put(row, mapName_ID.get(name));
System.out.println(" mapName_ID.get(name)= " + mapName_ID.get(name));
System.out.println("mapTableProductID.get(row) = " +mapTableProductID.get(row));
mapTableProductQuantity.put(row, Integer.parseInt(table.getModel().getValueAt(row, 1).toString()));
}
try{connection= sqliteConnection.dbConnector();
String querySelect= "select Product_ID, Quantity from Open_Orders where Order_ID='"+orderID+"' and Table_ID='"+id_tavoline+"'";
PreparedStatement pstSelect = connection.prepareStatement(querySelect);
ResultSet rs=pstSelect.executeQuery();
while(rs.next()){
mapDBProductID.put(i, rs.getInt("Product_ID"));
mapDBProductQuantity.put(i, rs.getInt("Quantity"));
i++;
}
pstSelect.close();
rs.close();
connection.close();
}catch(Exception e2){
JOptionPane.showMessageDialog(null, e2);
}
System.out.println("ne db ka i = " +i);
System.out.println("ne tabele ka = " +totalRows);
if(i>=totalRows){
System.out.println("Reshti 167");
int index1=0;
while(index1<i){
int index2=0;
//boolean found=false;
System.out.println("mapDBProductID.get(index1) = " +mapDBProductID.get(index1));
System.out.println("mapTableProductID.get(index2) = " +mapTableProductID.get(index2));
while((index2<totalRows)){
if(mapDBProductID.get(index1)!=mapTableProductID.get(index2)){
index2++;
}
else{//found=true;
System.out.println("Reshti 177");
if(mapDBProductQuantity.get(index1)!=mapTableProductQuantity.get(index2)){
System.out.println("Reshti 180");
System.out.println(orderID);
connection=sqliteConnection.dbConnector();
update(index2);
}else{
}
index1++;
break;
}
}
if(index2>=totalRows){
try{
String queryDelete="Delete from Open_Orders where Order_ID='"+orderID +"' and Table_ID='"+id_tavoline+"' and Product_ID='"+mapDBProductID.get(index1)+"'" ;
PreparedStatement pstDelete = connection.prepareStatement(queryDelete);
pstDelete.execute();
pstDelete.close();
}catch(Exception e4){
JOptionPane.showMessageDialog(null, "e4 = " +e4);
}
index1++;
}
}
}
if(i<totalRows){
int index1=0;
while(index1<totalRows){
int index2=0;
boolean found=false;
while((index2<i)&&(!found)){
if(mapTableProductID.get(index1)!=mapDBProductID.get(index2)){
index2++;
}else{found=true;
if(mapTableProductQuantity.get(index1)!=mapDBProductQuantity.get(index2)){
try{
String queryUpdate= "Update Open_Orders set Quantity='"+mapTableProductQuantity.get(index1)+"' where Order_ID='"+orderID +"' and Table_ID='"+id_tavoline+"' and Product_ID='"+mapTableProductID.get(index1)+"'" ;
PreparedStatement pstUpdate = connection.prepareStatement(queryUpdate);
pstUpdate.execute();
pstUpdate.close();
}catch(Exception e5){
JOptionPane.showMessageDialog(null, "e5 = " +e5);
}
}
break;
}
index2++;
}
if(index2>=i){
try {
String queryInsert="Insert into Open_Orders (Order_ID, Table_ID, Product_ID, Quantity) values (?,?,?,?)" ;
PreparedStatement pstInsert= connection.prepareStatement(queryInsert);
pstInsert.setInt(1, orderID);
pstInsert.setInt(2, id_tavoline);
pstInsert.setInt(3, mapTableProductID.get(index1));
pstInsert.setInt(1,mapTableProductQuantity.get(index1));
pstInsert.execute();
pstInsert.close();
}catch(Exception e10){
JOptionPane.showMessageDialog(null, "e10 = " +e10);
}
}
index1++;
}
}
}
}
private void setOrderID(){
try{
String query="select Order_ID from Order_Table where Table_ID='" + id_tavoline+"'";
PreparedStatement pst = connection.prepareStatement(query);
ResultSet rs=pst.executeQuery();
rs.next();
orderID=rs.getInt("Order_ID");
pst.close();
rs.close();
connection.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "setOrderID = "+e);
}
}

Categories