| 導購 | 订阅 | 在线投稿
分享
 
 
 

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

來源:互聯網  2008-06-01 02:20:54  評論

准備工作如下:

下載

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

准備工作如下: 下載 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
󰈣󰈤
王朝萬家燈火計劃
期待原創作者加盟
 
 
 
>>返回首頁<<
 
 
 
 
 熱帖排行
 
 
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有