(1)DatabaseMetaData:
package javase.jdbc; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.ResultSet; import java.sql.SQLException; public class DatabaseMetaDataDemo { public static void main(String[] args) { Connection connection = JDBCConnection.getConnection(); try { DatabaseMetaData dMetaData = connection.getMetaData(); // 实例化元数据 System.out.println("数据库名:" + dMetaData.getDatabaseProductName()); System.out.println("数据库版本: " + dMetaData.getDatabaseProductVersion()); ResultSet resultSet = dMetaData.getPrimaryKeys(null, null, "users"); while (resultSet.next()) { System.out.println("表类别:" + resultSet.getString(1)); System.out.println("表模式:" + resultSet.getString(2)); System.out.println("表名称:" + resultSet.getString(3)); System.out.println("列名称:" + resultSet.getString(4)); System.out.println("主键:" + resultSet.getString(5)); System.out.println("主键名称:" + resultSet.getString(6)); } } catch (SQLException e) { e.printStackTrace(); } } }
输出:
数据库名:MySQL 数据库版本: 5.5.19 表类别:jdbcdemo 表模式:null 表名称:users 列名称:id 主键:1 主键名称:PRIMARY
(2)ResultSetMetaData:
package javase.jdbc; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSetMetaData; import java.sql.SQLException; public class ResultSetMetaDataDemo { public static void main(String[] args) { String sql = "SELECT id,username,password,classId FROM users"; Connection connection = JDBCConnection.getConnection(); try { PreparedStatement pStatement = connection.prepareStatement(sql); ResultSetMetaData rSetMetaData = pStatement.getMetaData(); System.out.println("返回数据有" + rSetMetaData.getColumnCount() + "个字段"); if (rSetMetaData.isAutoIncrement(1)) System.out.println(rSetMetaData.getColumnName(1) + "字段是自增的"); } catch (SQLException e) { e.printStackTrace(); } } }
输出:
返回数据有4个字段 id字段是自增的
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。