我使用Postgres 和H2 而不是MySQL。所以我不确定你的问题可能是关于 MySQL 的。但是我现在能够使用在运行时提供的 MySQL 驱动程序创建一个新项目。请继续阅读以遵循我的示例。
Maven 驱动的项目示例
如今的 Java 项目通常由 dependency management 和 build automation 工具管理,例如 Maven、Gradle 或 Ivy。
我使用 Maven Quickstart Archetype 开始了一个新的 Java 项目。
在该原型提供的POM file 中,我做了两处更改:
- 将
<maven.compiler.source> 和 <maven.compiler.target> 从 1.7 更改为 11 以使用 Java 11,即当前的 long-term support 版本。
- 为您提到的 JDBC 驱动程序添加了一个依赖项:
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
然后我在App 类的main 方法中添加了一行。
try
{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Found class for JDBC driver.");
}
catch ( ClassNotFoundException e )
{
e.printStackTrace();
}
System.out.println( "Hello World!" );
当我运行它时,没有抛出异常。所以似乎成功找到了这个类。
完整的 POM:
<?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>work.basil.example</groupId>
<artifactId>jdbc-driver-in-jar</artifactId>
<version>1.0-SNAPSHOT</version>
<name>jdbc-driver-in-jar</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>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://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.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.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>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
完整的应用类:
package work.basil.example;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
try
{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Found class for JDBC driver.");
}
catch ( ClassNotFoundException e )
{
e.printStackTrace();
}
System.out.println( "Hello World!" );
}
}
一些注意事项:
我在控制台输出中注意到特定的 JDBC driver 已过时。您应该调查一下现在要使用的正确 JDBC 驱动程序。
加载类com.mysql.jdbc.Driver'. This is deprecated. The new driver class is com.mysql.cj.jdbc.Driver'。驱动通过SPI自动注册,一般不需要手动加载驱动类。
Class.forName 是加载 JDBC 驱动程序的老式方法,不再需要。几年前,JDBC 驱动程序加载得到了很大改进。今天的 JDBC 驱动程序是通过 Java 的 Service Provider Interface (SPI) 工具加载的。但是 Class.forName 代码可能仍然有助于探索您的这个问题,以验证可以在您的 classpath 上找到该课程。
通常最好使用DataSource 来获取数据库连接。您的 JDBC 驱动程序可能提供了DataSource 的实现。此类将保存与数据库服务器建立连接所需的信息,例如查找数据库服务器的地址,以及用于向数据库服务器进行身份验证的用户名和密码。