Java gets the number of columns and rows of the database query results, printing the query results

Source: Internet
Author: User
Tags stmt

Java Connection database and simple operation see my previous essay:http://www.cnblogs.com/meitian/p/5036332.html  first, the number of rows and columns to get the results of the queryquery result is ResultSet object
this. executequery (sql_temp);
 To view the number of columns:
int column_count=selectresult.getmetadata (). getColumnCount ();
GetMetaData () returns all column informationGetMetaData (). getColumnName (i): I pass the column int index to get the name of the column To view the number of rows:
int row_count=selectresult.getrow ();
 Second, print the results of the query and corresponding valuesso if you want to print the value of the query, you can query the query result column number, and then loop to print, the code is as follows
 This. con =drivermanager.getconnection (URL, user, password); This. stmt =con.createstatement (); ResultSet Selectresult= This. stmt.executequery (SQL);intColumn_count =selectresult.getmetadata (). getColumnCount ();intSize = 0; while(Selectresult.next () && Size < 10) {String Selectresult_single= "";  for(inti = 1; I <= Column_count; i++) {String ColumnName=selectresult.getmetadata (). getColumnName (i); String Columnvalue=selectresult.getstring (i); Selectresult_single= Selectresult_single + ColumnName + "=" + Columnvalue + ""; } size= size + 1;}
 Description: Because I don't want to print all of the results, I've added a maximum number of prints in the while

Java gets the number of columns and rows of the database query results, printing the query results

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.