gpt4 book ai didi

java - 无法使用 Jython 和 mysql.connector 模块连接到 mysql 数据库

转载 作者:行者123 更新时间:2023-11-29 10:54:15 26 4
gpt4 key购买 nike

我正在创建一个脚本,它将从 mysql 服务器获取数据并使用 Jasypt 解密文本。这是一个用于加密/解密文本的java库。所以我必须使用Jython来做到这一点.

我能够从 Jasypt 库导入 java 类。我还可以将 mysql.connector 导入 Jython 脚本。但脚本无法连接到 mysql 服务器。

这是我正在尝试的代码

import sys
SYSPATH = ['/usr/local/lib/python2.7/site-packages', '/data/packages/jasypt-1.9.2.jar']
[sys.path.append(p) for p in SYSPATH]
import mysql.connector

# adding this to avoid "AttributeError: socket.SOL_TCP does not exist" listed at https://github.com/pika/pika/issues/67
import socket
socket.SOL_TCP = socket.IPPROTO_TCP

# This line is giving error "TypeError: unpack_from(): 2nd arg can't be coerced to String"
con = mysql.connector.connect(user='username', password='password', host='192.168.123.123', database='users')

最后一行给了我错误

 File "2017-04-05-get-bank-sms.py", line 11, in createUserIdUuidMap
con = mysql.connector.connect(user='username', password='password', host='192.168.123.123', database='users')
File "/usr/local/lib/python2.7/site-packages/mysql/connector/__init__.py", line 179, in connect
return MySQLConnection(*args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/mysql/connector/connection.py", line 95, in __init__
self.connect(**kwargs)
File "/usr/local/lib/python2.7/site-packages/mysql/connector/abstracts.py", line 719, in connect
self._open_connection()
File "/usr/local/lib/python2.7/site-packages/mysql/connector/connection.py", line 207, in _open_connection
self._do_handshake()
File "/usr/local/lib/python2.7/site-packages/mysql/connector/connection.py", line 99, in _do_handshake
packet = self._socket.recv()
File "/usr/local/lib/python2.7/site-packages/mysql/connector/network.py", line 235, in recv_plain
payload_len = struct.unpack_from(
File "/usr/local/lib/python2.7/site-packages/mysql/connector/network.py", line 235, in recv_plain
payload_len = struct.unpack_from(
TypeError: unpack_from(): 2nd arg can't be coerced to String

我正在尝试的可能吗?

最佳答案

根据其他相关问题,最常见的方法似乎是使用“MySQL Connector/J”(JDBC 驱动程序)而不是“MySQL Connector/Python”。我可以确认这个 Jython 代码可以工作...

from com.ziclix.python.sql import zxJDBC

connectionUrl = "jdbc:mysql://localhost:3307"
cnxn = zxJDBC.connect(
connectionUrl,
"root",
"mypassword",
"com.mysql.jdbc.Driver")
crsr = cnxn.cursor()
crsr.execute("SHOW DATABASES")
rows = crsr.fetchall()
print(rows)

...当我使用以下批处理文件运行它时:

SET CLASSPATH=C:\Users\gord\.m2\repository\mysql\mysql-connector-java\5.1.41\mysql-connector-java-5.1.41.jar
C:\jython2.7.0\bin\jython mysql_test.py

关于java - 无法使用 Jython 和 mysql.connector 模块连接到 mysql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43257145/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com