使用Java中的JTable从MySQL数据库显示记录 [英] Display Records From MySQL Database using JTable in Java
问题描述
我想从MySQL数据库将 JTable
连接到 ResultSet
,以便查看数据。
I want to connect a JTable
to a ResultSet
from a MySQL database so I can view the data.
我正在寻找描述此任务的一些链接或代码片段。我正在使用Netbeans IDE ..
I am looking for some links or code snippets describing this task. I'm using the Netbeans IDE..
推荐答案
下面是一个课程,它将完成你想要做的事情的基础知识将数据从MySQL数据库读入Java中的 JTable
时。
Below is a class which will accomplish the very basics of what you want to do when reading data from a MySQL database into a JTable
in Java.
import java.awt.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class TableFromMySqlDatabase extends JFrame
{
public TableFromMySqlDatabase()
{
ArrayList columnNames = new ArrayList();
ArrayList data = new ArrayList();
// Connect to an MySQL Database, run query, get result set
String url = "jdbc:mysql://localhost:3306/yourdb";
String userid = "root";
String password = "sesame";
String sql = "SELECT * FROM animals";
// Java SE 7 has try-with-resources
// This will ensure that the sql objects are closed when the program
// is finished with them
try (Connection connection = DriverManager.getConnection( url, userid, password );
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery( sql ))
{
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
// Get column names
for (int i = 1; i <= columns; i++)
{
columnNames.add( md.getColumnName(i) );
}
// Get row data
while (rs.next())
{
ArrayList row = new ArrayList(columns);
for (int i = 1; i <= columns; i++)
{
row.add( rs.getObject(i) );
}
data.add( row );
}
}
catch (SQLException e)
{
System.out.println( e.getMessage() );
}
// Create Vectors and copy over elements from ArrayLists to them
// Vector is deprecated but I am using them in this example to keep
// things simple - the best practice would be to create a custom defined
// class which inherits from the AbstractTableModel class
Vector columnNamesVector = new Vector();
Vector dataVector = new Vector();
for (int i = 0; i < data.size(); i++)
{
ArrayList subArray = (ArrayList)data.get(i);
Vector subVector = new Vector();
for (int j = 0; j < subArray.size(); j++)
{
subVector.add(subArray.get(j));
}
dataVector.add(subVector);
}
for (int i = 0; i < columnNames.size(); i++ )
columnNamesVector.add(columnNames.get(i));
// Create table with database data
JTable table = new JTable(dataVector, columnNamesVector)
{
public Class getColumnClass(int column)
{
for (int row = 0; row < getRowCount(); row++)
{
Object o = getValueAt(row, column);
if (o != null)
{
return o.getClass();
}
}
return Object.class;
}
};
JScrollPane scrollPane = new JScrollPane( table );
getContentPane().add( scrollPane );
JPanel buttonPanel = new JPanel();
getContentPane().add( buttonPanel, BorderLayout.SOUTH );
}
public static void main(String[] args)
{
TableFromMySqlDatabase frame = new TableFromMySqlDatabase();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setVisible(true);
}
}
在您使用的NetBeans IDE中 - 您将需要在Project Properties中添加 MySQL JDBC Driver ,如下所示:
In the NetBeans IDE which you are using - you will need to add the MySQL JDBC Driver in Project Properties as I display here:
否则代码将抛出 SQLException
说明找不到驱动程序。
Otherwise the code will throw an SQLException
stating that the driver cannot be found.
现在在我的例子中, yourdb
是数据库的名称和 animals
是我正在执行查询的表的名称。
Now in my example, yourdb
is the name of the database and animals
is the name of the table that I am performing a query against.
这是什么将输出:
分手记录:
你说你是新手并且需要一些帮助来理解Java的一些基本类和概念。我将在此列出一些,但请记住,您可以随时浏览Oracle网站上的文档。
You stated that you were a novice and needed some help understanding some of the basic classes and concepts of Java. I will list a few here, but remember you can always browse the docs on Oracle's site.
- ArrayList
- Vector
- Try-with-resources statement
这篇关于使用Java中的JTable从MySQL数据库显示记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!