C# Java 中的 DataTable 等效项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1340283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataTable equivalent in Java
提问by Ajay
Is there a C# DataTable equivalent in Java?
Java 中是否有等效的 C# DataTable?
采纳答案by Robert Petermeier
A similar question that has been asked recently. ResultSet certainly is not a direct equivalent as it only works with an active connection to the database while a DataTable can be used "offline".
最近有人问过一个类似的问题。ResultSet 当然不是直接的等价物,因为它仅适用于与数据库的活动连接,而 DataTable 可以“离线”使用。
From personal experience I would say there is no direct equivalent in Java (haven't tried javax.sql.rowset.WebRowSet
, though). You either go with plain SQL and java.sql.ResultSet
is your friend. Or you use some ORM tool like Hibernate, Cayenne, Toplink to name a few. Or you build your own (not that I encourage this, but I think on more than one project that has been done successfully).
根据个人经验,我会说 Java 中没有直接的等价物(不过还没有尝试过javax.sql.rowset.WebRowSet
)。你要么使用普通的 SQL,要么java.sql.ResultSet
是你的朋友。或者您使用一些 ORM 工具,例如 Hibernate、Cayenne、Toplink 等等。或者您自己构建(不是我鼓励这样做,而是我认为已经成功完成的项目不止一个)。
回答by JL.
Consider using a
考虑使用
java.sql.ResultSet
Statement s = conn.createStatement();
ResultSet rs = s.executeQuery("select * from generic_table");
ResultSetMetaData md = rs.getMetaData();
回答by oxbow_lakes
No - not in the standard libraries (i.e. the Java API).
否 - 不在标准库中(即 Java API)。
回答by Menefee
A workaround I've used is JTable. It doesn't have the robust data features of a proper DataTable but it will allow you grab some data and bind it to a control with some structure.
我使用的一种解决方法是 JTable。它没有适当的 DataTable 的强大数据功能,但它允许您获取一些数据并将其绑定到具有某种结构的控件。
class TableModel extends AbstractTableModel
{
String[] columnNames = {“FirstName”,”LastName”,”Title”};
Object[][] rowData= {{‘John,”Smith”,”President”},{“John”,”Doe”,”Employee”}};
public int getColumnCount()
{
return columnNames.length;
}
public int getRowCount()
{
return rowData.length;
}
public String getColumnName(int col)
{
return columnNames[col];
}
public Object getValueAt(int row, int col)
{
return data[row][col];
}
}
And then to use you simply:
然后简单地使用你:
JTable table = new JTable(new TableModel());
Again, this is quick and simple and if you are dealing with large amounts of data I would recommend using a proper ORM tool.
同样,这既快速又简单,如果您正在处理大量数据,我建议您使用适当的 ORM 工具。
回答by jli_123
Give this framework a try:
试试这个框架:
Its a in-memory dataset library that is generic and also type safe. You can:
它是一个通用且类型安全的内存数据集库。你可以:
- Pull data automatically from relational queries (or from any other programmatic source),
- Issue complex, chained queries against the dataset,
- Optimize a given dataset by specifying indexes on particular columns.
- 从关系查询(或任何其他程序源)中自动提取数据,
- 针对数据集发出复杂的链式查询,
- 通过在特定列上指定索引来优化给定数据集。
Its easy-to-use and doesn't have any significant dependencies. Its also compliant with java.sql.ResultSet, so its possible to integrate this easily into any existing apps that query against relational database
它易于使用并且没有任何重要的依赖性。它也符合 java.sql.ResultSet,因此可以轻松地将其集成到任何现有的查询关系数据库的应用程序中
回答by Sayeed S. Alam
From Standard Library DefaultTableModel
is good class.
从标准库DefaultTableModel
是好课。
ResultSet set = s.getResultSet();
ResultSetMetaData metaData = set.getMetaData();
int totalColumn = metaData.getColumnCount();
Object[] dataRow = new Object[totalColumn];
if(set!= null)
{
for(int i=1;i<=totalColumn;i++)
{
table.addColumn(metaData.getColumnName(i));
}
while(set.next())
{
for(int i=1;i<=totalColumn;i++)
{
dataRow[i-1] = set.getObject(i);
}
table.addRow(dataRow);
}
}