无法连接到数据库的导出的可运行 JAR 文件。

huangapple go评论85阅读模式
英文:

Exported Runnable Jar - Unable to connect to database

问题

朋友们,请提供一个解决方案

我已经使用Eclipse IDE在Ubuntu 20.04操作系统中完成了一个使用Java Swing和Maven的项目,并使用了MySql数据库。将项目导出为可运行的JAR文件(启动配置设置为我的主函数类,库处理中所选的单选按钮是将所需库提取到生成的JAR文件中)。

然后,我在Windows 10操作系统中打开了这个可运行的JAR文件。双击后主窗口打开,我只能从主窗口的菜单栏中打开2个窗口,而这两个窗口类没有SQL连接。所有其他窗口都有数据库连接,但无法打开。

在Windows 10中,我安装了MySql Server 8.0.22(使用传统身份验证方法保留MySql 5.x兼容性)和Connector J 8.0.22,我可以使用查询浏览器访问我的数据库。

我在项目中使用的是mysql-connector-java 8.0.22的Maven依赖。我的连接类如下所示。

public class MysqlConnector {
    Connection con = null;

    public static Connection dbConnector() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306", "sarams", "password");
            return con;
        } catch (Exception e) {
            return null;
        }
    }
}
英文:

Friends please suggest a solution

I have completed a java swing maven project using eclipse IDE and MySql database in Ubuntu 20.04 OS. Exported the project as Runnable jar (Launch configuration as my main function class and library handling radio button selected is extract required libraries into generated jar).

The runnable jar then, I have opened in Windows 10 OS. By double click the main window opens and I can open only 2 windows from the menu bar of the main window and these two window class are not having sql connection. All other windows are having database connection and are not able to open.

In widows 10 I have installed MySql server 8.0.22 (Using legacy authentication method Retain MySql 5.x Compatibility)and Connector j 8.0.22 and I am able to access my database using Query Browser.

I am using maven dependency of mysql-connector-java 8.0.22 in my Project. And my connection Class is hereunder.`


public class MysqlConnector {
	Connection con=null;
	public static Connection dbConnector() {
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
	        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306","sarams", "password");
	        return con;
	}
		catch (Exception e) {
			return null;
			
		}
			 
		}
}     

答案1

得分: 0

<!-- language: java -->

首先确保您已经安装并将所有驱动程序添加到Maven中
> 使用void的示例

```java
public class MysqlConnector {

    /**
     * 连接到示例数据库
     */
    public static void connect() {
        Connection con = null;
        try {
           // 数据库参数,指明数据库的存储位置,以便访问它
            String url = "jdbc:mysql://localhost:3306/sarams";
            String user = "用户名";
            String password = "密码";
           
            // 创建与数据库的连接
            con = DriverManager.getConnection(url, user, password);
    
            System.out.println("已建立与MySQL的连接。");
    
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        } finally {
            try {
                if (con != null) {
                    con.close();
                }
            } catch (SQLException ex) {
                System.out.println(ex.getMessage());
            }
        }
    }
    
    // 建立连接
    public static void main(String[] args) {
        connect();
    }

使用Connection的示例
<!-- language: java -->

private Connection connect() {
    // MySQL连接字符串
    String url = "jdbc:mysql://localhost:3306/sarams";
    String user = "用户名";
    String password = "密码";
    Connection con = null;
    try {
        con = DriverManager.getConnection(url, user, password);
    } catch (SQLException e) {
        System.out.println(e.getMessage());
    }
    return con;
}

<details>
<summary>英文:</summary>

&lt;!-- language: java --&gt;

First make sure you have all the _drivers installed_ and added to the maven
&gt; Example Using void

public class MysqlConnector {

/**
 * Connect to a sample database
 */
public static void connect() {
    Connection con = null;
    try {
       // db parameters, where the database is being stored, so it could access it
        String url = &quot;jdbc:mysql://localhost:3306&quot;,&quot;sarams&quot;, &quot;password&quot;;
       
        // create a connection to the database
        con = DriverManager.getConnection(url);

        System.out.println(&quot;Connection to mysql has been established.&quot;);

    } catch (SQLException e) {
        System.out.println(e.getMessage());
    } finally {
        try {
            if (con != null) {
                con.close();
            }
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
    }
}

// establishing connection
public static void main(String[] args) {
    connect();
}
Example Using Connection
&lt;!-- language: java --&gt;

private Connection connect() {
// MySQL connection string
String url = "jdbc:mysql://localhost:3306","sarams", "password";
Connection con = null;
try {
con = DriverManager.getConnection(url);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return con;
}


</details>



# 答案2
**得分**: 0

问题已解决,只是一个Java冲突。使用JDK 11进行编译,并尝试运行JRE 1.8。移除了JRE并安装了JDK 11,问题得以解决。

<details>
<summary>英文:</summary>

The problem was solved and it was just a java conflict. Compiled using jdk 11 and tried to run JRE 1.8. removed JRE and installed JDK 11 and the problem was solved.


</details>



huangapple
  • 本文由 发表于 2020年10月27日 18:35:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64552594.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定