Maven缺少MySQL依赖

时间:2018-11-05 18:21:21

标签: java mysql maven

我在Eclipse中有一个Maven项目(使用ubuntu),带有dbtest类,可以在MySQL中访问我的SQL表,而我正在使用Apache Maven 3.3.9和Java 1.8.0_181。

当我使用Eclipse运行并编译代码时,它运行良好(我确实有一个名为acm的数据库,其用户名和密码为“ root”),但是当我使用Maven命令创建一个Jar时:

  

mvn全新安装

我将其编译为以下错误:

java -cp target/maven-1.jar test.dbtest
start
Class-start
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/acm
End main

我遵循了https://stackoverflow.com/questions/1074869/find-oracle-jdbc-,并尝试了许多选项,例如Peter Enis answer或将编译范围添加到依赖项,但没有一个解决了问题。 此外,我还下载了mysql-connector-java jar(我尝试了许多版本)并将其添加到库路径中,但这无济于事,因此我将提取的jar添加到了路径中,问题仍然存在。 / p>

下面是dbtest类和我的POM文件。

package test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class dbtest  
{
public static void main(String[] args) throws SQLException 
{
    try
    {
    System.out.println("start");
    //Class.forName("com.mysql.jdbc.Driver");//.newInstance();
    System.out.println("Class-start");
    Connection  connection = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/acm", "root", "root");
    System.out.println("connection");
    Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
    System.out.println("statement");
    String sql = "SELECT * FROM article_102 limit 4";
    ResultSet rs = statement.executeQuery(sql);
    System.out.println("article_id | publication_id");
    while (rs.next())
        System.out.println(rs.getString("article_id") + ", " + rs.getString("publication_id"));
    statement.close();
    connection.close();
    }//try
            catch(Exception e){ System. out.println(e.toString());}
    System.out.println("End main");
    }//main

}

<?xml version="1.0" encoding="UTF-8"?>

<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>maven</groupId>
  <artifactId>maven</artifactId>
  <version>1</version>

  <name>maven</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
       <dependency>
       <groupId>mysql</groupId>
       <artifactId>mysql-connector-java</artifactId>
       <version>5.1.6</version>
    </dependency>

  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
      <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.7.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.20.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

注释,如果需要此行Class.forName("com.mysql.jdbc.Driver");//.newInstance();,我将其保留为注释(当我使用它时,我只得到了java.lang.ClassNotFoundException:com。 mysql.jdbc.Driver),因为有人说我应该这样做,而另一些人说它不会改变。

2 个答案:

答案 0 :(得分:0)

Class.forName("com.mysql.jdbc.Driver").newInstance();需要注册驱动程序,如6.1 Connecting to MySQL Using the JDBC DriverManager Interface中所述。

错误java.lang.ClassNotFoundException: com.mysql.jdbc.Driver表示在类路径中不存在包含com.mysql.jdbc.Driver类的JAR文件。修复最简单的方法是更改​​-cp值:

java -cp path/to/mysql-connector-java.jar:target/maven-1.jar test.dbtest

还有其他方法,例如将MANIFEST.MFjava -jar一起使用,或使用Maven Shade Plugin构建一个超级jar。

答案 1 :(得分:0)

将此插件添加到pom中并执行

java -jar your_project-jar-with-dependencies.jar

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>
                                test.dbtest
                            </mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </execution>
        </executions>
    </plugin>