Java-Jdbc-驱动的所有设置参数配置说明

161 阅读2分钟

背景

最近使用有比较多的接触jdbc,我们知道在jdbc连接串可以通过设置参数来开启不同的功能,比如超时设置,多语句支持,SQL日志等。不过对于这些参数的资料,大部分比较零散。最近在使用中,发现官方在除了官网的文档,也提供了非常完备的参数说明,具体的实现在com.mysql.jdbc.ConnectionPropertiesImpl里。

实现

在jdbc驱动的ConnectionPropertiesImpl中,有个exposeAsXml方法。可以将所有的参数以xml的形式输出。因为xml不太好读,基于此所以将xml转换成csv格式,这样方便查询。获取方式如下。

jdbc 驱动是5.1.45版本的。

用来生成xml类型的文档

/**
 * Creates docbook table of connection properties from ConnectionProperties class.
 */
public class PropertiesDocGenerator extends ConnectionPropertiesImpl {

    static final long serialVersionUID = -4869689139143855383L;

    public static void main(String[] args) throws SQLException {
        System.out.println(new PropertiesDocGenerator().exposeAsXml());
    }
}

将文档转换成csv

需要commons-io和SAX

    public static void main(String[] args) throws IOException {
            public static void main(String[] args) throws IOException {
        String file = "properties.xml";
        parseXml(file);
    }
    }

 public static void parseXml(String fileName) {
        SAXBuilder builder = new SAXBuilder();
        try {
            org.jdom2.Document document = builder.build(new File(fileName));
            org.jdom2.Element root = document.getRootElement();
            StringBuilder sb = new StringBuilder();
            for (org.jdom2.Element e : root.getChildren()) {
                String cate = e.getAttribute("name").getValue();
                for (org.jdom2.Element c : e.getChildren()) {
                    sb.append(cate).append("\t");
                    for (org.jdom2.Attribute attr : c.getAttributes()) {
                        sb.append(attr.getValue()).append("\t");
                    }
                    sb.append(c.getValue().replaceAll("\n", ""));
                    sb.append("\n");
                }
            }
            FileUtils.writeStringToFile(new File("jdbc-properties.csv"), sb.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

效果如下

Connection/Authentication	user	No		-2147483647	all versions	 The user to connect as 
Connection/Authentication	password	No		-2147483646	all versions	 The password to use when connecting 
Connection/Authentication	socketFactory	No	com.mysql.jdbc.StandardSocketFactory	4	3.0.3	 The name of the class that the driver should use for creating socket connections to the server. This class must implement the interface 'com.mysql.jdbc.SocketFactory' and have public no-args constructor. 
Connection/Authentication	connectTimeout	No	0	9	3.0.1	 Timeout for socket connect (in milliseconds), with 0 being no timeout. Only works on JDK-1.4 or newer. Defaults to '0'. 
Connection/Authentication	socketTimeout	No	0	10	3.0.1	 Timeout (in milliseconds) on network socket operations (0, the default means no timeout). 
...

获取地址

地址放到了github上:java-jdbc-connector参数和说明 合计208个参数

参考资料

  1. connector-j-reference-configuration-properties