A value taken from a database in Java and displayed in a table

Source: Internet
Author: User
Tags odbc stmt

1: Simple import java.util.Vector; Import Javax.swing.JFrame; Import Javax.swing.JScrollPane; Import javax.swing.JTable; Import Javax.swing.table.DefaultTableModel; public class Table extends JFrame {private JTable table; public static void Main (String args[]) {try {table frame = new Table (); Frame.setvisible (TRUE); catch (Exception e) {e.printstacktrace ();}} Public Table () {super (); Getcontentpane_r (). setlayout (null); SetBounds (MB, 375); Setdefaultcloseoperation ( Jframe.exit_on_close); Final JScrollPane ScrollPane = new JScrollPane (); Scrollpane.setbounds (55, 65, 340, 174); Getcontentpane_r (). Add (ScrollPane); Table = new JTable (); Scrollpane.setviewportview (table); This. Creattable (); private void Creattable () {DefaultTableModel DTM = new DefaultTableModel (); Vector Title = new vector (); Add title Title.add ("name"); Title.add ("gender"); Title.add ("hobby"); Vector C1 = new vector (); Add the first piece of content C1.add ("Ximen Qing"); C1.add ("male"); C1.add ("SM"); Vector c2 = new vector (); Add toThe second content C2.add ("Jin Lian"); C2.add ("female"); C2.add ("ML"); Vector Main = new vector (); Main.add (C1); Main.add (C2); Dtm.setdatavector (Main, Title); Table.setmodel (DTM); //2. The database//is the authors table of the pubs database that connects SQL Server. I use ODBC driver, the data source is pubs. Import javax.swing.*; Import java.sql.*; Class SqlDemo2 extends jframe{SqlDemo2 () {try{string[] colheads={"name", "Phone", "City"}; Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver"); String url= "Jdbc:odbc:pubs"; Connection Con=drivermanager. Getconnection_r (URL, "sa", ""); Statement stmt=con.createstatement (); ResultSet rs; Rs=stmt.executequery ("SELECT count (*) as Au_count from authors"); Rs.next (); int Icount=rs. Getint_r ("Au_count"); Object[][] Data=new object[icount][]; int i=0; Rs=stmt.executequery ("SELECT * from authors"); while (Rs.next ()) {data[i]=new object[3]; data[i][0]=rs. Getstring_r ("au_fname"); Data[i][1]=rs. Getstring_r ("Phone"); Data[i][2]=rs. Getstring_r ("City"); i++; } JTable table=new JTable (data,colheads); JScrollPane JSP=new JScrollPane (table); This. Getcontentpane_r (). Add (JSP); This.setdefaultcloseoperation (Exit_on_close); catch (Exception e) {System.out.println (e);}} public static void Main (string[] args) {SqlDemo2 sd=new SqlDemo2 (); sd.setsize (400,250); sd.setvisible (True);}}

Related Article

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.