[英]java.sql.SQLException: No suitable driver found for jdbc
I am attempting to run some queries using JDBC and keep getting this error: 我试图使用JDBC运行一些查询,并不断收到此错误:
Exception in thread "main" java.lang.IllegalStateException: error
at com.mycompany.app.App.writer(App.java:195)
at com.mycompany.app.App.main(App.java:19)
Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/dbname
here is the relevant part of my code: 这是我的代码的相关部分:
public class App {
writer();
}
public static void writer() {
String url = "jdbc:mysql://localhost:3306/dbname
String username = "root";
String password = "password";
try (Connection connection = DriverManager.getConnection(url, username, password)) {
Statement st = connection.createStatement();
ResultSet r= st.executeQuery("insert query here");
} catch (SQLException e) {
throw new IllegalStateException("error");
}
}
}
When I run it through Intellij Idea it works, but I need it to run on a server running Centos. 当我通过Intellij Idea运行它时,它可以工作,但是我需要它在运行Centos的服务器上运行。
I have tried running it with this commands: 我尝试使用以下命令运行它:
javac -cp "filepath/mysql-connector-java-5.1.35-bin.jar" App.java
java -cp ".filepath/mysql-connector-java-5.1.35-bin.jar" App
I have tried running it using maven, including 我尝试使用maven运行它,包括
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
in pom.xml and still get the same error 在pom.xml中,仍然出现相同的错误
I have looked through many articles online (and stack questions) and still can't find the solution. 我在网上浏览了许多文章(以及有关堆栈的问题),但仍然找不到解决方案。
The server is running CentoOS 6.6 and the database is running locally. 服务器正在运行CentoOS 6.6,数据库正在本地运行。
I am using: 我在用:
java version "1.8.0_45" Java(TM) SE Runtime Environment (build 1.8.0_45-b14) Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode) Java版本“ 1.8.0_45” Java™SE运行时环境(内部版本1.8.0_45-b14)Java HotSpot(TM)64位服务器VM(内部版本25.45-b02,混合模式)
Entries on the classpath on Unix-like operating systems must be separated by :
. 在类Unix操作系统中的类路径条目必须分开
:
。 Add a :
between .
在之间添加一个
:
.
(which indicates the current directory) and the path of the jar: (指示当前目录)和jar的路径:
java -cp .:filepath/mysql-connector-java-5.1.35-bin.jar App
Alternatively, you may place your mysql-connector-java-5.1.35-bin.jar
to the following directory - 或者,您可以将
mysql-connector-java-5.1.35-bin.jar
放在以下目录中-
JAVA_HOME/jre/lib/ext
If you place the jar
in this directory then you don't have to add it to classpath
by using command. 如果将
jar
放在此目录中,则不必使用命令将其添加到classpath
。
You can try loading the driver class using class.forName("qualified name of driver class")
. 您可以尝试使用
class.forName("qualified name of driver class")
加载驱动class.forName("qualified name of driver class")
。 This should resolve the issue if you're not facing the issue of class path with maven. 如果您没有遇到Maven的类路径问题,这应该可以解决该问题。 Have a look at this where the index tag was removed from pom and it worked.
看一看这个地方从POM移除索引标记和它的工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.