When you use JDBC to connect to a database query, the result that you typically return is a collection of key-value pairs for each row of data. At this point we need to know what fields are available for the queried data. The name of each field can be obtained by resultsetmetadata based on the resultset result set. The main use is Getcolumnlabel (int column) and getcolumnname (int column) two methods to obtain. The following are the meanings of the 3 nouns (taken from the JDK API 1.6.0 Chinese version)
ResultSetMetaData: An object used to obtain information about the ResultSet
type and attributes of a column in an object;
getColumnName (int column): Gets the name of the specified column.
Getcolumnlabel (int column): Gets the suggested title for the specified column for printing output and display.
Difference: getColumnName may only fetch the field name of the queried database table, not the alias used in the SQL statement, and Getcolumnlabel takes the name (field name or alias) specified in the SQL statement. Here are the differences between the MySQL database and the Oracle database when querying SQL
Mysql:
1 Importjava.sql.Connection;2 ImportJava.sql.DriverManager;3 Importjava.sql.PreparedStatement;4 ImportJava.sql.ResultSet;5 ImportJava.sql.ResultSetMetaData;6 Importjava.sql.SQLException;7 8 Public classTest9 {Ten Public Static voidMain (string[] args) One { AString dirver = "Com.mysql.jdbc.Driver";//Database-driven -String Dburl = "JDBC:MYSQL://127.0.0.1:3306/TEST?CHARACTERENCODING=GBK";//Database Address -String user = "TestUser";//User theString password = "TestUser";//Password -Connection conn =NULL;//Define Links -PreparedStatement pstmt =NULL;//Defining precompiled Commands -ResultSet rs =NULL;//defining a result set + Try - { +Class.forName (Dirver);//Load Driver Aconn = Drivermanager.getconnection (dburl, user, password);//Get Connections atString sql = "Select T.id as tid,t.name as tname,t.sex as tsex from T_user T";//Defining SQL Statements -pstmt = conn.preparestatement (sql);//Execute SQL -rs = Pstmt.executequery ();//Get result set - if(Rs.next ()) - { -ResultSetMetaData metaData = Rs.getmetadata ();//Get ResultSetMetaData in intCount = Metadata.getcolumncount ();//returns the number of columns in this ResultSet object. - //traverse the name of the field that takes out each query to for(inti = 0; I < count; i++) + { -System.out.println ("getColumnName obtained name:" + metadata.getcolumnname (i + 1). toLowerCase () + "" the+ "Getcolumnlabel obtained name:" + metadata.getcolumnlabel (i + 1). toLowerCase ()); * } $ }Panax Notoginseng } - Catch(Exception e) the { + e.printstacktrace (); A } the finally + { - $ Try $ { - if(rs! =NULL ) - { the rs.close (); - }Wuyi if(Pstmt! =NULL ) the { - pstmt.close (); Wu } - if(Conn! =NULL ) About { $ conn.close (); - } - } - Catch(SQLException e) A { + //TODO auto-generated Catch block the e.printstacktrace (); - } $ the } the } the } the Results: - getColumnName obtained name: ID Getcolumnlabel obtained name: Tid in name obtained by getColumnName: name Getcolumnlabel: Tname thegetColumnName acquired name: Sex Getcolumnlabel name obtained: Tsex
Oracle
1 Importjava.sql.Connection;2 ImportJava.sql.DriverManager;3 Importjava.sql.PreparedStatement;4 ImportJava.sql.ResultSet;5 ImportJava.sql.ResultSetMetaData;6 Importjava.sql.SQLException;7 8 Public classTest9 {Ten Public Static voidMain (string[] args) One { AString dirver = "Oracle.jdbc.driver.OracleDriver";//Database-driven -String Dburl = "JDBC:ORACLE:THIN:@127.0.0.1:1521:ORCL";//Database Address -String user = "TestUser";//User theString password = "TestUser";//Password -Connection conn =NULL;//Define Links -PreparedStatement pstmt =NULL;//Defining precompiled Commands -ResultSet rs =NULL;//defining a result set + Try - { +Class.forName (Dirver);//Load Driver Aconn = Drivermanager.getconnection (dburl, user, password);//Get Connections atString sql = "Select T.id as tid,t.name as tname,t.sex as tsex from T_user T";//Defining SQL Statements -pstmt = conn.preparestatement (sql);//Execute SQL -rs = Pstmt.executequery ();//Get result set - if(Rs.next ()) - { -ResultSetMetaData metaData = Rs.getmetadata ();//Get ResultSetMetaData in intCount = Metadata.getcolumncount ();//returns the number of columns in this ResultSet object. - //traverse the name of the field that takes out each query to for(inti = 0; I < count; i++) + { -System.out.println ("getColumnName obtained name:" + metadata.getcolumnname (i + 1). toLowerCase () + "" the+ "Getcolumnlabel obtained name:" + metadata.getcolumnlabel (i + 1). toLowerCase ()); * } $ }Panax Notoginseng } - Catch(Exception e) the { + e.printstacktrace (); A } the finally + { - $ Try $ { - if(rs! =NULL ) - { the rs.close (); - }Wuyi if(Pstmt! =NULL ) the { - pstmt.close (); Wu } - if(Conn! =NULL ) About { $ conn.close (); - } - } - Catch(SQLException e) A { + //TODO auto-generated Catch block the e.printstacktrace (); - } $ the } the } the } the Results: - name obtained by getColumnName: Tid getcolumnlabel name: Tid in name obtained by getColumnName: tname getcolumnlabel name: Tname theName obtained by getColumnName: tsex Getcolumnlabel Name: tsex
Summarize:
MySQL obviously getColumnName does not meet the requirements of the user when using two methods to obtain the SQL statement name, not the alias. However, Oracle takes the same value for both methods. Therefore, it is generally recommended to use the Getcolumnlabel method
Note: This article only represents personal understanding and views yo! I have no relationship with the company and the group!
The difference between Getcolumnlabel and getColumnName in ResultSetMetaData