• validationQuery验证数据库连接


    validationQuery是用来验证数据库连接的查询语句,这个查询语句必须是至少返回一条数据的SELECT语句。每种数据库都有各自的验证语句,下表中收集了几种常见数据库的validationQuery。

    DataBasevalidationQuery
    hsqldb select 1 from INFORMATION_SCHEMA.SYSTEM_USERS
    Oracle select 1 from dual
    DB2 select 1 from sysibm.sysdummy1
    MySql select 1
    Microsoft SqlServer select1
    postgresql select version()
    ingres select 1
    derby values 1
    H2 select 1


    如果你想支持多种数据库,你可以根据JDBC驱动来获取validationQuery,这里有个简单的类,根据JDBC驱动名称来获取validationQuery

    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
    
    public class ValidationQuery {
    
        public String getValidationQuery(String driver) {
            Properties properties = loadProperties();
            return properties.getProperty(driver, "");
        }
    
        private Properties loadProperties() {
            String propertyFilename = "db.validation.properties";
            try {
                Properties props = new Properties();
                InputStream resourceAsStream = this.getClass().
                getClassLoader().getResourceAsStream(propertyFilename);
                props.load(resourceAsStream);
                resourceAsStream.close();
                return props;
            } catch (IOException e) {
                throw new RuntimeException("Cannot load properties file '" + propertyFilename + "'.", e);
            }
        }
    
        //Example: Get validationQuery for hsqldb
        public static void main(String[] args) {
            System.out.println(new ValidationQuery().getValidationQuery("org.hsqldb.jdbcDriver"));
        }
    }

    创建“db.validation.properties”文件,并放置在classpath目录下

    #hsqldb
    org.hsqldb.jdbcDriver=select 1 from INFORMATION_SCHEMA.SYSTEM_USERS
    #Oracle
    oracle.jdbc.driver.OracleDriver=select 1 from dual
    #DB2
    com.ibm.db2.jcc.DB2Driver=select 1 from sysibm.sysdummy1
    #mysql
    com.mysql.jdbc.Driver=select 1
    org.gjt.mm.mysql.Driver=select 1
    #microsoft sql
    com.microsoft.sqlserver.jdbc.SQLServerDriver=select 1
    #postgresql
    org.postgresql.Driver=select version();
    #ingres
    com.ingres.jdbc.IngresDriver=select 1
    #derby
    org.apache.derby.jdbc.ClientDriver=values 1
    #H2
    org.h2.Driver=select 1
  • 相关阅读:
    less css
    Eclipse折叠代码 coffee bytes code folding
    jTDS jdbc驱动
    十点建议:从程序员变企业家 10 Tips for Moving From Programmer to Entrepreneur
    转:Hibernate Query examples (HQL) 示例
    [转]风雨7年话3D 长篇连载
    在游戏中使用CEGUI —— 第一章(底层)
    plusMark(正号硬件性能测试器)
    我会在月底之前将CEGUI相关的东西共享出来
    近期继超女之后的2大新闻
  • 原文地址:https://www.cnblogs.com/histlyb/p/7515564.html
Copyright © 2020-2023  润新知