zyl910

优化技巧、硬件体系、图像处理、图形学、游戏编程、国际化与文本信息处理。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

作者: zyl910

一、缘由

当数据库服务器很多时,或者要与第三方公司做数据库表交换时,此时觉得若有一个简单快捷的数据库连接测试工具就好了。
因为若是采取直接把程序部署到tomcat等容器再测试的办法,那就太费功夫了。

其次,连上数据库后最好能显示一下数据库名称、版本等基础信息,这样便能判定所连的数据库是否正确。

二、用法

考虑到为了能一次性测试多个数据库连接。于是我将它设计成一个命令行工具。

它的命令参数格式为: zjdbcping <driverClassName> <url> <username> <password>

  • <driverClassName>: 驱动类的全名. 例如 oracle.jdbc.driver.OracleDriver
  • <url>: JDBC连接串. 因为有可能会包含特殊资费,故建议用双引号括起来,例如 "jdbc:oracle:thin:@192.168.3.33:1521:zjkf"
  • <username>: 数据库账号.
  • <password>: 数据库密码.

具体运行时,还注意得用java命令来运行,并指定加载驱动 jar. 例如——

java -Xbootclasspath/a:"ojdbc6.jar" -jar zjdbcping.jar oracle.jdbc.driver.OracleDriver "jdbc:oracle:thin:@192.168.3.33:1521:zjkf" crjapp crjapp

运行结果如下——

zjdbcping <driverClassName> <url> <username> <password>

getCatalog:	null
getClientInfo:
-- listing properties --
getMetaData:
	getDatabaseMajorVersion:	11
	getDatabaseMinorVersion:	2
	getDatabaseProductName:	Oracle
	getDatabaseProductVersion:	Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
	getDefaultTransactionIsolation:	2
	getDriverMajorVersion:	11
	getDriverMinorVersion:	2
	getDriverName:	Oracle JDBC driver
	getDriverVersion:	11.2.0.1.0
	getJDBCMajorVersion:	11
	getJDBCMinorVersion:	2
	getURL:	jdbc:oracle:thin:@192.168.3.33:1521:zjkf
	getUserName:	CRJAPP
	getClientInfoProperties:
		#getColumnCount:	4
		NAME	MAX_LEN	DEFAULT_VALUE	DESCRIPTION
		#RowCount:	0

一般情况下,写个bat(或 sh)脚本来测试会比较方便,例如 “zjdbcping.bat” ——

java -Xbootclasspath/a:"ojdbc6.jar" -jar zjdbcping.jar oracle.jdbc.driver.OracleDriver "jdbc:oracle:thin:@192.168.3.33:1521:zjkf" crjapp crjapp
pause

三、源码

ZJdbcPing.java ——

package org.zyl910.zjdbcping;

import java.io.PrintStream;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Properties;

/** 测试Jdbc连接.
 * 
 * @author zhouyuelin
 *
 */
public class ZJdbcPing {
	public static void main(String[] args) {
		PrintStream outs = System.out;
		// title.
		outs.println("zjdbcping <driverClassName> <url> <username> <password>");
		// oracle.jdbc.driver.OracleDriver "jdbc:oracle:thin:@192.168.3.33:1521:zjkf" crjapp crjapp
		outs.println();
		// args.
		String driverClassName="";
		String url="";
		String username="";
		String password="";
		if (args.length>0) driverClassName=args[0];
		if (args.length>1) url=args[1];
		if (args.length>2) username=args[2];
		if (args.length>3) password=args[3];
		// connect.
		Connection conn = null;
		try {
			Class.forName(driverClassName);
			conn = DriverManager.getConnection(url, username, password);
		}
		catch(Throwable ex) {
			ex.printStackTrace(outs);
		}
		if (null==conn) return;
		// info.
		try {
			outs.println(String.format("getCatalog:\t%s", conn.getCatalog()));
			outs.println("getClientInfo:");
			Properties properties = conn.getClientInfo();
			properties.list(outs);
			//outs.println("getTypeMap:");
			//Map<String,Class<?>> typeMap = conn.getTypeMap();
			//for(String key: typeMap.keySet()) {
			//	Class<?> c = typeMap.get(key);
			//	outs.println(String.format("\t%s:\t%s", key, c.toString()));
			//}
			outs.println("getMetaData:");
			DatabaseMetaData databaseMetaData = conn.getMetaData();
			printDatabaseMetaData(outs, databaseMetaData, "\t");
		}
		catch(Throwable ex) {
			ex.printStackTrace(outs);
		}
		finally {
			try {
				conn.close();
			}
			catch(Throwable ex) {
				ex.printStackTrace(outs);
			}
		}
		return;
	}

