Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将本机查询打印到控制台?_Java_Spring_Hibernate - Fatal编程技术网

Java 如何将本机查询打印到控制台?

Java 如何将本机查询打印到控制台?,java,spring,hibernate,Java,Spring,Hibernate,我正在创建用于数据库管理的web应用程序。其中一个功能是“解释器SQL”。 下面是我用来执行查询的代码: 示例1 1 public String executeSQL(String[] split){ 2 SessionFactory hibernateFactory = someService.getHibernateFactory(); 3 Session session = hibernateFactory.openSession(); 4 String m

我正在创建用于数据库管理的web应用程序。其中一个功能是“解释器SQL”。
下面是我用来执行查询的代码:

示例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  }
1公共字符串executeSQL(字符串[]拆分){
2 SessionFactory hibernateFactory=someService.getHibernateFactory();
3 Session Session=hibernateFactory.openSession();
4字符串消息=null;
5表示(int i=0;i连接.prepareStatement(查询).execute());
9
10 message=“成功!”;
11         }
12捕获(例外e){
13 message=((SQLGrammarException)e).getSQLException().getMessage();
14         }
15
16      }
第17课时结束();
18返回消息;
19  }
有人能告诉我如何调用“getResultList()”(第8行)吗?
我知道如何在该示例中执行此操作(使用createNativeQuery):

示例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  } 
1公共字符串executeSQL(字符串[]拆分){
2 SessionFactory hibernateFactory=someService.getHibernateFactory();
3 Session Session=hibernateFactory.openSession();
4字符串消息=null;
5表示(int i=0;i
但正如您所看到的(第14行),我不知道如何在这里捕获SQLException,因为我得到的不是SQLException,而是: 我无法访问该字段以获取消息(它是私有的): 我需要将该消息打印到html中。因此,我正在尝试另一种方法来执行查询prepareStatement()。

任何人都知道如何将该查询打印到控制台(示例1)

要将SQL查询打印到控制台,请将以下内容添加到应用程序。属性文件:

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

希望能有所帮助!

谢谢,但有没有办法在java中将“值”添加到某个列表中?我相信您需要使用记录器,但不幸的是,我不知道如何操作。请看这里: