jdbc连接mysql各种情况总结

时间:2022-09-03 18:43:25

连接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