Posts

Showing posts from February, 2022

How To Fetch Data From MySQL table In Java Using Hibernate

Image
In this post, I will be showing you how to fetch data from MySQL table and show it in the console of your IDE using Hibernate and Java. If you have chosen Hibernate version 3.0 in your DTD in hibernate.cfg.xml file and if you refer to https://developer.jboss.org/docs/DOC-15062#api . you will find that query execution methods like find(), iterate(), filter() and delete() has been deprecated instead we'll be using createQuery() method. Later in this document, you will also get error for using Query class as it is deprecated but don't worry about it as it will still work. You can also read the document above completely to understand what new features are released and which ones have been removed in newer version. Suppose you have a Student class: @Entity public class Student{     @Id     private String id;     private String name; //getters and setters here : : : } and you have the following data in your Student MySQL table. ID|Name 1  | Sumit 2 | Am...

How To Update SQL Table from CSV Files Using Hibernate in Maven Java Project

Image
In this post, I will show you how to update your SQL table from CSV files using Hibernate in Maven. Before we move on, make sure you have followed below posts because its going to help us updating the SQL table with CSV data: 1.  How To Read CSV Files in Java as Maven Project (sumitkp.in) 2.  How To Create A Hibernate Project in Maven (sumitkp.in) In short, let me tell you what I will be doing. I will first convert CSV data into an object of Product class and then I will use the data inside the object to move them into our SQL table. In this project, we will be having three classes: App.java: This class will have the main method Product.java: This class will have all the variables and Getters and Setters and a parametrized constructors for storing data into these objects. readCSV.java: This class will be used for getting data from CSV files and then storing these data into our MySQL table. File: App.java ( package : in.sumitkp.HibernateProject ) Important packages to import...

How To Create A Hibernate Project in Maven

Image
In this post, I will be showing you how to create a Hibernate project using Maven and how we can add MySQL dependencies to it. Here, is a whole process picture which you can click and follow the steps. The direction of the process has been indicated by arrow icons. Now, configuring the Hibernate XML file: Inside your project folder, right-click on 'src/main/java' and select New > Other... > Type filter text: xml > Under XML folder, click on XML File > Click Next > Under File Name, type: hibernate.cfg.xml > Finish. Inside this hibernate.cfg.xml file, we would now need a dtd line. dtd stands for 'document type definition'. It defines the tagging structure of an XML document. For this, search on Google or any search engine: hibernate dtd. Find for the result that says "Index of /dtd - Hibernate". Click the link or go to  https://hibernate.org/dtd/  . On the next page, click on either of these three links. A dtd file will be downloaded. Open the ...

How To Read CSV Files in Java as Maven Project

Image
Many of us have problems reading CSV files in Java but not anymore!. In this guide, I will share how to read CSV files as Maven Project. For creating a Maven project: In your Eclipse IDE, click on File > New > Project... > Maven >Maven Project > Next > Check "Create a simple project" checkbox. Under Group Id: The name could be anything. It is same as any package name. E.g. in.sumitkp Under Artifact Id: Same as above It is same as any project name. E.g. maven-demo1 Let the Version be the default. Under Name: Give the name for your project Click Finish. In your Project Explorer, all the necessary files will be automatically generated. Inside the root folder, there will be an XML file auto-generated called  pom.xml . About pom.xml: POM stands for Project Object Model It is an XML file that contains information about the project and configuration details used by Maven to build the project. When running a task, Maven looks for the pom.xml file in the directory. I...

Java Thread That Will Look for New File Even After Program Has Ended

Image
In this post, I will be showing you how to write a thread program that will keep running even after the main program has ended, and will show files at a particular location that will keep updating after you add any new files into the folder. Prerequisite: Eclipse IDE Java 1.8 Steps: Right click on your project in Package Explorer , click New > Class . Leave the package name for now and let it be default. Type any class name in the Name field, check the public static void main(String[] args) checkbox and click Finish . Right-click on your project again, click New > Folder . Name the folder as " Files ". Paste here your files in this folder. For now lets paste 1 file. Suppose let's name the class: Class: BackgroundRunningThread.java Code: import java.io.File; public class BackgroundRunningThread {   public static void main(String[] args) {     Runnable r = new Runnable() {       @Override       public void run() {       ...