LaVOZs

The World’s Largest Online Community for Developers

'; java - How to print native query into console? - LavOzs.Com

I am creating web-app to database management. One of functionalities is "interpreter SQL".
Here is my code which is used to execute queries:

Example 1

1  public String executeSQL(String[] split){
2      SessionFactory hibernateFactory = someService.getHibernateFactory();
3      Session session = hibernateFactory.openSession();
4      String message = null;
5      for (int i = 0; i < split.length; i++) {
6          try{
7              String query = split[i];
8              session.doWork(connection -> connection.prepareStatement(query).execute());
9      
10             message = "Success!";
11         }
12         catch(Exception e){
13             message = ((SQLGrammarException) e).getSQLException().getMessage();
14         }
15      
16      }
17      session.close();
18      return message;
19  }

Can someone tell me how to call "getResultList()"(line 8)?
I know how to do this in that example (with createNativeQuery):

Example 2

1  public String executeSQL(String[] split){
2      SessionFactory hibernateFactory = someService.getHibernateFactory();
3      Session session = hibernateFactory.openSession();
4      String message = null;
5      for (int i = 0; i < split.length; i++) {
6          try{
7              String query = split[i];
8              EntityManager entityManager = hibernateFactory.createEntityManager();
9              List<Object[]> resultList = entityManager.createNativeQuery(query).getResultList();
10             resultList.stream().map(Arrays::toString).forEach(System.out::println);
11             message = "Success!";
12          }
13         catch(Exception e){
14              // i can't catch SQLException here :( i mean i don't know how...
15         }
16  
17      }
18      session.close();
19      return message;
20  } 

but as you can see (line 14) I don't know how to catch SQLException here because instead of SQLException I am getting this: enter image description here and i am not able to get access to that field to get the message (it is private): enter image description here I need that message to print it into my html. So i am trying another method to execute queries prepareStatement().
Anyone have an idea how to print that query into console (Example 1)?

To have your SQL queries printed into your console, add the following to your application.properties file:

logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

Hope it helps!

Related
How do I test a private function or a class that has private methods, fields or inner classes?
How do I efficiently iterate over each entry in a Java Map?
How do I read / convert an InputStream into a String in Java?
How do I generate random integers within a specific range in Java?
What's the simplest way to print a Java array?
How do I convert a String to an int in Java?
How to fix 'android.os.NetworkOnMainThreadException'?
How to create a memory leak in Java?
Why does this code using random strings print “hello world”?
Why is printing “B” dramatically slower than printing “#”?