找不到适合 jdbc:mysql/localhost:3306/world 的驱动程序 [英] No suitable driver found for jdbc:mysql/localhost:3306/world
问题描述
我是这方面编程的新手,我在驱动程序方面遇到了一些问题,有点:
I'm new at this area programming and I got some problems with driver, kinda:
当我直接在客户端上使用 MySql 时,它工作正常,问题是我无法与 MySql 建立连接 tomcat
MySql is working fine when I'm using it directly with client, problem is I can't make a connection tomcat with MySql
我将所有驱动程序都放在 WEB-INF/lib 中
I put all my drivers at WEB-INF/lib
使用 mysql 5.7、tomcat 9.1、java 8、IntellijIdea 2016.1.1
using mysql 5.7, tomcat 9.1, java 8, IntellijIdea 2016.1.1
java.sql.SQLException: 找不到适合 jdbc:mysql/localhost:3306/world 的驱动程序在 java.sql.DriverManager.getConnection(DriverManager.java:689)在 java.sql.DriverManager.getConnection(DriverManager.java:247)在 Db.main(Db.java:20)
java.sql.SQLException: No suitable driver found for jdbc:mysql/localhost:3306/world at java.sql.DriverManager.getConnection(DriverManager.java:689) at java.sql.DriverManager.getConnection(DriverManager.java:247) at Db.main(Db.java:20)
这是我的代码:
import java.io.Serializable;
import java.sql.*;
public class Db implements Serializable{
public static void main(String args[]){
Connection connection;
final String dbURL = "jdbc:mysql/localhost:3306/world";
final String username = "root";
final String pswd = "******";
try{
DriverManager.getDrivers();
connection = DriverManager.getConnection(dbURL,username,pswd);
Statement statement = connection.createStatement();
String query = "SELECT * FROM city WHERE Name LIKE 'New%'";
ResultSet result = statement.executeQuery(query);
while(result.next()){
int id = result.getInt("ID");
String name = result.getString("Name");
String cCode = result.getString("CountryCode");
String district = result.getString("District");
int pop = result.getInt("Population");
System.out.print("id = "+id+"</br>Name = "+name+
"</br>Code = "+cCode+"</br>District = "+district+
"</br>Population = "+pop+"</br>");
}
}catch (SQLException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
}
推荐答案
用于连接 MySQL 服务器的 JDBC URL 的一般格式如下,方括号 ([]) 中的项目是可选的:
The general format for a JDBC URL for connecting to a MySQL server is as follows, with items in square brackets ([ ]) being optional:
jdbc:mysql://[host1][:port1][,[host2][:port2]]...[/[database]][?propertyName1=propertyValue1[&propertyName2=propertyValue2]...]
以下是连接 URL 的简单示例:
Here is a simple example for a connection URL:
jdbc:mysql://localhost:3306/sakila?profileSQL=true
您的 URL jdbc:mysql/localhost:3306/world
在 mysql
之后缺少 :/
.
Your URL jdbc:mysql/localhost:3306/world
is missing a :/
after mysql
.
端口号默认为 3306
,所以你的 URL 应该是:
Port number defaults to 3306
, so your URL should be:
jdbc:mysql://localhost/world
这篇关于找不到适合 jdbc:mysql/localhost:3306/world 的驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!