Solved

How to execute SQL statements from txt file using Hibernate

Posted on 2009-05-05
3
2,655 Views
Last Modified: 2013-11-23
Hello,

I have a program, the user load a txt file that contain a list of SQL statements. I would like to know how can execute these SQL statements using Hibernate taking as a starting point that the user will load a list of Conventional SQL statements.

How can I do that?

Thank you for help.
0
Comment
Question by:Fionageo80
  • 2
3 Comments
 
LVL 8

Expert Comment

by:ozlevanon
ID: 24304033
There is some information missing - which way do the commands appear in the file? Should something be done with their result? etc.
The following code is assuming there is one SQL command per line (i.e. there is a newline character between each command) and that these are not query statement for which the program should analyze the result.

Note that in this example the session is closed after all the commands, but you can close it and open a new one after each command.
			SessionFactory sessionFactory = null; // init this the way you usually do.
			FileReader fr = new FileReader(args[0]);
			BufferedReader br = new BufferedReader(fr);
			Session session = sessionFactory.getCurrentSession();
			session.beginTransaction();
			while (true)
			{
				String sql = br.readLine();
				if (sql == null)
				{
					break;
				}
				try
				{
					int result = session.createSQLQuery(sql).executeUpdate();
					System.out.printf("Got result %d from '%s'\n", result, sql);
				}
				catch (Exception e)
				{
					System.err.printf("Error executing '%s'\n", sql);
				}
			}
			session.getTransaction().commit();
			fr.close();

Open in new window

0
 

Author Comment

by:Fionageo80
ID: 24304184
Thank you for your quick reply.

Actually I have to use Hibernate.
The SQL statements are listed as you can see below, one statement after the other.

I have a EntityBean.java  that gets the EntityManager.

**** I want to know if I can execute directly these statements using entityManager.createNativeQuery? or createQuery? *****

Yes, I have to post process the result of those queries.

Thank you!


select * from user a where status= 1 and not exists(select * from detail where id= a.id)
select * from user a where status <> 1 and exists(select * from detail where id= a.id)

Open in new window

0
 
LVL 8

Accepted Solution

by:
ozlevanon earned 500 total points
ID: 24304296
This example is for hibernate, but uses a SessionFactory which creates a session.
If you're using EntityManager you should call createNativeQuery for SQL queries, whereas createQuery is for HQL (hibernate query language) statements.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Oracle SQL syntax check  without executing 6 50
servlet example issue 6 38
java stored proc example 9 29
jboss wildfly 10.1 10 76
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now