标签:des style blog http color io java ar 数据
在Eclipse中新建一个Clojure工程clj01
clojure 操作mysql需要依赖mysql-connector-java、clojure-contrib与java.jdbc三个jar包。
project.clj配置信息如下:配置完:dependencies 保存,系统会自动下载所配置的jar包信息。
(defproject clj01 "0.1.0-SNAPSHOT" :description "FIXME: write description" :url "http://example.com/FIXME" :license {:name "Eclipse Public License" :url "http://www.eclipse.org/legal/epl-v10.html"} :dependencies [ [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"] ])
新建mysql.clj
(ns clj01.mysql (:require [clojure.java.jdbc :as sql])) ;配置参数 (def mysql-db{ :classname "com.mysql.jdbc.Driver" :subprotocol "mysql" :subname "//127.0.0.1:3306/test" :user "root" :password "" }) ;链接数据库 (sql/with-connection mysql-db) ;插入数据 (sql/with-connection mysql-db (sql/insert-records :user {:name "测试1" :age 25} {:name "测试2" :age 26})) ;删除记录 (sql/with-connection mysql-db (sql/delete-rows :user ["id > ?" 8])) ;更新记录 (sql/with-connection mysql-db (sql/update-values :user ["id = ?" 8] {:name "测试" :age 35})) ;查询记录 (println (sql/with-connection mysql-db (sql/with-query-results rows ["select id,name,age from user where age > ?" 25] (dorun (map #(println %) rows)))))
表结构:
mysql> desc user; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(10) | YES | | NULL | | | age | int(11) | YES | | NULL | | +-------+-------------+------+-----+---------+----------------+
问题:在第一次运行代码时控制台提示如下异常:
otFoundException Could not locate clojure/java/jdbc__init.class or clojure/java/jdbc.clj on classpath: clojure.lang.RT.load (RT.java:443)
重启Eclipse之后问题解决,好像是Eclipse工具问题引起的。
标签:des style blog http color io java ar 数据
原文地址:http://www.cnblogs.com/yshyee/p/3949455.html