时间:2021-07-01 10:21:17 帮助过:19人阅读
2.JDBC的API
connection
statement
3.JDBC的元数据MataDate(提高通用性较高的代码)
* 获得数据库元数据:Connection中getMetaData();
* 获得参数元数据:PreparedStatement中getParameterMetaData()
* 获得结果集元数据:ResultSet中getMeta()
4.元数据的使用
public void demo2(){ Connection conn = null; conn = JDBCUtils2.getConnection(); // 获得数据库元数据: try { DatabaseMetaData metaData = conn.getMetaData(); System.out.println("获得驱动名称:"+metaData.getDriverName()); System.out.println("获得驱动URL:"+metaData.getURL()); System.out.println("获得用户名:"+metaData.getUserName()); // 获得表中的主键: ResultSet rs = metaData.getPrimaryKeys(null, null, "category"); if(rs.next()){ String name = rs.getString("COLUMN_NAME"); System.out.println(name); } } catch (SQLException e) { e.printStackTrace(); } }
参数元数据
public void demo3(){ Connection conn = null; PreparedStatement stmt = null; try{ conn = JDBCUtils2.getConnection(); String sql = "update category set cname = ? where cid = ?"; stmt = conn.prepareStatement(sql); ParameterMetaData metaData = stmt.getParameterMetaData(); int count = metaData.getParameterCount(); System.out.println(count); }catch(Exception e){ } }
结果集元数据
public void demo4(){ Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; try{ conn = JDBCUtils2.getConnection(); String sql = "select * from category"; stmt = conn.prepareStatement(sql); rs = stmt.executeQuery(); ResultSetMetaData metaData = rs.getMetaData(); int count = metaData.getColumnCount(); for(int i = 1;i<=count ;i++){ String name = metaData.getColumnName(i); String type = metaData.getColumnTypeName(i); System.out.println(name+type); } }catch(Exception e){ } }
JDBC1
标签:cep div 使用 code 类型 技术 sun公司 res use