元数据的使用

元数据的使用

package bull04.Metadata;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;

import org.junit.Test;

import bull04.C3P0.C3P0JDBCUtils;



/**
 * 元数据的使用
 * @author 45度炸
 *
 */
public class MetadataDemo1 {
    @Test
    /**
     * 数据库元数据
     */
    public void demo1() {
        Connection conn = null;
        PreparedStatement psmt = null;
        ResultSet rs = null;
        conn = C3P0JDBCUtils.getConnection();
        //获得数据库元数据
        try {
            DatabaseMetaData metaData = conn.getMetaData();
            System.out.println("驱动名称:" + metaData.getDriverName());
            System.out.println("url:" + metaData.getURL());
            System.out.println("用户名:" + metaData.getUserName());

            //获得表中的主键
            rs = metaData.getPrimaryKeys(null, null, "category");
            while(rs.next()) {
                //主键
                String keyName = rs.getString("COLUMN_NAME");
                System.out.println(keyName);//主键名称
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            C3P0JDBCUtils.closeResource(rs, psmt, conn);
        }
    }

    @Test
    /**
     * 参数元数据
     */
    public void demo2() {
        Connection conn = null;
        PreparedStatement psmt = null;
        ResultSet rs = null;
        try {
            conn = C3P0JDBCUtils.getConnection();
            String sql = "updata category set cname = ? where cid = ?";
            psmt = conn.prepareStatement(sql);
            //获取参数个数
            ParameterMetaData meteData = psmt.getParameterMetaData();
            int count = meteData.getParameterCount();
            System.out.println(count);//参数个数
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            C3P0JDBCUtils.getConnection();
        }
    }

    @Test
    /**
     * 结果元数据
     */
    public void demo3() {
        Connection conn = null;
        PreparedStatement psmt = null;
        ResultSet rs = null;

        try {
            conn = C3P0JDBCUtils.getConnection();
            String sql = "select * from category";
            psmt = conn.prepareStatement(sql);
            rs = psmt.executeQuery();

            ResultSetMetaData metaData = rs.getMetaData();
            int count = metaData.getColumnCount();
            for(int i = 1;i <= count;i++) {
                String DBname = metaData.getCatalogName(i);//mydb数据库名称
                String name = metaData.getColumnName(i);//属性名称
                String type = metaData.getColumnTypeName(i);//数据类型
                System.out.println(DBname+"---"+name+"---"+type);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            C3P0JDBCUtils.closeResource(rs, psmt, conn);
        }
    }
}

元数据案例

package bull04.Metadata;
/**
 * 元数据案例
 * 传参实现增删改
 */
import java.sql.Connection;
import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.junit.Test;

import bull04.C3P0.C3P0JDBCUtils;

public class MyDBUtils {
    public static void main(String[] args) {
        //MyDBUtils.update("update category set cname = ? where cid = ?", "服装",1);
        //MyDBUtils.update("delete from category where cid = ?", 5);
        MyDBUtils.update("insert into category value(?,?)", 4,"箱包");

    }

    public static void update(String sql,Object... obj) {
        Connection conn = null;
        PreparedStatement psmt = null;
        ResultSet rs = null;
        try {
            conn = C3P0JDBCUtils.getConnection();
            psmt = conn.prepareStatement(sql);
            ParameterMetaData metaData = psmt.getParameterMetaData();
            for(int i = 1;i <= metaData.getParameterCount();i++) {
                psmt.setObject(i, obj[i-1]);
            }
            int i = psmt.executeUpdate();
            if(i > 0) {
                System.out.println("操作成功!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            C3P0JDBCUtils.closeResource(rs, psmt, conn);
        }
    }
}
阅读更多
版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/sinat_40662281/article/details/80322762
文章标签: java
个人分类: JavaWeb
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