Java code for getting data from SQL server to MongoDB

I have been struggling with my Mongo-SQL code for a while and still need your help :)

I have a problem while transferring data from an SQL server database to MongoDB. My problem is that I can't do calculations like AVERAGE() or SUM() on my data since I saved them as string in MongoDB. I thought that the numbers would be integers since I got them from my SQL server database where they are stored as integers using the code below. I see now that I use getString() when getting the values. Is that why the numbers are strings in MongoDB? How can I get them as integers? I really want to be able to manipulate them as numbers! Also, some values in SQL server are datetime, so I will need a lot of 'if' statements and different 'get' methods to get all the types right in MongoDB. Does anyone have a good solution to this problem?

StringBuilder orderstatus = new StringBuilder();
orderstatus.append("SELECT * FROM dbo.fact_orderstatus");
PreparedStatement t = connect.prepareStatement(orderstatus.toString());  

DBCollection orderstat = db.getCollection("Orderstatus");

ResultSet v = t.executeQuery();
ResultSetMetaData rsm = t.getMetaData();
int column = rsm.getColumnCount();

while (v.next()) {
BasicDBObject orderObj = new BasicDBObject();
for(int x=1; x<column +1; x++){
String namn= rsm.getColumnName(x);

String custNum = (v.getString(x));
if (custNum != null && !custNum.trim().isEmpty()
&& custNum.length() != 0)
orderObj.append(namn, custNum);

}


orderstat.insert(orderObj)

Answers


You can utilize the getColumnType() method on your result set.

Since you're using SQL-server I would suggest reading this first: http://msdn.microsoft.com/en-us/library/ms378668.aspx

This SO question/answer is likely to be helpful too: Most effecient conversion of ResultSet to JSON?


Need Your Help

JNI crash when CallVoidMethod to java function to handler

android android-ndk jni

When calling my JNI function CallVoidMethod for my UDP program, my program crashes. I'm having trouble deciphering the problem from the logcat. Any help would be greatly appreciated.

Capture current user custom field and save in database

c# asp.net-mvc

I'm using a custom membership provider. I'm trying to capture a custom field called FirstName which is in the Users table. I have another table called Pages which is used to store newly created pag...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.