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
----------------------------------------------------
*/
Derby 连接
最新推荐文章于 2023-06-20 21:47:19 发布