JTable Writing to database contents

Source: Internet
Author: User
Tags import database

/*
Import database data in JTable.
Creation of 2 vectors
Col and dat  col are stored in the field name
DAT to the data content.
Dbname=new JTable (Dat,col);

*/
Package Demo;import java.awt.*;import javax.swing.*;import java.awt.event.*;import java.io.*;import java.sql.*; Import Java.util.vector;public class Tabledemo extends Jframe{jscrollpane jsp=new JScrollPane (); JTable dbname;public Tabledemo () {super ("Loading database content example"); Container C=getcontentpane (); C.setlayout (new BorderLayout ()); ResultSetMetaData Rsd=null; Vector col= new vector (); Vector dat= new vector (); Try{class.forname ("Com.mysql.jdbc.Driver");} catch (ClassNotFoundException E1) {System.out.println ("The related driver not found!");} Try{connection con=drivermanager.getconnection ("Jdbc:mysql://localhost:3306/mysql?user=root&password=root &useunicode=true&characterencoding=utf8 "); Statement st=con.createstatement (); ResultSet rs=st.executequery ("SELECT * from User"), Rsd=rs.getmetadata (); for (int i=1;i<=rsd.getcolumncount (); i++) {Col.add (Rsd.getcolumnname (i));} while (Rs.next ()) {vector v=new vector (), for (int i=1;i<=rsd.getcolumncount (); i++) {v.addelement (rs.getstring (i));} Dat.addelement (v);} St.close (); con.clOSE (); SYSTEM.OUT.PRINTLN ("Database load succeeded!");} catch (SQLException E2) {System.out.println ("Table not found!" +e2.getnextexception () + "\ n" +e2.getsqlstate ()); Dbname=new JTable (Dat,col); Jsp.getviewport (). Add (dbname); C.add (JSP); Setdefaultcloseoperation (Jframe.exit_on_ CLOSE); setSize (800,600); setvisible (true);} public static void Main (string[] args) {Tabledemo jst=new tabledemo ();}}

JTable write to database contents

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.