Datatable Equivalent in Java

DataTable equivalent in Java

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".

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).

I need something like a DataTable in Java

Think OOP. You need to create a Person class with the properties name, age and height.

public class Person {

private String name;
private String age;
private String height;

// Getter and setter methods...
}

Next, read CSV file line by line using a BufferedReader. For each line you want to split the line using line.split(";"); to give you an String[] containing the tokens.

For the line you can now create a Person from the tokens;

Person person = new Person();
person.setName(tokens[0]);
person.setAge(tokens[1]);
person.setHeight(tokens[2]);

Note that this assumes the positions of the columns. If you want to remove this assumption you need to Map the header to a position.

String headerLine = "NAME;AGE;HEIGHT";
String[] headers = headerLine.split(";");
Map<String,Integer> map = new HashMap<String,Integer>();
for (int i=0; i<headers.length; i++) {
map.put(headers[i], i);
}

You can then get the position of a column by it's name;

Person person = new Person();
person.setName(tokens[map.get("NAME")]);
person.setAge(tokens[map.get("AGE")]);
person.setHeight(tokens[map.get("HEIGHT")]);

Table type classes are an anti-pattern. They tend to appear when the programmer can't be bothered to think in terms of Objects. I often see Table classes in legacy java applications and its a real PITA to work with them. Don't do it.

Is there a type in java equivalent to DataSet in C#?

You're probably looking for disconnected rowsets.

But frankly, most of the time, I just transform the data in a result set into a list of Java objects, and then close the connection, and use this list of objects later on.

Create datatable in java

Simplest solution use javax.persistance.* JPA, define in persistance.xml your connection URL, persistance unit name. With native query you dont need to know ORM(object relation mapping) just standart SQL syntax. Then simple cast Object to corresponding primitive type.

factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
EntityManager em = factory.createEntityManager();
Query q = em.createNativeQuery("SELECT a.firstname, a.age FROM Author a");
List<Object[]> authors = q.getResultList();
for (Object[] a : authors) {
String first = (String)a[0];
int last = (int)a[1];
}

List<Object[]> is like 2D datatable.

what is the equivalent of ado.net datatable in java?

There is no exact equivalent of a DataTable in JDBC. The closest thing is probably the CachedRowSet.

Convert serialized instance of C# DataTable to java

(I'm assuming the server uses the default .NET serializer.)

The .NET and Java serializer are incompatible, so you can't deserialize the C# class to Java.

To connect the C# server to the Java client, you have to use a compatible serialization library on both platforms. One example is WOX, as of this answer to a similar question. (This is an XML serializer, so there will be considerable overhead.)

How do I do like DataTable in Android-Java based application.

If you are using eclipse then you can use SQlite Browser to store your database and then can push data base in ur device by going to Window->Open perspective->other->DDMS
how can we push ".db" into emulator?
and then u can apply queries on your database.
http://www.vogella.de/articles/AndroidSQLite/article.html

DataSet class in Java?

Have you looked at javax.sql.rowset.WebRowSet?

From the Javadocs:

The WebRowSetImpl provides the
standard reference implementation,
which may be extended if required.

The standard WebRowSet XML Schema
definition is available at the
following URI:

   http://java.sun.com/xml/ns/jdbc/webrowset.xsd

It describes the standard XML document
format required when describing a
RowSet object in XML and must be used
be all standard implementations of the
WebRowSet interface to ensure
interoperability. In addition, the
WebRowSet schema uses specific SQL/XML
Schema annotations, thus ensuring
greater cross platform
inter-operability. This is an effort
currently under way at the ISO
organization. The SQL/XML definition
is available at the following URI:

   http://standards.iso.org/iso/9075/2002/12/sqlxml


Related Topics



Leave a reply



Submit