clojure java.jdbc db-do-prepared and dynamic parameters

667 views
Skip to first unread message

Avinash Dongre

unread,
Dec 8, 2013, 12:44:22 PM12/8/13
to clo...@googlegroups.com

I see following example in Clojure.java.jdbc

(sql/db-do-prepared db "INSERT INTO fruit2 ( name, appearance, cost, grade ) VALUES ( ?, ?, ?, ? )" ["test" "test" 1 1.0])

But how do i convert following java code into clojure. I am new to clojure and not sure how to i pass multiple vector

final int numRows = 10000;
    PreparedStatement pstmt = conn
        .prepareStatement("insert into new_order values (?, ?, ?)");
    for (int id = 1; id <= numRows; id++) {
      pstmt.setInt(1, id % 98);
      pstmt.setInt(2, id % 98);
      pstmt.setInt(3, id);
      int count;
      if ((count = pstmt.executeUpdate()) != 1) {
        System.err.println("unexpected update count for a single insert " + count);
        System.exit(2);
      }
      if ((id % 500) == 0) {
        System.out.println("Completed " + id + " inserts ...");
      }
    }

Sean Corfield

unread,
Dec 8, 2013, 5:56:40 PM12/8/13
to clo...@googlegroups.com
Do you want that specific piece of (procedural) Java converted to
Clojure or do you have a real use case in the context of a real
problem that you're trying to solve?

I suspect just showing you what that piece of Java would look like in
Clojure isn't going to teach you how to do this in general... and
there are multiple approaches that would each be more suitable to
particular real world problems.

Sean
> --
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@googlegroups.com
> Note that posts from new members are moderated - please be patient with your
> first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.



--
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
World Singles, LLC. -- http://worldsingles.com/

"Perfection is the enemy of the good."
-- Gustave Flaubert, French realist novelist (1821-1880)

Niels van Klaveren

unread,
Dec 9, 2013, 4:52:14 AM12/9/13
to clo...@googlegroups.com
I think the OP just meant to ask how to do a PreparedStatement batch update in clojure.jdbc.

The simplest answer is, just give in multiple parameter vectors to db-do-prepared


 (sql/db-do-prepared db
                     "INSERT INTO fruit2 ( name, appearance, cost, grade ) VALUES ( ?, ?, ?, ? )"
                     ["banana" "yellow" 1 1.0]
                     ["apple" "green" 2 1.0]
                     ["orange" "orange" 3 1.0])

And as usual, if you have what should be separate parameters already in a collection, use the apply function

  (apply sql/db-do-prepared db
  "INSERT INTO fruit2 ( name, appearance, cost, grade ) VALUES ( ?, ?, ?, ? )"
   [["banana" "yellow" 1 1.0]
    ["apple" "green" 2 1.0]
    ["orange" "orange" 3 1.0]])
Reply all
Reply to author
Forward
0 new messages