java getmetadata_JDBC 元数据信息 getMetaData()

数据库元数据信息:

import java.sql.DatabaseMetaData;

import java.sql.DriverManager;

import java.sql.SQLException;

/**

*

* 2008-12-7

*

* @author liyong

*

*/

public class DBMD {

/**

* @param args

* @throws SQLException

* @throws ClassNotFoundException

*/

public static void main(String[] args) throws SQLException, ClassNotFoundException {

Class.forName("com.mysql.jdbc.Driver");

String url = "jdbc:mysql://localhost:3306/jdbc";

String user = "root";

String password = "";

java.sql.Connection conn = DriverManager.getConnection(url, user, password);

DatabaseMetaData dbmd = conn.getMetaData();//获取数据源的信息,可以用来判断数据源是否支持某些功能,比如事务、批处理等。全在dbmd.support中

System.out.println("db name: " + dbmd.getDatabaseProductName());

System.out.println("tx: " + dbmd.supportsTransactions());//判断是否支持事务

conn.close();

}

}

参数的元数据信息

public class ParameterMetaTest {

/**

* @param args

* @throws SQLException

*/

public static void main(String[] args) throws SQLException {

Object[] params = new Object[] { "lisi", 100f };

read("select * from user where name=? and  money > ?", params);

}

static void read(String sql, Object[] params) throws SQLException {

Connection conn = null;

PreparedStatement ps = null;

ResultSet rs = null;

try {

conn = JdbcUtils.getConnection();

ps = conn.prepareStatement(sql);

//            ParameterMetaData pmd = ps.getParameterMetaData();

//           int count = pmd.getParameterCount();//可以获取参数个数            for (int i = 1; i <= params.length; i++) {

// System.out.print(pmd.getParameterClassName(i) + "\t");  类名

// System.out.print(pmd.getParameterType(i) + "\t");  类型名

// System.out.println(pmd.getParameterTypeName(i));  在数据库中的类型名

//以上这三条不太实用,因为有的数据库引擎是不支持的。

ps.setObject(i, params[i - 1]);

}

rs = ps.executeQuery();

while (rs.next()) {

System.out.println(rs.getInt("id") + "\t"

+ rs.getString("name") + "\t" + rs.getDate("birthday")

+ "\t" + rs.getFloat("money"));

}

} finally {

JdbcUtils.free(rs, ps, conn);

}

}

}

结果集的元数据信息

public class ResultSetMetaDataTest {

/**

* @param args

* @throws SQLException

*/

public static void main(String[] args) throws SQLException {

List> datas = read("select id, name as n from user where id < 5");    //n是别名 name是表中列本来的的名字

System.out.println(datas);

}

static List> read(String sql) throws SQLException {

Connection conn = null;

PreparedStatement ps = null;

ResultSet rs = null;

try {

conn = JdbcUtils.getConnection();

ps = conn.prepareStatement(sql);

rs = ps.executeQuery();

ResultSetMetaData rsmd = rs.getMetaData();

int count = rsmd.getColumnCount();//获取列数            String[] colNames = new String[count];

for (int i = 1; i <= count; i++) {

// System.out.print(rsmd.getColumnClassName(i) + "\t"); //获取数据类型

// System.out.print(rsmd.getColumnName(i) + "\t"); //获取列本来的名字

// System.out.println(rsmd.getColumnLabel(i));

colNames[i - 1] = rsmd.getColumnLabel(i);//获取列的别名,如果没有别名就是列的本名            }

List> datas = new ArrayList>();

while (rs.next()) {

Map data = new HashMap();

for (int i = 0; i < colNames.length; i++) {

data.put(colNames[i], rs.getObject(colNames[i]));

}

datas.add(data);

}

return datas;

} finally {

JdbcUtils.free(rs, ps, conn);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值