How to use jdbtable mysql database connections?
Abstract: How to use jdbtable mysql database connections?
To be an example
Jdbtable is synchronized with a show that the content of the database, thank you!
Up!
Mark!
Import java.sql .*;
Import javax.swing .*;
Import java.awt .*;
Import java.awt.event .*;
Import java.util .*;
Public class inensshow extends JFrame (
Private Connection connection;
Private Statement statement;
Private ResultSet resultSet;
Private ResultSetMetaData rsMetaData;
/ / GUI variable definitions
Private JTable table;
Private JTextArea inputQuery;
Private JButton submitQuery;
Public inensshow ()
(
/ / Form Heading
Super ( "input SQL statements, by enquiries button to see the results.");
String url = "jdbc: mysql: / / localhost: 3306/web";
String username = "inens";
String password = "inens";
/ / Load the driver to connect database
Try (
Class.forName ( "org.gjt.mm.mysql.Driver");
Connection = DriverManager.getConnection (
Url, username, password);
)
/ / Capture abnormal loading drivers
Catch (ClassNotFoundException cnfex) (
System.err.println (
"Loading JDBC / ODBC driver failure.");
Cnfex.printStackTrace ();
System.exit (1); / / terminate program
)
/ / Catch connecting to the database anomalies
Catch (SQLException sqlex) (
System.err.println ( "not connecting to the database");
Sqlex.printStackTrace ();
System.exit (1); / / terminate program
)
/ / If successful database connection, the establishment of GUI
/ / SQL statement
String test = "SELECT * FROM data";
InputQuery = new JTextArea (test, 4, 30);
SubmitQuery = new JButton ( "Search");
/ / Button incident
SubmitQuery.addActionListener (
New ActionListener () (
Public void actionPerformed (ActionEvent e)
(
GetTable ();
)
)
);
JPanel topPanel = new JPanel ();
TopPanel.setLayout (new BorderLayout ());
/ / "Input about the" edit box layout to the "CENTER"
TopPanel.add (new JScrollPane (inputQuery), BorderLayout.CENTER);
/ / "To the inquiry" button layout to the "SOUTH"
TopPanel.add (submitQuery, BorderLayout.SOUTH);
Table = new JTable ();
Container c = getContentPane ();
C.setLayout (new BorderLayout ());
/ / "TopPanel" edit box layout to the "NORTH"
C.add (topPanel, BorderLayout.NORTH);
/ / "Table" edit box layout to the "CENTER"
C.add (table, BorderLayout.CENTER);
GetTable ();
SetSize (500, 300);
/ / Show Form
Show ();
)
Private void getTable ()
(
Try (
/ / Implementation of SQL statements
String query = inputQuery.getText ();
Connection.createStatement statement = ();
ResultSet = statement.executeQuery (query);
/ / In the form of query results show
DisplayResultSet (resultSet);
)
Catch (SQLException sqlex) (
Sqlex.printStackTrace ();
)
)
Private void displayResultSet (ResultSet rs)
Throws SQLException
(
/ / Positioning arrival of the first records
Boolean moreRecords = rs.next ();
/ / If there is no record, a news tips
If (! MoreRecords) (
JOptionPane.showMessageDialog (this,
"As a result no centralized records");
SetTitle ( "records show");
Return;
)
Vector columnHeads = new Vector ();
Vector rows = new Vector ();
Try (
/ / Get the name field
ResultSetMetaData rsmd = rs.getMetaData ();
For (int i = 1; i <= rsmd.getColumnCount (); + + i)
ColumnHeads.addElement (rsmd.getColumnName (i));
/ / Get the record set
Do (
Rows.addElement (getNextRow (rs, rsmd));
) While (rs.next ());
/ / In the form of query results show
Table = new JTable (rows, columnHeads);
JScrollPane scroller = new JScrollPane (table);
Container c = getContentPane ();
C.remove (1);
C.add (scroller, BorderLayout.CENTER);
/ / Refresh Table
C.validate ();
)
Catch (SQLException sqlex) (
Sqlex.printStackTrace ();
)
)
Private Vector getNextRow (ResultSet rs,
ResultSetMetaData rsmd)
Throws SQLException
(
Vector currentRow = new Vector ();
For (int i = 1; i <= rsmd.getColumnCount (); + + i)
CurrentRow.addElement (rs.getString (i));
/ / Return to a record
Return currentRow;
)
Public void shutDown ()
(
Try (
/ / Disconnect database connection
Connection.close ();
)
Catch (SQLException sqlex) (
System.err.println ( "Unable to disconnect");
Sqlex.printStackTrace ();
)
)
Public static void main (String args [])
(
Final inensshow app =
New inensshow ();
App.addWindowListener (
New WindowAdapter () (
Public void windowClosing (WindowEvent e)
(
App.shutDown ();
System.exit (0);
)
)
);
)
)
↑ Back
Tags: java mysql
Releated Java Articles
June 24, 2007 | Filed Under Database Programming |






