元数据
在jdbc中获取数据库的定义,例如:数据库、表、列的定义信息。就用到元数据。
数据库元数据、参数元数据、结果集元数据
.MetaData()
1.数据库元数据
package com.cn.metadata;import java.sql.Connection;import java.sql.DatabaseMetaData;import org.junit.Test;import com.cn.util.JdbcUtil;public class Demo1 { /** * 1.数据库元数据 * @throws Exception */ @Test public void test1() throws Exception { //获取连接 Connection conn = JdbcUtil.getConnection(); //获取数据库元数据 DatabaseMetaData metaData = conn.getMetaData();//alt+shift+L 快速获取方法返回值 /** * 获取数据库元数据 */ System.out.println(metaData.getUserName()); System.out.println(metaData.getURL()); System.out.println(metaData.getDatabaseProductName()); System.out.println(metaData.getDatabaseProductVersion()); System.out.println(metaData.getDriverVersion()); }}
2.参数元数据
package com.cn.metadata;import java.sql.Connection;import java.sql.ParameterMetaData;import java.sql.PreparedStatement;import org.junit.Test;import com.cn.util.JdbcUtil;public class Demo1 { /** * 2.参数元数据 * @throws Exception */ @Test public void test2() throws Exception { //获取连接 Connection conn = JdbcUtil.getConnection(); //sql String sql = "SELECT * FROM dept where id=? and deptName=?"; PreparedStatement pstmt = conn.prepareStatement(sql); ParameterMetaData paraMetaData = pstmt.getParameterMetaData(); //获取参数的个数 int count = paraMetaData.getParameterCount(); System.out.println(count);//结果为2 }}
3.结果集元数据
package com.cn.metadata;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import org.junit.Test;import com.cn.util.JdbcUtil;public class Demo1 { /** * 3.结果集元数据 */ @Test public void test3() throws Exception { //获取连接 Connection conn = JdbcUtil.getConnection(); //sql String sql = "SELECT * FROM dept"; PreparedStatement pstmt = conn.prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); ResultSetMetaData resultMetaData = rs.getMetaData(); /** * 得到结果集元数据(目标:通过结果集元数据,得到列的名称) */ //迭代每一行结果 while(rs.next()){ //1.获取列的个数 int columnCount = resultMetaData.getColumnCount(); //2.遍历,获取每一列的值 for(int i=1; i<=columnCount; i++){ //得到每一列的列名称 String columnName = resultMetaData.getColumnName(i);//从1开始 //获取该行对应的列的值 Object columnValue = rs.getObject(columnName); System.out.print(columnName + "=" + columnValue + ", "); } System.out.println(); } }}
附上自定义的Jdbc工具类
package com.cn.util;import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Properties;/** * jdbc的工具类 * @author liuzhiyong * */public class JdbcUtil { private static String url = null; private static String user = null; private static String password = null; private static String driverClass = null; /** * 静态代码块(只调用一次) */ static{ try { //读取db.properties文件 Properties properties = new Properties(); /** * 使用类路径的读取方式 * / 斜杠表示classpath的根目录 * 在java项目下,classpath的根目录从bin目录开始 * 在web项目下,classpath的根目录从WEB-INF/classes目录开始 */ InputStream in = JdbcUtil.class.getResourceAsStream("/db.properties"); properties.load(in); //读取信息 url = properties.getProperty("url"); user = properties.getProperty("user"); password = properties.getProperty("password"); driverClass = properties.getProperty("driverClass"); Class.forName(driverClass); } catch (Exception e) { e.printStackTrace(); System.out.println("驱动程序注册出错!"); } } /** * 获取连接对象的方法 */ public static Connection getConnection(){ try { Connection conn = DriverManager.getConnection(url, user, password); return conn; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); throw new RuntimeException(e); } } /** * 释放资源的重载方法 */ public static void close(Connection conn, Statement stmt, ResultSet rs){ //关闭资源(顺序:后打开,先关闭) if(rs != null){ try { rs.close(); } catch (SQLException e) { System.out.println("ResultSet关闭失败!"); throw new RuntimeException(e); } }if(stmt != null){ try { stmt.close(); } catch (SQLException e) { System.out.println("Statement关闭失败!"); throw new RuntimeException(e); } } if(conn != null){ try { conn.close(); } catch (SQLException e) { System.out.println("Connection关闭失败!"); throw new RuntimeException(e); } } } /** * 释放资源的重载方法 */ public static void close(Connection conn, Statement stmt){ //关闭资源(顺序:后打开,先关闭) if(stmt != null){ try { stmt.close(); } catch (SQLException e) { System.out.println("Statement关闭失败!"); throw new RuntimeException(e); } } if(conn != null){ try { conn.close(); } catch (SQLException e) { System.out.println("Connection关闭失败!"); throw new RuntimeException(e); } } } }标签:java,Connection,sql,println,import,数据,conn From: https://blog.51cto.com/u_15769923/5775571