Output the elements in the database as a table (with source code)

Source: Internet
Author: User

There are two classes: one for the database, one for the test.

Package com.test.table;

Import Java.awt.Color;

Import Javax.swing.BorderFactory;
Import Javax.swing.JFrame;
Import Javax.swing.JScrollPane;
Import javax.swing.JTable;
Import Javax.swing.table.DefaultTableModel;

/**
* Creation of Table
*
* @author Administrator
*
*/
public class TestTable {

Private JFrame frame;
private static DefaultTableModel model;
private static JTable table;
Private string[] Header = {"title", "Publication Number", "Price"};
Private string[][] data = null;

Public TestTable () {
Init ();
Addcompont ();
Show ();
}

private void Show () {
Frame.setbounds (200, 300, 300, 400);
Frame.setvisible (TRUE);

}

/**
* Add components
*/
private void Addcompont () {

Frame.add (New JScrollPane (table));

}

/**
* Instance First select the method to be listed separately right-click Refactor->extract or Alt+shift+m Import package
* Ctrl+shift+o View shortcut keys Ctrl+shift+l
*/
private void init () {
frame = new JFrame ("Test");
Model = new DefaultTableModel (data, header);
Table = new JTable (model);
Table.setborder (Borderfactory.createlineborder (Color.Blue));

}

public static void Main (string[] args) {
New TestTable ();
New DataBase (). addtable (model);
}

}

---------------------------------------------------------------------------------------

Package com.test.table;

Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;

Import Javax.swing.table.DefaultTableModel;

Import Org.junit.Test;

Import com.mysql.jdbc.PreparedStatement;

/**
* Database Operations
*
* @author Administrator
*
*/
public class DataBase {

private Connection con = null;
Private PreparedStatement PS = null;
Private ResultSet rs = null;
private String name;
Private String ID;
Private String Price;
Private string[] data;

static {
try {
Class.forName ("Com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
SYSTEM.OUT.PRINTLN ("Driver load Failed");
}
}

@Test
Public Connection Connection () {
try {
con = drivermanager.getconnection (
"Jdbc:mysql://localhost:3306/book", "root", "111111");
} catch (SQLException e) {
SYSTEM.OUT.PRINTLN ("Database connection failed");
}
return con;
}

@Test
public void addtable (DefaultTableModel model) {

try {
PS = (PreparedStatement) connection (). Preparestatement (
"Select*from math");
rs = Ps.executequery ();

while (Rs.next ()) {
data = null;
Name = rs.getstring ("name");
id = rs.getstring ("id");
Price = Rs.getstring ("price");
data = new string[] {name, ID, price};
Model.addrow (data);
}

} catch (SQLException e) {
}

}

}

-----------------------------------------

Database and running results

Output the elements in the database as a table (with source code)

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.