jdbc连接mysql各种情况总结

连接URL格式

http://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html

单库简单连接

jdbc:mysql://localhost:3306/sakila?profileSQL=true

多库failover设置

# Connection URL for a server failover setup: 
jdbc:mysql//primaryhost,secondaryhost1,secondaryhost2/test

loadbalance和复制设置

# Connection URL for load balancing: 
jdbc:mysql:loadbalance://localhost:3306,localhost:3310/sakila

# Connection URL for server replication: 
jdbc:mysql:replication://master,slave1,slave2,slave3/test

连接配置属性

Configuration properties define how Connector/J will make a connection to a MySQL server. Unless otherwise noted, properties can be set for a DataSource object or for a Connection object.

Configuration properties can be set in one of the following ways:

  1. Using the set*() methods on MySQL implementations of
    java.sql.DataSource (which is the preferred method when using
    implementations of java.sql.DataSource):

    com.mysql.jdbc.jdbc2.optional.MysqlDataSource

    com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource

  2. As a key/value pair in the java.util.Properties instance passed to DriverManager.getConnection() or Driver.connect()

  3. As a JDBC URL parameter in the URL given to java.sql.DriverManager.getConnection(), java.sql.Driver.connect() or the MySQL implementations of the javax.sql.DataSource setURL() method. If you specify a configuration property in the URL without providing a value for it, nothing will be set; for example, adding useServerPrepStmts alone to the URL does not make Connector/J use server-side prepared statements; you need to add useServerPrepStmts=true.

参考文档

http://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html
http://dev.mysql.com/doc/connector-j/5.1/en/connector-j-multi-host-connections.html
http://blog.sina.com.cn/s/blog_68af3f090101b75x.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值