Create a new Clojure project in Eclipse clj01
Clojure operation MySQL relies on the Mysql-connector-java, Clojure-contrib and JAVA.JDBC three jar packages.
PROJECT.CLJ configuration information is as follows: Configuration is complete: dependencies save, the system will automatically download the configured jar package information.
(Defproject clj01"0.1.0-snapshot":d escription"fixme:write Description": URL"Http://example.com/FIXME": License {: Name"Eclipse Public License": URL"http://www.eclipse.org/legal/epl-v10.html"}:d ependencies [[Org.clojure/clojure"1.5.1"] [Org.clojure/java.jdbc"0.2.3"] [Org.clojure/clojure-contrib"1.2.0"] [MySQL/mysql-connector-java"5.1.26"] ])
New MYSQL.CLJ
(NS Clj01.mysql (: Require [Clojure.java.jdbc:as SQL]), configuration parameters (defmysql-db{: ClassName"Com.mysql.jdbc.Driver": Subprotocol"MySQL": SubName"//127.0.0.1:3306/test": User"Root":p Assword""}); link database (SQL/with-connection mysql-db); Insert data (SQL/with-connection mysql-db (SQL/insert-records:user {: Name"Test 1": Age 25} {: Name"Test 2": Age 26}); Delete record (SQL/with-connection mysql-db (SQL/delete-rows:user ["ID >?"8]); update record (SQL/with-connection mysql-db (SQL/update-Values:user ["id =?"8] {: Name"Test": Age 35}); query record (println (SQL/with-connection mysql-db (SQL/with-query-results rows ["Select Id,name,age from user where >?"25] (Dorun (Map#(println%) rows ))))
Table structure:
mysql> desc User; +-------+-------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra ID int(one) | NO | PRI | NULL | auto_increment | | name | varchar | YES | | NULL | | | age int(one) | YES | | NULL | | +-------+-------------+------+-----+---------+----------------+
Issue: The console prompts the following exception when you run the code for the first time:
Locate Clojure/java/jdbc__init.class or CLOJURE/JAVA/JDBC.CLJ on classpath: clojure.lang.RT.load ( Rt.java:443)
Problem resolution after restarting Eclipse seems to be caused by an eclipse tool problem.
Clojure Operation MySQL