赛迪网 > IT技术 数据库 > MySQL
  IT资讯搜索
 
IT产品搜索
[程序开发][网管世界][网络安全][数据库技术]
[操作系统][嘉宾聊天·在线访谈][活动集锦]
[精彩专题][Symantec专区][订阅IT技术周刊]
[开发论坛][网管论坛][安全论坛][数据库论坛]
[操作系统论坛][Sybase专区][IBM dW技术专区]
[病毒求助][病毒与漏洞播报][文档·源码下载]

讲解jython访问MySQL数据库的具体步骤

发布时间:2008.03.19 04:39     来源:赛迪网    作者:Alizze

【赛迪网-IT技术报道】

准备工作如下:

下载

http://jaist.dl.sourceforge.net/sourceforge/zxjdbc/zxJDBC-2.1.tar.gz

http://jaist.dl.sourceforge.net/sourceforge/jython/jython_installer-2.2.1.jar

http://mmmysql.sourceforge.net/dist/mm.mysql-2.0.4-bin.jar

安装(需要已经安装了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

(责任编辑:卢兆林)


[ 发表评论 ] 字体[  ] [ 打印 ] [ 进入博客 ] [ 进入论坛 ]  [ 推荐给朋友 ]
  相关文章
  客户需求反馈表
* 姓  名:
更多资料  了解方案  认识厂商
* 单位名称:
* 联系电话:
* 电子邮件:
  赛迪推荐  
  手机·资费 ·新品·导购·评测·手机资费·宽带
手机搜索  诺基亚 N73 MOTO Z6
  IT产品 ·笔记本·台式机·服务器·打印·投影
IT产品搜索 
  IT技术 ·开发·网管·安全·数据库·操作系统
  信息化 ·热点·专题·访谈·周刊·方案案例
· 工信部“三定”公布 总编制731名设24司局
· 北京发电子商务监管意见 营利性网店须办照
· 直播 08中国城市信息化高峰论坛 案例点评
· 烽火网络校园解决方案 移民安置信息管理系统
  IT博客 ·曾剑秋·项立刚·Java学习·网管
  IT技术论坛 ·开发·网管·安全·数据库·系统