JDBC连接和释放连接的工具类:
public class JDBCCoonection {
private final static String DRIVE = "oracle.jdbc.OracleDriver";
private final static String URL ="jdbc:oracle:thin:@10.211.55.3:1521:orcl";
private final static String USER = "lee";
private final static String PASSWORD="lee";
private static Connection connection = null;
private final static PreparedStatement pst = null;
private final static Statement statement = null;
private final static ResultSet rs = null;
//获取connection连接
public static Connection getConnection() {
try {
Class.forName(DRIVE);//加载驱动
try {
connection = DriverManager.getConnection(URL, USER, PASSWORD);//获取连接
} catch (SQLException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return connection;
}
//关闭连接connection
public static void disConnection() throws SQLException {
if (rs!=null) {
rs.close();
}
if (pst !=null) {
pst.close();
}
if (statement !=null) {
statement.close();
}
if (connection != null) {
connection.close();
}
}
}
JDBC的元数据分为三种:
数据库元数据(DataBaseMetaData)
参数元数据(ParameterMetaData)
结果集元数据(ResultSetMetaData)
4.1 数据库元数据
数据库元数据(DatabaseMetaData)主要用于:获取数据库及主键的相关信息。
DataBaseMetaData接口的常见方法如下:
方法 | 简介 |
---|---|
String getDatabaseProductName() throws SQLException | 获取数据库名 |
String getDatabaseProductVersion() throws SQLException | 获取数据库版本信息 |
String getDriverName() throws SQLException | 获取驱动名 |
String getURL() throws SQLException | 获取URL |
String getUserName() throws SQLException | 获取用户名 |
ResultSet getPrimaryKeys(String catalog, String schema, String table) throws SQLException; | 获取指定表的主键信息。参数:catalog:类别信息,通常设为null;schema:①如果是oracle数据库:大写的用户名;②如果是其它数据库:数据库名。也可以设为null。 table:指定大写的表名。 返回值是一个ResultSet,该结果集包含以下信息:1表类别 2表模式 3表名称 4列名称 5主键中的序列号(值1表示主键中的第一列,值2表示主键中的第二列,…) 6主键的名称 |
DataBaseMetaData.java
public class DataBaseMetaData {
public static void dateBaseMetaDataDemo() throws SQLException {
Connection connection = JDBCCoonection.getConnection();//获取连接
DatabaseMetaData metaData = connection.getMetaData();//获取DatabaseMetaData
String productName = metaData.getDatabaseProductName();//获取数据库名称
String productVersion = metaData.getDatabaseProductVersion();//获取数据库版本
String driverName = metaData.getDriverName();//获取驱动名字
String url = metaData.getURL();//获取URL
String userName = metaData.getUserName();//获取用户名
ResultSet rs = metaData.getPrimaryKeys(null, userName, "STUDENT");
System.out.println("数据库名称:"+productName);
System.out.println("数据库版本:"+productVersion);
System.out.println("驱动名称:"+driverName);
System.out.println("URL:"+url);
System.out.println("用户名:"+userName);
while(rs.next()) {
Object tablename = rs.getObject(3);//3:表名
System.out.println(tablename);
Object primaryKeyColumnName = rs.getObject(4);//4:主键列名
System.out.println(primaryKeyColumnName);
}
}
public static void main(String[] args) throws SQLException {
dateBaseMetaDataDemo();
}
}
运行结果:
4.2 参数元数据
参数元数据(ParameterMetaData)主要用于:获取SQL语句中占位符的相关信息。
ParameterMetaData接口的常见方法如下:
方法 | 简介 |
---|---|
int getParameterCount() throws SQLException; | 获取SQL语句中,参数占位符的个数 |
String getParameterTypeName(int param) throws SQLException; | 获取第param个参数的类型名 |
注意:
很多数据库对ParameterMetaData的支持不是很完善。例如,使用ParameterMetaData前:
①Oralce目前必须使用ojdbc7.jar作为驱动包;
②MySql必须在url中附加参数配置: jdbc:mysql://localhost:3306/数据库名?generateSimpleParameterMetadata=true
ParameterMetaData.java
public class ParameterMetaData {
public static void parameterMetaDataDemo() throws SQLException {
Connection connection = JDBCCoonection.getConnection();//获取连接
String sql = "select * from student where id=? and name = ?";
PreparedStatement pstmt = connection.prepareStatement(sql );
java.sql.ParameterMetaData parameterMetaData = pstmt.getParameterMetaData() ;
int parameterCount = parameterMetaData.getParameterCount();//获取占位符个数
System.out.println("占位符一共有:" + parameterCount);
/**
* 注意:遍历的时候一定要从1开始,从0开始会报错无效的索引列
*/
for(int i= 1; i<=parameterCount ; i++) {
String parameterTypeName = parameterMetaData.getParameterTypeName(i);
System.out.println("第"+i+"个参数的类型是:"+parameterTypeName);
}
}
public static void main(String[] args) throws SQLException {
parameterMetaDataDemo();
}
}
运行结果:
4.3 结果集元数据
结果集元数据(ResultSetMetaData)主要用于:获取SQL语句中占位符的相关信息。
ResultSetMetaData接口的常见方法如下:
方法 | 简介 |
---|---|
int getColumnCount() throws SQLException; | 获取结果集中,包含列的数量 |
String getColumnName(int column) throws SQLException; | 获取结果集中,第column列的名称 |
String getColumnTypeName(int column) throws SQLException; | 获取结果集中,第column列的类型 |
ResultMetaData.java
public class ResultMetaData {
public static void resultMetaDataDemo() throws SQLException {
Connection connection = JDBCCoonection.getConnection();//获取连接
ResultSet rs = connection.createStatement().executeQuery("select * from student");
ResultSetMetaData metaData = rs.getMetaData();//获取结果集元数据ResultSetMetaData对象
int columnCount = metaData.getColumnCount();//获取列的数量
for(int i=1;i<=columnCount;i++){
String columnTypeName = metaData.getColumnTypeName(i) ;//获取列类型的名字
String columnName = metaData.getColumnName(i) ;//获取列名
System.out.println("第"+i+"列的类型是:"+columnTypeName+",名称是:"+columnName);
}
System.out.println();
//显示整个student表的数据
while(rs.next()){//获取每一行
//获取每一列
for(int i=1;i<=columnCount;i++){
System.out.print(rs.getObject(i)+"\t");
}
System.out.println();
}
}
public static void main(String[] args) throws SQLException {
resultMetaDataDemo();
}
}
运行结果: