The value obtained from the database in Java, which is displayed in a table.

Source: Internet
Author: User

// Column 1: simple <br/> Import Java. util. vector; <br/> Import javax. swing. jframe; <br/> Import javax. swing. jscrollpane; <br/> Import javax. swing. jtable; <br/> Import javax. swing. table. defaulttablemodel; <br/> public class Table extends jframe {</P> <p> private jtable table; <br/> Public static void main (string ARGs []) {<br/> try {<br/> Table Frame = new table (); <br/> frame. setvisible (true); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}</P> <p> Public table () {<br/> super (); <br/> getcontentpane_r (). setlayout (null); <br/> setbounds (100,100,500,375); <br/> setdefaclocloseoperation (jframe. exit_on_close); </P> <p> final jscrollpane scrollpane = new jscrollpane (); <br/> scrollpane. setbounds (55,65,340,174); <br/> getcontentpane_r (). add (scrollpane); </P> <p> table = new jtable (); <br/> scrollpane. setviewportview (table); <br/> // <br/> This. creattable (); <br/>}</P> <p> private void creattable () {<br/> defaulttablemodel DTM = new defaulttablemodel (); <br/> vector Title = new vector (); <br/> // Add a title <br/> title. add ("name"); <br/> title. add ("gender"); <br/> title. add ("hobbies"); <br/> vector C1 = new vector (); <br/> // Add the first content <br/> c1.add ("Ximen Qing "); <br/> c1.add ("male"); <br/> c1.add ("SM"); <br/> vector C2 = new vector (); <br/> // Add the second content <br/> c2.add ("Pan Jinlian"); <br/> c2.add ("female "); <br/> c2.add ("ML"); <br/> vector main = new vector (); <br/> main. add (C1); <br/> main. add (C2); <br/> DTM. setdatavector (main, title); <br/> table. setmodel (DTM); <br/>}</P> <p> // 2. column-child database </P> <p> // It is the authors table connecting to the pubs database of sqlserver. I use the ODBC driver and the data source is pubs. <br/> Import javax. swing. *; <br/> Import Java. SQL. *; </P> <p> class sqldemo2 extends jframe {<br/> sqldemo2 () {<br/> try {<br/> string [] colheads = {"name", "phone", "city" };< br/> class. forname ("Sun. JDBC. ODBC. jdbcodbcdriver "); <br/> string url =" JDBC: ODBC: pubs "; <br/> connection con = drivermanager. getconnection_r (URL, "sa", ""); <br/> statement stmt = con. createstatement (); <br/> resultset RS; <br/> rs1_stmt.exe cutequery ("select count (*) as au_count from authors"); <br/> Rs. next (); </P> <p> int icount = Rs. getint_r ("au_count"); </P> <p> object [] [] DATA = new object [icount] []; <br/> int I = 0; <br/> rs1_stmt.exe cutequery ("select * from authors"); </P> <p> while (RS. next () {<br/> data [I] = new object [3]; <br/> data [I] [0] = Rs. getstring_r ("au_fname"); <br/> data [I] [1] = Rs. getstring_r ("phone"); <br/> data [I] [2] = Rs. getstring_r ("city"); <br/> I ++; <br/>}</P> <p> jtable table = new jtable (data, colheads ); <br/> jscrollpane JSP = new jscrollpane (table); <br/> This. getcontentpane_r (). add (JSP); <br/> This. setdefaclocloseoperation (exit_on_close); <br/>}< br/> catch (exception e) {<br/> system. out. println (E); <br/>}</P> <p> Public static void main (string [] ARGs) {<br/> sqldemo2 SD = new sqldemo2 (); <br/> SD. setsize (400,250); <br/> SD. setvisible (true); <br/>}</P> <p>

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.