如何在Java中创建DataTable? [英] How to Create DataTable in Java ?
本文介绍了如何在Java中创建DataTable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我是java编程的初学者。我尝试使用ResultSet在java中读取Sql Table,并希望在本地过滤该Resultset,就像在C#中的DataTable.select()一样。但是Resultset不允许这样做。我不确定是否可以使用Resultset。
[我想将Sql查询结果存储在本地内存中以访问进一步过滤而不再使用命中数据库&再次]
我目前的代码:
Hi All,
I'm beginner in java programming. I tried to read Sql Table in java using ResultSet and want to filter that Resultset locally just like DataTable.select() in C#. But Resultset can not allowed that. I'm not sure about is it ok to use Resultset.
[I want to store Sql query Result in Local memory to access further filter without hit database again & again]
My current code :
Connection conn = null;
String url = "jdbc:sqlserver://server;
String username = username;
String password = password;
conn = DriverManager.getConnection(url, username, password);
// Executing SQL-command
String sqlcommand = "SELECT * FROM [" + database + "].[dbo].["
+ tablename + "]";
Statement statement = conn.createStatement();
ResultSet res = statement.executeQuery(sqlcommand);
If anybody have idea please share.
推荐答案
这是一个可以帮助用Java创建DataTable的简单代码。您可以查看我提供的示例,然后根据示例修改代码。
Here is a simple code that can help make a DataTable in Java. You Can look at the example I provided and then modify your code based on the example.
/*
* Imports the content from the specified DataTable into a new Aspose.Words Table object.
* The table is inserted at the current position of the document builder and using the current builder's formatting if any is defined.
*/
public static Table importTableFromDataTable(DocumentBuilder builder, DataTable dataTable, boolean importColumnHeadings) throws Exception
{
Table table = builder.startTable();
ResultSetMetaData metaData = dataTable.getResultSet().getMetaData();
int numColumns = metaData.getColumnCount();
// Check if the names of the columns from the data source are to be included in a header row.
if (importColumnHeadings)
{
// Store the original values of these properties before changing them.
boolean boldValue = builder.getFont().getBold();
int paragraphAlignmentValue = builder.getParagraphFormat().getAlignment();
// Format the heading row with the appropriate properties.
builder.getFont().setBold(true);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
// Create a new row and insert the name of each column into the first row of the table.
for (int i = 1; i < numColumns + 1; i++)
{
builder.insertCell();
builder.writeln(metaData.getColumnName(i));
}
builder.endRow();
// Restore the original formatting.
builder.getFont().setBold(boldValue);
builder.getParagraphFormat().setAlignment(paragraphAlignmentValue);
}
// Iterate through all rows and then columns of the data.
while(dataTable.getResultSet().next())
{
for (int i = 1; i < numColumns + 1; i++)
{
// Insert a new cell for each object.
builder.insertCell();
// Retrieve the current record.
Object item = dataTable.getResultSet().getObject(metaData.getColumnName(i));
// This is name of the data type.
String typeName = item.getClass().getSimpleName();
if(typeName.equals("byte[]"))
{
// Assume a byte array is an image. Other data types can be added here.
builder.insertImage((byte[])item, 50, 50);
}
else if(typeName.equals("Timestamp"))
{
// Define a custom format for dates and times.
builder.write(new SimpleDateFormat("MMMM d, yyyy").format((Timestamp)item));
}
else
{
// By default any other item will be inserted as text.
builder.write(item.toString());
}
}
// After we insert all the data from the current record we can end the table row.
builder.endRow();
}
// We have finished inserting all the data from the DataTable, we can end the table.
builder.endTable();
return table;
}
这篇关于如何在Java中创建DataTable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文