我是这个领域编程的新手,我在驱动程序方面遇到了一些问题,有点:
当我直接与客户端一起使用时,MySql 工作正常,问题是我无法将 tomcat 与 MySql 连接
我将所有驱动程序放在 WEB-INF/lib 中
使用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)
所以这是我的代码:
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 https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html is:
连接 MySQL 服务器的 JDBC URL 的一般格式如下,方括号 ([ ]) 中的项目是可选的:
jdbc:mysql://[host1][:port1][,[host2][:port2]]...[/[database]][?propertyName1=propertyValue1[&propertyName2=propertyValue2]...]
以下是连接 URL 的简单示例:
jdbc:mysql://localhost:3306/sakila?profileSQL=true
您的网址jdbc:mysql/localhost:3306/world
缺少一个:/
after mysql
.
端口号默认为3306
,所以你的 URL 应该是:
jdbc:mysql://localhost/world
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)