• Spring setConnectionProperties方法:设置数据库连接的各种属性

    该方法用于设置数据库连接的各种属性。

    语法:

    setConnectionProperties(Properties connectionProperties)

    参数说明:

    • connectionProperties:连接数据库的属性集合对象,该对象是 Properties 类的示例对象。

    示例

    本示例创建一个属性集合,将数据库连接需要的属性添加到该属性集合中,然后将该属性集合 setConnectionProperties 方法添加到 DriverManagerDataSource 类的示例对象中,关键代码如下:

    public static void main(String[] args){
      String driver = "com.mysql.jdbc.Driver";
      String url = "jdbc:mysql://lzw:3306/testDatabase";  //创建一个表示数据库路径的字符串
      DriverManagerDataSource dmd = new DriverManagerDataSource(driver,url,"root","111");
      Properties props = new Properties();
      props.setProperty("useUnicode","true");
      props.setProperty("characterEncoding","GB2312");
      dmd.setConnectionProperties(props);  //设置连接属性集
      if(props!=null){  //获取连接属性集并输出
        props.list(System.out);
      }
    }

更多...

加载中...