How to get database metadata from entity manager

前端 未结 6 1761
梦如初夏
梦如初夏 2021-02-07 09:00

I have an app which is using hibernate and jpa. I need to find out which db it is connected to so that some native sql query i execute based on db say for eg. oracle and postgre

6条回答
  •  悲&欢浪女
    2021-02-07 09:21

    In case you want yo get the dba name from the url

    public String getDbName() throws SQLException {
        SessionImplementor sessionImp = (SessionImplementor) em.getDelegate();
        DatabaseMetaData metadata = sessionImp.connection().getMetaData(); 
    
        String dbName="";
        Pattern urlDbName = Pattern.compile("databaseName=([A-Za-z0-9\\-\\_]+)");
        Matcher m = urlDbName.matcher(metadata.getURL());
        while (m.find()) {
            dbName = m.group(1);
        }
    
        return dbName;   
    }
    

提交回复
热议问题