	private static void printDatabaseMetaData(PrintStream outs,
			DatabaseMetaData dbmd, String prefix) throws Exception {
		if (null==outs) return;
		if (null==dbmd) return;
		outs.println(prefix+String.format("getDatabaseMajorVersion:\t%d", dbmd.getDatabaseMajorVersion()));
		outs.println(prefix+String.format("getDatabaseMinorVersion:\t%d", dbmd.getDatabaseMinorVersion()));
		outs.println(prefix+String.format("getDatabaseProductName:\t%s", dbmd.getDatabaseProductName()));
		outs.println(prefix+String.format("getDatabaseProductVersion:\t%s", dbmd.getDatabaseProductVersion()));
		outs.println(prefix+String.format("getDefaultTransactionIsolation:\t%d", dbmd.getDefaultTransactionIsolation()));
		outs.println(prefix+String.format("getDriverMajorVersion:\t%d", dbmd.getDriverMajorVersion()));
		outs.println(prefix+String.format("getDriverMinorVersion:\t%d", dbmd.getDriverMinorVersion()));
		outs.println(prefix+String.format("getDriverName:\t%s", dbmd.getDriverName()));
		outs.println(prefix+String.format("getDriverVersion:\t%s", dbmd.getDriverVersion()));
		outs.println(prefix+String.format("getJDBCMajorVersion:\t%d", dbmd.getJDBCMajorVersion()));
		outs.println(prefix+String.format("getJDBCMinorVersion:\t%d", dbmd.getJDBCMinorVersion()));
		outs.println(prefix+String.format("getURL:\t%s", dbmd.getURL()));
		outs.println(prefix+String.format("getUserName:\t%s", dbmd.getUserName()));
		ResultSet rs = dbmd.getClientInfoProperties();
		outs.println(prefix+"getClientInfoProperties:");
		printResultSet(outs, rs, prefix+"\t");
		rs.close();
	}

	private static void printResultSet(PrintStream outs, ResultSet rs,
			String prefix) throws Exception {
		if (null==outs) return;
		if (null==rs) return;
		ResultSetMetaData rsmd = rs.getMetaData();
		int cols = rsmd.getColumnCount();
		outs.println(prefix+String.format("#getColumnCount:\t%d", cols));
		// col.
		if (true) {
			outs.print(prefix);
			for(int i=1; i<=cols; ++i) {
				if (i>1) outs.print('\t');
				String str = rsmd.getColumnName(i);
				outs.print(str);
			}
			outs.println();
		}
		// rows.
		int rows = 0;
		while(rs.next()) {
			++rows;
			outs.print(prefix);
			for(int i=1; i<=cols; ++i) {
				if (i>1) outs.print('\t');
				Object o = null;
				String str = null;
				try {
					o = rs.getObject(i);
					if (null!=o)
						str = o.toString();
				}
				catch(Exception ex) {
					str = String.format("(%s)", ex.getMessage());
				}
				outs.print(str);
			}
			outs.println();
		}
		outs.println(prefix+String.format("#RowCount:\t%d", rows));
	}

}

下载地址——
https://git.coding.net/zyl910/zjdbcping.git

posted on 2017-03-16 17:16  zyl910  阅读(4276)  评论(1编辑  收藏  举报