程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> 關於MYSQL數據庫 >> 講解jython訪問MySQL數據庫的具體步驟_MySQL教程

講解jython訪問MySQL數據庫的具體步驟_MySQL教程

編輯:關於MYSQL數據庫

准備工作如下:

下載

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved