Jython访问MySQL的准备工作如下:
成都创新互联长期为成百上千客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为洛川企业提供专业的网站建设、网站设计,洛川网站改版等技术服务。拥有十余年丰富建站经验和众多成功案例,为您定制开发。
下载Jython,JDBC和MySQL。
安装(需要已经安装了jdk/jre?)
java -jar jython_installer-2.2.1.jar 安装到C:\jython2.2.1
将zxJDBC.jar解压缩到c:\tmp
将mm.mysql-2.0.4-bin.jar复制到c:\tmp
C:\jython2.2.1>set CLASSPATH=c:\tmp\mm.mysql-2.0.4-bin.jar;c:\tmp\zxJDBC.jar;%CLASSPATH%
启动mysql服务器
- C:\mysql51>cd bin
- C:\mysql51\bin>mysqld --console
并建立数据库
- C:\mysql51\bin>mysql -u root
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 1
- Server version: 5.1.14-beta-community MySQL Community Server (GPL)
- Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
- mysql> show databases
- -> ;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | mysql |
- +--------------------+
- 2 rows in set (0.00 sec)
- mysql> create database test;
- Query OK, 1 row affected (0.01 sec)
- mysql> use test
- Database changed
- mysql> create table a1(c1 varchar(30));
- Query OK, 0 rows affected (0.02 sec)
- mysql> insert into a1 values ('Hello World');
- Query OK, 1 row affected (0.00 sec)
进入Jython访问MySQL
- C:\jython2.2.1>jython
- Jython 2.2.1 on java1.6.0
- Type "copyright", "credits" or "license" for more information.
- >>> from com.ziclix.python.sql import zxJDBC
- >>> mysqlConn = zxJDBC.connect("jdbc:mysql://localhost/test",
- ... "root", "",
- ... "org.gjt.mm.mysql.Driver")
- >>> cursor = mysqlConn.cursor()
- >>> cursor.execute("SELECT * FROM a1");
- >>> cursor.fetchone()
- ('Hello World',)
- >>> cursor.fetchall()
- []
- >>> cursor.description
- [('c1', 12, 30, None, None, None, 1)]
- >>>
需要用jython自己封装的dbexts库
最后编辑一个名为dbexts.ini的文件保存到c:\jython2.2.1\lib
- [default]
- name=mysqltest
- [jdbc]
- name=mysqltest
- url=jdbc:mysql://localhost/test
- user=root
- pwd=
- driver=org.gjt.mm.mysql.Driver
进入jython
- >>> from dbexts import dbexts
- >>> mysqlcon = dbexts("mysqltest", "c:\jython2.2.1\lib\dbexts.ini
- >>> mysqlcon.table()
- TABLE_CAT | TABLE_SCHEM | TABLE_NAME | TABLE_TYPE | REMARKS
- -----------------------------------------------------------
- | | a1 | TABLE |
- 1 row affected
- >>> mysqlcon.table('a1')
- TABLE_CAT | TABLE_SCHEM | TABLE_NAME | COLUMN_NAME | DATA_TYPE |
- S | NUM_PREC_RADIX | NULLABLE | REMARKS | COLUMN_DEF | SQL_DATA_
- SITION | IS_NULLABLE
- ----------------------------------------------------------------
- ----------------------------------------------------------------
- --------------------
- | | a1 | c1 | 12 |
- | 10 | 1 | | | 0
- | YES
- 1 row affected
- >>> mysqlcon.isql("SELECT * FROM a1")
- C1
- -----------
- Hello World
- 1 row affected
- DML
- >>> mysqlcon.isql("insert into a1 values('insert by dbexts')")
- >>> mysqlcon.isql("SELECT * FROM a1")
- C1
- ----------------
- Hello World
- insert by dbexts
- 2 rows affected
Jython访问MySQL的设置到此就完成了。
分享名称:Jython访问MySQL详细步骤
转载源于:http://www.36103.cn/qtweb/news43/11943.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联