0

i am trying to export data from postgresql database to MongoDB.I have successfully created string in JSON format and when i store this json in mongoDB collection, only first entry get stored. here is my code: public class jsonTobson {

    public static void main(String[] args) {

    Connection con = null;
    Statement st = null;

    try{
        Class.forName("org.postgresql.Driver");
        con = DriverManager.getConnection("jdbc:postgresql://localhost:5544/ddc", "postgres", "aman");
        st = con.createStatement();

        String sql = "select row_to_json(judge_info) FROM dp.judge_info order by judge_idno";

        ResultSet rs = st.executeQuery(sql);
        StringBuilder builder = new StringBuilder();
        int columnCount = rs.getMetaData().getColumnCount();

        while (rs.next()) {
            for (int i = 0; i < columnCount;) {
                builder.append(rs.getString(i + 1));
                if (++i < columnCount) builder.append(",");
            }
            builder.append("\r\n");
        }
        String resultSetAsString = builder.toString();



        MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
        DB db = mongoClient.getDB( "mongoTest" );
        DBCollection coll = db.getCollection("newTable");
        Set<String> colls = db.getCollectionNames();

        for (String s : colls) {
            System.out.println(s);
        }


    DBObject dbObject = (DBObject)JSON.parse(resultSetAsString);

    coll.insert(dbObject, WriteConcern.NORMAL);                     
        DBCursor cursorDocJSON = coll.find();
        while (cursorDocJSON.hasNext()) {
            System.out.println(cursorDocJSON.next());
        }       

            rs.close();
            st.close();
            con.close();
        } catch ( Exception e)
        {
            e.printStackTrace();
            System.err.println(e.getClass().getName()+": "+e.getMessage());
            System.exit(0);
        } finally {
        }


    }


}
2
  • It doesn't look like you're building valid objects. There's no array being built to insert multiple rows, and/or you're not looping through the results to insert each row as a document. Commented Apr 1, 2014 at 10:40
  • can you post the output of String resultSetAsString = builder.toString(); Commented Apr 1, 2014 at 23:39

1 Answer 1

0
    public static void main(String[] args) {

    Connection con = null;
    Statement st = null;

    try{
        Class.forName("org.postgresql.Driver");
        con = DriverManager.getConnection("jdbc:postgresql://localhost:5544/ddc", "postgres", "aman");
        st = con.createStatement();

        String sql = "select row_to_json(judge_info) FROM dp.judge_info order by judge_idno";

        ResultSet rs = st.executeQuery(sql);

        MongoClient mongoClient=null;
            try {
                mongoClient = new MongoClient("localhost", 27017);
            } catch (UnknownHostException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

        DB db = mongoClient.getDB("mongoTest");
        System.out.println("Connect to database successfully");

        while (rs.next()) {
                DBCollection coll = db.getCollection("newTable");
                System.out.println("Collection selected successfully");

                BasicDBObject doc = new BasicDBObject("data", rs.getString(1));

                coll.insert(doc);
                }
        } catch ( Exception e)
        {
            e.printStackTrace();
            System.err.println(e.getClass().getName()+": "+e.getMessage());
            System.exit(0);
        } finally {
        }
    }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.