概述
- 贴代码
import java.io.PrintStream; import java.sql.*; import java.util.Properties; 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)); } }
- java打jar包
方法步骤
(1)用命令行进入到该目录下,编译这个文件
javac ZJdbcPing.java
(2)将编译后的ZJdbcPing.class文件打成jar包
jar -cvf ZJdbcPing.jar ZJdbcPing.class
c表示要创建一个新的jar包,v表示创建的过程中在控制台输出创建过程的一些信息,f表示给生成的jar包命名
(3) java -jar ZJdbcPing.jar 这时会报如下错误 ZJdbcPing.jar中没有主清单属性
添加Main-Class属性
用压缩软件打开ZJdbcPing.jar,会发现里面多了一个META-INF文件夹,里面有一个MENIFEST.MF的文件,用记事本打开
1 Manifest-Version: 1.0 2 Created-By: 1.8.0_121 (Oracle Corporation) 3
在第三行的位置写入 Main-Class: ZJdbcPing(注意冒号后面有一个空格,整个文件最后有一行空行),保存
具体运行时,还注意得用java命令来运行,并指定加载驱动 jar. 例如——
java -Xbootclasspath/a:"ojdbc6-11.2.0.3.jar" -jar ZJdbcPing.jar oracle.jdbc.driver.OracleDriver "jdbc:oracle:thin:@10.10.10.245:1521:orcl" yjkj 123456
pause
运行结果
转载于:https://www.cnblogs.com/joyny/p/11176643.html
最后
以上就是明理指甲油为你收集整理的JDBC数据库连接测试工具的全部内容,希望文章能够帮你解决JDBC数据库连接测试工具所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复