Derby 连接

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLWarning;

public class DerbyConnect {
	private static final String driver = "org.apache.derby.jdbc.EmbeddedDriver";
	private static final String url = "jdbc:derby:test;create=true";
	static void printSQLException(SQLException se){
		while(se != null){
			System.out.print("SQLException:State:" + se.getSQLState());//获取此 SQLException 对象的 SQLState。
			System.out.print("Severity:" + se.getErrorCode());//Severity:严重的  errorcode:供应商错误代码
			System.out.println(se.getMessage());
			se = se.getNextException();//通过 setNextException(SQLException ex) 获取链接到此 SQLException 对象的异常。
		}
	}
	static void printSQLWarning(SQLWarning sw){
		while(sw != null){
			System.out.print("SQLWarning:State: " + sw.getSQLState());
			System.out.print(";Severity:" + sw.getErrorCode());
			System.out.println(sw.getMessage());
			sw = sw.getNextWarning();
		}
	}
	public static void main(String[] args){
		Connection con = null;
		DatabaseMetaData dbmd = null;
		try{
			Class.forName(driver);
			con = DriverManager.getConnection(url);
			SQLWarning swarn = con.getWarnings();
			if(swarn != null){
				printSQLWarning(swarn);
			}
			dbmd = con.getMetaData();
			System.out.println("\n----------------------------------------------------");
			System.out.println("数据库产品名称:" + dbmd.getDatabaseProductName());
			System.out.println("数据库版本::" + dbmd.getDatabaseProductVersion());
			System.out.println("驱动程序的名称:" + dbmd.getDriverName());
			System.out.println("驱动程序的版本::" + dbmd.getDriverVersion());
			System.out.println("数据库的地址::" + dbmd.getURL());
			System.out.println("\n----------------------------------------------------");
		}catch(SQLException se){
			printSQLException(se);
		}catch(ClassNotFoundException ce){
			System.out.println("JDBC Driver" + driver + "not found in ClassPath");
		}finally{
			if(con != null){
				try{
					con.close();
				}catch(SQLException se){
					printSQLException(se);
				}
			}
		}
	}
}
/*
 * 
----------------------------------------------------
数据库产品名称:Apache Derby
数据库版本::10.4.2.1 - (706043)
驱动程序的名称:Apache Derby Embedded JDBC Driver
驱动程序的版本::10.4.2.1 - (706043)
数据库的地址::jdbc:derby:test

----------------------------------------------------
 */


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值