http://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html
jdbc: MySQL://(主机)[failoverhost…][:港口]/[数据库],
[? propertyName1] [=propertyValue1][和propertyName2] [=propertyValue2]…
jdbc: mysql://(主持人:端口),(主持人:端口)…/(数据库),
[? propertyName1] [=propertyValue1][和propertyName2] [=propertyValue2]…
实例:
jdbc: mysql://localhost: 3306/sakila吗?profileSQL=true
package java_data_jdbc;
现只列举几个重要的参数,如下表所示:
参数名称参数说明缺省值最低版本要求用户数据库用户名(用于连接数据库)所有版本密码用户密码(用于连接数据库)
所有版本useUnicode是否使用Unicode字符集,如果参数characterEncoding设置为gb2312或gbk,本参数值必须设置为truefalse1.1gcharacterEncoding当useUnicode设置为真时,指定字符编码。比如可设置为gb2312或gbkfalse1.1gautoReconnect当数据库连接异常中断时,是否自动重新连接? false1.1autoReconnectForPools是否使用针对数据库连接池的重连策略false3.1.3failOverReadOnly自动重连成功后,连接是否设置为只读? true3.0.12maxReconnectsautoReconnect设置为真时,重试连接的次数31.1 initialtimeoutautoreconnect设置为真时,两次重连之间的时间间隔,单位:21.1秒connecttimeout和数据库服务器建立插座连接时的超时,单位:毫秒。0表示永不超时,适用于JDK 1.4及更高版本03.0.1socketTimeoutsocket操作(读,写)超时,单位:毫秒。0表示永不超时03.0.1
,
对应中文环境,通常mysql连接URL可以设置为:
jdbc: mysql://localhost: 3306/测试?用户=root&密码=,useUnicode=true& characterEncoding=gbk& autoReconnect=true& failOverReadOnly=false
在使用数据库连接池的情况下,最好设置如下两个参数:
autoReconnect=true& failOverReadOnly=false
需要注意的是,在xml配置文件中,URL中的,符号需要转义成,比。如在tomcat的服务器。xml中配置数据库连接池时,mysql jdbc url样例如下:
jdbc: mysql://localhost: 3306/测试?用户=root&音箱;密码=,amp; useUnicode=true&音箱;characterEncoding=gbk
, amp; autoReconnect=true&音箱;failOverReadOnly=false
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class  Jdbc_01 { public static  void main (String [], args), { String userName =,“根”; String password =,“根”;/* ,*这个url的格式后面是可以带很多参数的,详细请参考mysql官网http://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html ,*/String url =, " jdbc: mysql://localhost: 3306 ? characterEncoding=utf-8& useSSL=true”; String sql1 =, " Select *,得到阶级”; String sql =, " INSERT INTO  Class (姓名、年龄),VALUES (“你”好,“21”)”; try {/* ,*这个驱动的写法有两种,两者是继承关系,还有是:org.gjt.mm.mysql.Driver ,*/forname(“com.mysql.jdbc.Driver”); Connection conn =, DriverManager.getConnection (url,用户名,密码); Statement stmt =, conn.createStatement (); stmt.execute (“use 学生;”); stmt.execute (sql); ResultSet res =, stmt.executeQuery (sql1); 而(res.next ()) { String id =, res.getString (" id "); String name =, res.getString(“名字”); int age =, res.getInt(“年龄”); System.out.println(“序号:,“+ id +,“,,,”+“姓名:,“+,name +,“”才能+,年龄+“岁!”); } },catch (Exception e), { e.printStackTrace (); } } }
部分资料http://elf8848.iteye.com/blog/1684414