【问题标题】:Verifying the database which is connected - Java persistence API验证连接的数据库 - Java 持久性 API
【发布时间】:2012-05-07 19:58:03
【问题描述】:

我正在使用 eclipse、eclipseLink、java 持久性 API (JPA) 和两个具有相同结构(相同表)的数据库(MYSQL 和 SQLServer)进行 java 项目。有时我需要检查用于应用条件语句(例如“if”或其他)的数据库。问题是: 如何使用参数中的信息验证我是否连接到 MySQL 或 SQLServer 数据库?

【问题讨论】:

    标签: java mysql sql-server eclipse eclipselink


    【解决方案1】:

    创建一个布尔值 connectedToDatabase 并将其初始化为 false。 创建一个将建立连接的 try and catch 块。在尝试将 connectedToDatabase 初始化为 true。如果在尝试连接时捕获到异常,布尔值将保持为 false。

    private boolean connectedToDatabase = false;
    
    try
    {
        connection = DriverManager.getConnection(urlOfDatabase);
        connectedToDatabase = true;
    }catch(SQLException sqlException){
        sqlException.printStackTrace();}
    

    然后您可以创建一个 if 语句,如果 connectedToDatabase 为 false,则该语句将引发异常。只需确保该方法抛出 IllegalStateException。

    if(!connectedToDatabase)
         throw new IllegalStateException("Not Connected To Database");
    

    如果服务器出现故障,这将不起作用,因为它只会确定初始连接是否成功。

    【讨论】:

    • 这不是答案,因为您发布了用于 JAVA 中传统数据库连接的代码。这不适用于使用 JPA 和 persistence.xml 文件的项目。
    【解决方案2】:

    我解决了在 persistence.xml 配置中读取标签 propertyAttribute name="eclipselink.target-database" 的问题文件。我使用了这些导入:

    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.DocumentBuilder;
    import org.w3c.dom.Document;
    import org.w3c.dom.NodeList;
    import org.w3c.dom.Node;
    import org.w3c.dom.Element;
    

    这是我用来读取属性的函数:

    public String DatabaseIdentifier() 
    {
        String dbIdentifier=null ;
        try {
            //Creates a virtual file where is allowed for default the persistence.xml configurarion file
            File path= new File("src\\META-INF");
    
            //Using the xml libraries prepare the document to be read.
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(new File(path+"\\persistence.xml"));
            doc.getDocumentElement().normalize();
    
            //Gets the list of properties  contained in the persistence.xml file
            NodeList listProperties= doc.getElementsByTagName("property");
    
            //We searching in the list of properties the attribute with the name 
            //eclipselink.target-database and there we get the database that 
            //is in use.
            for (int i = 0; i < listProperties.getLength(); i ++) {
                Node team = listProperties.item(i);
    
                if (team.getNodeType() == Node.ELEMENT_NODE)    
                {
                    Element element = (Element) team;
    
                if (element.getAttribute("name").contains("eclipselink.target-database"))
                        {
                    dbIdentifier=element.getAttribute("value").toString();
                    System.out.println(element.getAttribute("value"));
                        }
    
                }
            }
        } catch (Exception e) 
        {e.printStackTrace();}
        return dbIdentifier;
    }
    

    在另一个类中,您应该将此方法的返回值分配给 String。这就是答案,现在您可以在“if”之类的条件语句中使用此信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-10
      • 1970-01-01
      • 2010-09-21
      • 2015-06-09
      • 1970-01-01
      • 2011-02-03
      • 2011-08-09
      • 1970-01-01
      相关资源
      最近更新 更多