Javaweb07-html Notes (iv)

Source: Internet
Author: User
Tags html notes

1.1 Case Three: manual extraction of a Dbutils tool class: 1.1.1 Requirements:
A lot of the code is similar every time JDBC curd is performed. You can not extract the tool class. Complete some common code?
1.1.2 Analysis: 1.1.2.1 Technical Analysis:
"JDBC Meta-data Matadata" (understanding)-write code that is more versatile.

? DatabaseMetaData:获得数据库连接的信息,获得数据库的表的信息.* 获得数据库元数据:Connection中getMetaData();? ParameterMetaData:获得SQL中的参数的个数及类型.* 获得参数元数据:PreparedStatement中getParameterMetaData()? ResultSetMetaData:获得结果集中的列名及列的类型.* 获得结果集元数据:ResultSet中getMeta()

"Use of meta data"

@Test/** * Database meta data */public void Demo1 () {Connection conn = Null;conn = Jdbcutils2.getconnection ();//Get Database metadata: try {database MetaData MetaData = Conn.getmetadata (); System.out.println ("Get Driver Name:" +metadata.getdrivername ()); System.out.println ("Get Driver URL:" +metadata.geturl ()); System.out.println ("Get User name:" +metadata.getusername ());//Get primary key in table: 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 ();}} @Test/** * parameter metadata: */public void Demo2 () {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) {}} @Test/** * result set meta data: */public void Demo3 () {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) {}}

Javaweb07-html Notes (iv)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.