I am working on a java project using hibernate. I have a csv file that contains more than 200 data. I've successfully retrieved data from csv file. Now I have to insert those data to the table.
The problem is only the last row is being added to the table. Other rows are not being inserted.
The schema of the table is given below:
INSERT INTO `attendence_table`
(`serial_no` int auto-increment,
`employee_id` varchar2,
`in_time` varchar2,
`out_time` varchar2,
`attend_date` date)
The Attendence class is given below:
#Entity
#Table(name = "attendence_table")
public class Attendence {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "serial_no")
private int id;
#Column(name = "employee_id")
private String employee_id;
#Column(name = "in_time")
private String inTime;
#Column(name = "out_time")
private String outTime;
#Column(name = "attend_date")
private String date;
public String getEmployee_id() {
return employee_id;
}
public void setEmployee_id(String employee_id) {
this.employee_id = employee_id;
}
public String getInTime() {
return inTime;
}
public void setInTime(String inTime) {
this.inTime = inTime;
}
public String getOutTime() {
return outTime;
}
public void setOutTime(String outTime) {
this.outTime = outTime;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
The insert function is given below:
private static SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
public static void hibernateInsertAttendenceSession(List<Attendence> collection) {
Session session = sessionFactory.openSession();
session.beginTransaction();
for (Attendence obj : collection) {
session.save(obj);
System.out.println("Object Added");
}
session.getTransaction().commit();
session.close();
}
For your convenience, I'm also adding the glimpse of the csv file:
Test_company,TestId001,Test Name,2018/03/22,08:53:15,17:50:40
Test_company,TestId001,Test Name,2018/03/25,08:51:02,17:55:18
Test_company,TestId001,Test Name,2018/03/27,08:50:16,18:03:47
Test_company,TestId001,Test Name,2018/03/28,08:48:07,18:46:42
Test_company,TestId001,Test Name,2018/03/29,08:56:16,20:14:16
Thanks in advance for giving your valuable time to help me with this issue.
You are saving the reference of the Attendence object, while you're modifying it's content everytime.
You should probably instantiate an Attendence Object every time you attempt saving it.
for (Attendence obj : collection) {
Attendence newRef = new Attendence(obj);
session.save(newRef);
System.out.println("Object Added");
}
Sorry, My issue was in different place. Thank you all for your help. While retrieving data from csv file, there was a little error which created the issue. Thank you all for your time :)
In the readfromcsv function, previously I did the following:
public static void readFromExcel(String path) {
ArrayList<Attendence> attendences = new ArrayList<Attendence>();
String csvFile = path;
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
Attendence attendenceLine=new Attendence();
try {
br = new BufferedReader(new FileReader(csvFile));
//Attendence attendenceLine = new Attendence();
line = br.readLine();
while ((line = br.readLine()) != null) {
String[] data = line.split(cvsSplitBy);
if (data.length == 6) {
attendenceLine.setEmployee_id(data[1]);
attendenceLine.setDate(data[3]);
attendenceLine.setInTime(data[4]);
attendenceLine.setOutTime(data[5]);
}
else{
attendenceLine.setEmployee_id(data[1]);
attendenceLine.setDate(data[3]);
attendenceLine.setInTime("no punch");
attendenceLine.setOutTime("no punch");
}
attendences.add(attendenceLine);
}
for(Attendence attendence: attendences){
HibernateOperation.hibernateInsertOneAttendenceSession(attendence);
}
//HibernateOperation.hibernateInsertAttendenceSession(attendences);
} catch (FileNotFoundException ex) {
Logger.getLogger(AddToDatabaseOperation.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(AddToDatabaseOperation.class.getName()).log(Level.SEVERE, null, ex);
}
}
Here the attendenceLine String Variable was only having the last row as a reference value. Thats why for every iteration, I need to create the object again. I did the following to solve the issue.
public static void readFromExcel(String path) {
ArrayList<Attendence> attendences = new ArrayList<Attendence>();
String csvFile = path;
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
try {
br = new BufferedReader(new FileReader(csvFile));
//Attendence attendenceLine = new Attendence();
line = br.readLine();
while ((line = br.readLine()) != null) {
String[] data = line.split(cvsSplitBy);
Attendence attendenceLine=new Attendence();
if (data.length == 6) {
attendenceLine.setEmployee_id(data[1]);
attendenceLine.setDate(data[3]);
attendenceLine.setInTime(data[4]);
attendenceLine.setOutTime(data[5]);
}
else{
attendenceLine.setEmployee_id(data[1]);
attendenceLine.setDate(data[3]);
attendenceLine.setInTime("no punch");
attendenceLine.setOutTime("no punch");
}
attendences.add(attendenceLine);
}
for(Attendence attendence: attendences){
HibernateOperation.hibernateInsertOneAttendenceSession(attendence);
}
//HibernateOperation.hibernateInsertAttendenceSession(attendences);
} catch (FileNotFoundException ex) {
Logger.getLogger(AddToDatabaseOperation.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(AddToDatabaseOperation.class.getName()).log(Level.SEVERE, null, ex);
}
}
Please call new class and add all filed to this new class and save new class.
It will work.
for (Attendence obj : collection) {
Attendence newRef = new Attendence();
newRef.setSerialNo(obj.getSerialNo())
// set newRef to obj of all column......
session.save(newRef);
System.out.println("Object Added");
}
Related
I have an application that reads csv files in a folder and then reads those csv files and put them in a databse but I am not able to figure out how to populate database as soon as I run my spring boot program
my FlightService.java
package com.package.service;
#Service
public class FlightService{
File dir = new File("C:\\Users\\Akhil\\Desktop\\assignmentFour\\CSV");
static FlightDao flightDao = (FlightDao) AppContextUtil.context.getBean("flightDao");
public void readCSV() {
File files[] = dir.listFiles();
ArrayList<String> listofFileNames = new ArrayList<String>();
for (File file : files) {
Airline airline = FlightController.readFile(file);
flightDao.saveAirline(airline);
}
}
}
FlightController.java
package com.package.controller;
public class FlightController {
public static final SimpleDateFormat dateformat = new SimpleDateFormat("dd-MM-yyyy");
public static Airline readFile(File file) {
BufferedReader reader = null;
Airline aObj = new Airline();
aObj.setName(file.getName());
HashSet<Flight> flight_Set = new HashSet<Flight>();
try {
reader = new BufferedReader(new FileReader(file));
String line = reader.readLine();
line = reader.readLine();
while (line != null) {
Flight f = manipulateLine(line,aObj);
line = reader.readLine();
flight_Set.add(f);
}
} catch (Exception e) {
System.err.println("Could Not Read the File");
} finally {
if (reader != null) {
try {
reader.close();
} catch (Exception e) {
System.err.println("Could Not Close the File");
}
}
}
aObj.setFlights(flight_Set);
return aObj;
}
private static Flight manipulateLine(String line, Airline aObj) {
StringTokenizer st = new StringTokenizer(line, "|");
String flightNo = st.nextToken();
String depLoc = st.nextToken();
String arrLoc = st.nextToken();
String validTillDate = st.nextToken();
Date validTill = new Date();
try {
validTill = dateformat.parse(validTillDate);
} catch (ParseException e) {
System.err.print("Date not in appropriate(dd-MM-yyyy) format");
}
int flightTime = Integer.parseInt(st.nextToken());
Double flightDuration = Double.parseDouble(st.nextToken());
int fare = Integer.parseInt(st.nextToken());
String avail = st.nextToken();
Boolean seatAvailability;
if (avail.charAt(0) == 'Y')
seatAvailability = true;
else
seatAvailability = false;
String flightClass = st.nextToken();
return new Flight(flightNo, depLoc, arrLoc, fare, validTill,
flightTime, flightDuration, seatAvailability, flightClass,aObj);
}
}
For url
package com.package.controller;
#Controller
public class FlightCont {
#RequestMapping(value ="/flightSearch" , method=RequestMethod.POST)
public ModelAndView flightSearch(#Valid #ModelAttribute("flightDetails")FlightDetailsEntered flightDetails,BindingResult result){
ModelAndView modelAndView =new ModelAndView("flightSearch");
if(result.hasErrors())
{
System.err.println(result);
return modelAndView ;
}
List<Flight> listOfMatchingFlights= flightDetails.getListOfMatchingFlights();
modelAndView = new ModelAndView("flightList");
modelAndView.addObject("list", listOfMatchingFlights);
return modelAndView ;
}
}
As I run the program i am getting this error
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flightService' defined in file [C:\Users\Akhil\Desktop\project\target\classes\com\package\service\FlightService.class]: Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError
I don't know what I am doing wrong here any suggestions would be really helpful
You can keep file in classpath(src/main/resources) and load the file as mentioned below,
package com.package.service;
#Service
public class FlightService{
public void readCSV() {
InputStream stream = readerFile("CSV.txt");
try (BufferedReader br = new BufferedReader(new InputStreamReader(stream))) {
String line = br.readLine();
System.out.println(line);
} catch (IOException e) {
//do nothing
}
}
private InputStream readerFile(String fileName) {
return getClass().getClassLoader().getResourceAsStream(fileName);
}
}
I have included my code above. I have a buffered reader, which I am attempting to use to read textfiles.
I need Review + Restaurant otherwise I get review values printed as "null"
#Override
public Repository load(String filename) {
Repository repository = new Repository();
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
//ArrayList <Review> review=new ArrayList<>();
String[] revs;
String line = br.readLine();
while(line!=null){
revs=line.split(Character.toString(DELIMITER));
int id = Integer.valueOf(revs[0]);
String name= stripQuotes(revs[1]);
String location = stripQuotes(revs[2]);
Restaurant restaurant = new Restaurant(id, name,location);
int noreview=Integer.parseInt(revs[3]);
for (int i=0; i<noreview; i++) {
line = br.readLine();
revs=line.split(Character.toString(DELIMITER));
String reviewer = stripQuotes(revs[4]);
int rating= Integer.parseInt(revs[5]);
Review review = new Review (reviewer, rating);
repository.add(restaurant);
repository.add(review);
}
}
br.close();
} catch (IOException ex) {
Logger.getLogger(DAOTextImpl.class.getName()).log(Level.SEVERE, null, ex);
}
return repository;
}
I have a list of names in the form of a CSV and I am up for google searching those names using java. But the problem that i am facing is that when i initially run the code i am able to search the query but in the middle of the code the code starts to throw 503 exceptions and when i again run the code it starts throwing 503 exceptions from the very beginning.Here is the code that i am using.
public class ExtractInformation
{
static String firstname,middlename,lastname;
public static final int PAGE_NUMBERS = 10;
public static void readCSV()
{
boolean first = true;
try
{
String splitBy = ",";
BufferedReader br = new BufferedReader(new FileReader("E:\\KOLDump\\names.csv"));
String line = null;
String site = null;
while((line=br.readLine())!=null)
{
if(first)
{
first = false;
continue;
}
String[] b = line.split(splitBy);
firstname = b[0];
middlename = b[1];
lastname = b[2];
String name = null;
if(middlename == null || middlename.length() == 0)
{
name = firstname+" "+lastname+" OR "+lastname+" "+firstname.charAt(0);
}
else
{
name = firstname+" "+lastname+" OR "+lastname+" "+firstname.charAt(0)+" OR "+firstname+" "+middlename.charAt(0)+". "+lastname;
}
BufferedReader brs = new BufferedReader(new FileReader("E:\\KOLDump\\site.csv"));
while((site = brs.readLine()) != null)
{
if(first)
{
first = false;
continue;
}
String [] s = site.split(splitBy);
String siteName = s[0];
siteName = (siteName.replace("www.", ""));
siteName = (siteName.replace("http://", ""));
getDataFromGoogle(name.trim(), siteName.trim());
}
brs.close();
}
//br.close();
}
catch(Exception e)
{
System.out.println("unable to read file...some problem in the csv");
}
}
public static void main(String[] args)
{
readCSV();
}
private static void getDataFromGoogle(String query,String siteName)
{
Set<String> result = new HashSet<String>();
String request = "http://www.google.co.in/search?q="+query+" "+siteName;
try
{
Document doc = Jsoup.connect(request).userAgent("Chrome").timeout(10000).get();
Element query_results = doc.getElementById("ires");
Elements gees = query_results.getElementsByClass("g");
for(Element gee : gees)
{
Element h3 = gee.getElementsByTag("h3").get(0);
String annotation = h3.getElementsByTag("a").get(0).attr("href");
if(annotation.split("q=",2)[1].contains(siteName))
{
System.out.println(annotation.split("q=",2)[1]);
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
any suggestions on how to remove this exceptions from the code would really be helpful.
If you wait a little do the 503's go away? If so, then you're probably being rate-limited by Google. https://support.google.com/gsa/answer/2686272?hl=en
You may need to put some kind of delay between requests.
I have a text file, formatted as follows:
Han Solo:1000
Harry:100
Ron:10
Yoda:0
I need to make an arrayList of objects which store the player's name (Han Solo) and their score (1000) as attributes. I would like to be able to make this arrayList by reading the file line by line and splitting the string in order to get the desired attributes.
I tried using a Scanner object, but didn't get far. Any help on this would be greatly appreciated, thanks.
You can have a Player class like this:-
class Player { // Class which holds the player data
private String name;
private int score;
public Player(String name, int score) {
this.name = name;
this.score = score;
}
// Getters & Setters
// Overrride toString() - I did this. Its optional though.
}
and you can parse your file which contains the data like this:-
List<Player> players = new ArrayList<Player>();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt"))); // I used BufferedReader instead of a Scanner
String line = null;
while ((line = br.readLine()) != null) {
String[] values = line.split(":"); // Split on ":"
players.add(new Player(values[0], Integer.parseInt(values[1]))); // Create a new Player object with the values extract and add it to the list
}
} catch (IOException ioe) {
// Exception Handling
}
System.out.println(players); // Just printing the list. toString() method of Player class is called.
You can create a Class call player. playerName and score will be the attributes.
public class Player {
private String playerName;
private String score;
// getters and setters
}
Then you can create a List
List<Player> playerList=new ArrayList<>();
Now you can try to do your task.
Additionally, you can read from file and split each line by : and put first part as playerName and second part as score.
List<Player> list=new ArrayList<>();
while (scanner.hasNextLine()){
String line=scanner.nextLine();
Player player=new Player();
player.setPlayerName(line.split(":")[0]);
player.setScore(line.split(":")[1]);
list.add(player);
}
If you have Object:
public class User
{
private String name;
private int score;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getScore()
{
return score;
}
public void setScore(int score)
{
this.score = score;
}
}
Make an Reader class that reads from the file :
public class Reader
{
public static void main(String[] args)
{
List<User> list = new ArrayList<User>();
File file = new File("test.txt");
BufferedReader reader = null;
try
{
reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null)
{
String[] splitedString = line.split(":");
User user = new User();
user.setName(splitedString[0]);
user.setScore(Integer.parseInt(splitedString[1]));
list.add(user);
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (reader != null)
{
try
{
reader.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
for (User user : list)
{
System.out.println(user.getName()+" "+user.getScore());
}
}
}
The output will be :
Han Solo 1000
Harry 100
Ron 10
Yoda 0
Let's assume you have a class called Player that comprises two data members - name of type String and score of type int.
List<Player> players=new ArrayList<Player>();
BufferedReader br=null;
try{
br=new BufferedReader(new FileReader("filename"));
String record;
String arr[];
while((record=br.readLine())!=null){
arr=record.split(":");
//Player instantiated through two-argument constructor
players.add(new Player(arr[0], Integer.parseInt(arr[1])));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally{
if(br!=null)
try {
br.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
For small files (less than 8kb), you can use this
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class NameScoreReader {
List<Player> readFile(final String fileName) throws IOException
{
final List<Player> retval = new ArrayList<Player>();
final Path path = Paths.get(fileName);
final List<String> source = Files.readAllLines(path, StandardCharsets.UTF_8);
for (final String line : source) {
final String[] array = line.split(":");
if (array.length == 2) {
retval.add(new Player(array[0], Integer.parseInt(array[1])));
} else {
System.out.println("Invalid format: " + array);
}
}
return retval;
}
class Player {
protected Player(final String pName, final int pScore) {
super();
this.name = pName;
this.score = pScore;
}
private String name;
private int score;
public String getName()
{
return this.name;
}
public void setName(final String name)
{
this.name = name;
}
public int getScore()
{
return this.score;
}
public void setScore(final int score)
{
this.score = score;
}
}
}
Read the file and convert it to string and split function you can apply for the result.
public static String getStringFromFile(String fileName) {
BufferedReader reader;
String str = "";
try {
reader = new BufferedReader(new FileReader(fileName));
String line = null;
StringBuilder stringBuilder = new StringBuilder();
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append("\n");
}
str = stringBuilder.toString();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public static void main(String[] args) {
String stringFromText = getStringFromFile("C:/DBMT/data.txt");
//Split and other logic goes here
}
i have a frame that contain a button and textfield.
my porpuse is read ID number frome textfield and when clicked on button, my table should display that ID with its name and mark.
my "UserSearchFile.txt" is this:
12 joe 120
14 ted 220
19 alex 560
22 julia 668
my jButton6ActionPerformed whole code is this:
int idS=Integer.parseInt(jTextField2.getText());
final Vector data = new Vector();
final Vector column = new Vector();
File f=new File("D:\\UserSearchFile.txt");
try{
FileReader fr=new FileReader(f);
BufferedReader br1=new BufferedReader(fr);
String s;
while ((s = br1.readLine()) != null) {
String[] st = s.split(" ");
String id = st[0];
String name = st[1];
String mark = st[2];
if (id.equals(String.valueOf(idS))) {
try {
String line2;
FileInputStream fis = new FileInputStream("D:\\UserSearchFile.txt");
BufferedReader br2 = new BufferedReader(new InputStreamReader(fis));
StringTokenizer st1 = new StringTokenizer(br2.readLine(), " ");
while (st1.hasMoreTokens())
column.addElement(st1.nextToken());
while ((line2 = br2.readLine()) != null) {
StringTokenizer st2 = new StringTokenizer(line2, " ");
while (st2.hasMoreTokens())
data.addElement(st2.nextToken());
}
br2.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
} catch (IOException ex) {
Logger.getLogger(MainFrame1.class.getName()).log(Level.SEVERE, null, ex);
}
jTable1.setModel(new AbstractTableModel() {
public int getRowCount() {
return data.size() / getColumnCount();
}
public int getColumnCount() {
return column.size();
}
public Object getValueAt(int rowIndex, int columnIndex) {
return (String) data.elementAt((rowIndex * getColumnCount())
+ columnIndex);
}
});
jTable1.setVisible(true);
please help me and tell me how to write cleany and simple.
Start by using meaningful names for your variables and methods. jTable1, br1, br2 and jButton6ActionPerformed are not acceptable names.
Then try to split a complex method into 2 or three operations, themselves split into 2 or three operations, etc. Each operation should be a method call. For example:
private void readButtonClicked() {
String id = idTextField.getText();
Student student = findStudentWithId(id);
showStudentInGUI(student);
}
private Student findStudentWithId(String id) {
List<String> lines = readLinesInFile();
List<Student> students = transformLinesIntoStudents(lines);
Student studentWithId = findStudentWithId(students, id);
}
private Student findStudentWithId(List<Student> students, String id) {
for (Student student : students) {
if (student.getId().equals(id)) {
return student;
}
}
return null;
}
private List<Student> transformLinesIntoStudents(List<String> lines) {
List<Student> students = new ArrayList<Student>(lines.size());
for (String line : lines) {
students.add(parseStudentLine(line);
}
return students;
}
...