简体   繁体   English

使用Java中的DataSource连接到mysql时出错

[英]Getting error on connecting to mysql with DataSource in Java

Getting this exception on trying to connect to mysql! 尝试连接mysql时遇到此异常!

Please don't mark this as duplicate, I tried everything in every thread I found on this, but nothing helped me. 请不要将此标记为重复,我在此发现的每个线程中都尝试了所有方法,但没有任何帮助。

Not using DataSources doesn't help either! 不使用数据源也无济于事! This is part of my plugin for the JTS3Servermod. 这是我的JTS3Servermod插件的一部分。

Exception in thread "Soulsbot" java.lang.NoClassDefFoundError: com/mysql/jdbc/jdbc2/optional/MysqlDataSource 线程“ Soulsbot”中的异常java.lang.NoClassDefFoundError:com / mysql / jdbc / jdbc2 / optional / MysqlDataSource

The error is in the first line of the code 错误在代码的第一行

I have installed the Drivers etc correctly added the newest libraries to my Workspace in eclipse. 我已经安装了Drivers等,在Eclipse中正确地将最新的库添加到了我的工作区中。

I am running this on Debian8. 我在Debian8上运行它。 On another User using spigot it worked. 在另一个使用spigot的用户上,它可以正常工作。

            MysqlDataSource dataSource = new MysqlDataSource();
            dataSource.setUser(username);
            dataSource.setPassword(password);
            dataSource.setServerName(host);

            connection = dataSource.getConnection();

Full error message: 完整的错误消息:

Exception in thread "Soulsbot" java.lang.NoClassDefFoundError: com/mysql/jdbc/jdbc2/optional/MysqlDataSource
    at net.mysticsouls.TeamSpeakBot.utils.NameUUIDUtils.connect(NameUUIDUtils.java:36)
    at net.mysticsouls.TeamSpeakBot.utils.Updater.start(Updater.java:11)
    at net.mysticsouls.TeamSpeakBot.TeamSpeakBot.activate(TeamSpeakBot.java:45)
    at de.stefan1200.jts3servermod.JTS3ServerMod.e(Unknown Source)
    at de.stefan1200.jts3servermod.JTS3ServerMod.b(Unknown Source)
    at de.stefan1200.jts3servermod.i.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.jdbc2.optional.MysqlDataSource
        at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 7 more

pom.xml from Maven: 来自Maven的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>TeamSpeakBotMaven</groupId>
  <artifactId>TeamSpeakBotMaven</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>
  </dependencies>
</project>

您需要将适当的jar添加到classpath。

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

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