Click the button to read data from the database and then display it through jtable

Source: Internet
Author: User

// Key part

Vector <string> columnnames = new vector <string> ();
Vector rows = new vector ();
// Execute the query and obtain the query result
Try {
String SQL = "select * From fnsubject ";
Con = dbconnection. getcon ();
Stmt = con. createstatement ();
Rs = stmt.exe cutequery (SQL );
Resultsetmetadata rsmd = Rs. getmetadata ();
Int colnum = rsmd. getcolumncount (); // obtain the number of Columns
Columnnames. Clear (); // clear all elements in the Set
For (INT column = 0; column <colnum; column ++ ){
Columnnames. addelement (rsmd. getcolumnlabel (column + 1 ));
}
// Obtain the information of all rows and add them to the vector rows.
Rows. Clear (); // clear the set
While (Rs. Next ()){
Vector <Object> newrow = new vector <Object> ();
For (INT I = 1; I <= colnum; I ++ ){
Newrow. addelement (Rs. GetObject (I ));
}
Rows. addelement (newrow );
}
} Catch (exception ex ){
}
// Create a table using the query results and display the table
Jdialog jd1 = new jdialog (this ,"");
Jtable book_table = new jtable (rows, columnnames );
Jscrollpane scrollpane1 = new jscrollpane (book_table );
Book_table.setpreferredscrollableviewportsize (new dimension (500, 70 ));
Jd1.getcontentpane (). Add (scrollpane1, borderlayout. center );
Jd1.setsize (450,150 );
Jd1.setlocation (550,100 );
Jd1.show ();

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.