jdbc mysql 函数 慢,用MySQL的JDBC真的很慢,不知道为什么

博主遇到的问题是Java程序与MySQL数据库之间的连接速度缓慢。尝试了不同地点和数据库连接方式,包括连接到外部服务器和附近的PC,但速度仍然慢。SQL查询涉及到关键词匹配和优先级排序。尝试使用Apache Commons DBCP进行数据库连接池管理,但未见明显改善。解决方案可能在于优化数据库连接设置或使用连接池技术提高效率。
摘要由CSDN通过智能技术生成

I have a problem with a really slow connection between my Java code and a MySQL Database. I don't know where the bottle neck is.

My program is more or less a chatbot. The user types something in, my program splits the sentence into words and sends it word per word to the database. If it finds something there, the user gets an output.

The database is on an external Server, but I also tried to connect to a pc next to me. Both is slow.

I tried the connection once at another place then where I normally work and there it was fast, most of the time.

My SQL Code:

SELECT info.INFORMATION FROM INFORMATION info, INFO_SCHLUESSEL sch

WHERE LCASE(sch.SCHLUESSELWORT) LIKE '" + input + "%' AND info.ID_INFO = sch.ID_INFO

Order BY info.PRIORITAET DESC LIMIT 1;

(just remembered, if it helps to understand the sql code:

schluessel = key

Schluesselwort = key word

prioritaet = priority)

My Java Database Code is more or less standard stuff:

String driver = "com.mysql.jdbc.Driver";

String dbase = "jdbc:mysql://bla";

String dbuser = "bla";

String dbpw = "bla";

Class.forName(driver);

Connection con = DriverManager.getConnection(dbase, dbuser, dbpw);

Statement stmt = con.createStatement();

ResultSet rs = stmt.executeQuery(query);

while (rs.next())

{

ergebnis = rs.getString("info.INFORMATION");

}

rs.close();

stmt.close();

con.close();

edit:

I have tried this DBCP for a while now, and I can't seem to get it to work. It seems to be as slow as the old connection. This is the example provided by the website that I use:

GenericObjectPool connectionPool = new GenericObjectPool(null);

ConnectionFactory connectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://bla", "bla", "bla");

PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true);

PoolingDriver driver = new PoolingDriver();

driver.registerPool("example",connectionPool);

Connection conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:example");

解决方案

I suspect that it's the connection setup that is causing the problem. It would be worth timing how long this takes:

Connection con = DriverManager.getConnection(dbase, dbuser, dbpw);

and if so, check out Apache Commons DBCP, which allows you to pool database connections.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值