简体   繁体   English

无法使用JDBC连接到MySQL数据库

[英]unable to connect to MySQL database using JDBC

I have this Java application in which I am now developing a module that can read and execute SQL statements from a file and output the results of the queries. 我有这个Java应用程序,现在正在其中开发一个模块,该模块可以从文件读取和执行SQL语句并输出查询结果。 If I launch the module via its main method, it works fine. 如果我通过其main方法启动该模块,则可以正常工作。 I give the path to the DB connection settings file (with the user, pass, port.. etc), the path to a file containing SQL queries and the name of the output file. 我给出了DB连接设置文件的路径(包括用户,通过,端口等),包含SQL查询的文件的路径以及输出文件的名称。 As I said, launched as stand alone, it works fine, connects with no problem, executes queries and outputs their results. 就像我说的那样,作为独立启动,它可以正常工作,毫无问题地连接,执行查询并输出结果。 If I integrate it in the application workflow, it fails to connect to the database, even if the credentials (ie, DB connection settings file) are the same. 如果将其集成到应用程序工作流程中,即使凭据(即数据库连接设置文件)相同,也无法连接到数据库。 I have tried to wrap it around in a thread, tried to search if I need to launch the connection asynchronously, but nothing helped. 我尝试将其包装在线程中,尝试搜索是否需要异步启动连接,但没有任何帮助。 The mysql-connector-java-5.1.28 is in the classpath, everything looks fine. mysql-connector-java-5.1.28在类路径中,一切看起来都很好。 I have also tried to set the autoReconnect (the commented line) but it also fails. 我也尝试设置了autoReconnect(注释行),但是也失败了。 This is the code to create the connection: 这是创建连接的代码:

    String url = "jdbc:mysql://"+server+":3306/?useCursorFetch=true";
//String url = "jdbc:mysql://"+server+":3306/?autoReconnect=true";

try {
    return DriverManager.getConnection(url, user, password);
} catch (SQLException e1) {
        Logging.outputStackTrace(e1);
        throw new RuntimeException("Cannot connect to DB server: " +  e1.getMessage());

with server = localhost. 与服务器=本地主机。 The user and pass are fine, sorry for repeating a third time, the connection is successful while launching it via the module's main method. 用户和pass都很好,对于第三次重复感到抱歉,通过模块的main方法启动连接时连接成功。 This is the stack trace I get while running the module via the application: 这是通过应用程序运行模块时获得的堆栈跟踪:

ERROR: An error occurred : Could not create connection to database server. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
Check the error log for details
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
java.lang.reflect.Constructor.newInstance(Unknown Source)
com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
com.mysql.jdbc.Util.getInstance(Util.java:386)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1015)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:975)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:920)
com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2575)
com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2311)
com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834)
com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
java.lang.reflect.Constructor.newInstance(Unknown Source)
com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:347)
java.sql.DriverManager.getConnection(Unknown Source)
java.sql.DriverManager.getConnection(Unknown Source)
org.application.utilities.DBConnection.connectToMySQL(DBConnection.java:144)
org.application.utilities.DBConnection.connect(DBConnection.java:107)
org.application.utilities.RunSQL.run(RunSQL.java:110)
org.application.workflow.Processing.extractData(Processing.java:201)
org.application.workflow.Processing.<init>(Processing.java:73)
org.application.gui.ApiGUI$2.actionPerformed(ApiGUI.java:349)
javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
javax.swing.DefaultButtonModel.setPressed(Unknown Source)
javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
java.awt.Component.processMouseEvent(Unknown Source)
javax.swing.JComponent.processMouseEvent(Unknown Source)
java.awt.Component.processEvent(Unknown Source)
java.awt.Container.processEvent(Unknown Source)
java.awt.Component.dispatchEventImpl(Unknown Source)
java.awt.Container.dispatchEventImpl(Unknown Source)
java.awt.Component.dispatchEvent(Unknown Source)
java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
java.awt.Container.dispatchEventImpl(Unknown Source)
java.awt.Window.dispatchEventImpl(Unknown Source)
java.awt.Component.dispatchEvent(Unknown Source)
java.awt.EventQueue.dispatchEventImpl(Unknown Source)
java.awt.EventQueue.access$200(Unknown Source)
java.awt.EventQueue$3.run(Unknown Source)
java.awt.EventQueue$3.run(Unknown Source)
java.security.AccessController.doPrivileged(Native Method)
java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
java.awt.EventQueue$4.run(Unknown Source)
java.awt.EventQueue$4.run(Unknown Source)
java.security.AccessController.doPrivileged(Native Method)
java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
java.awt.EventQueue.dispatchEvent(Unknown Source)
java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
java.awt.EventDispatchThread.pumpEvents(Unknown Source)
java.awt.EventDispatchThread.pumpEvents(Unknown Source)
java.awt.EventDispatchThread.run(Unknown Source)

Any ideas why this is happening? 任何想法为什么会这样? Is this a thread related issue? 这是与线程相关的问题吗? Can the event manager of the GUI have something to do with it? GUI的事件管理器可以和它有关吗? There are no multiple connections or concurrent connections to the database. 没有多个连接或并发连接到数据库。 Only once and it is properly closed when done. 仅一次,完成后将其正确关闭。 Via MySQL command line I cal also access my database, I made sure the server is on everytime, etc... Thank you. 通过MySQL命令行,我还可以访问我的数据库,确保服务器每次都在运行,等等。谢谢。

i think you forgot to mention database name 我认为您忘了提及数据库名称

String url = "jdbc:mysql://"+server+":3306/?useCursorFetch=true";

must be 一定是

String url = "jdbc:mysql://"+server+":3306/DB_NAME?useCursorFetch=true";

OK. 好。 Got it. 得到它了。 When I was running the SQL module via the application, I was setting the file encoding to ANSI via 通过应用程序运行SQL模块时,我通过以下方式将文件编码设置为ANSI:

System.setProperty("file.encoding", "ANSI");

which was not the case while running it stand alone. 独立运行时并非如此。 Apparently the DriverManager.getConnection(url, user, pass) does not set the character set on its own to make sure it sends requests in a compatible character encoding. 显然,DriverManager.getConnection(url,user,pass)不会自行设置字符集以确保其以兼容的字符编码发送请求。 Maybe that should be fixed in the java.sql package. 也许应该在java.sql包中解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM