java - java项目无法通过JDBC连接MYSQL,找不到com.mysql.cj.jdbc.Driver
问题描述
我正在尝试使用 MYSQL Connector/j8.0 将我的项目连接到 MYSQL 数据库。我尝试了许多不同的技术,但仍然无法连接。在我遇到错误之前,但我能够使用 try and catch 对它们进行排序,而且我认为我已经正确安装了连接器。请帮忙。
这是我的代码(我遵循 MYSQL 文档:https ://dev.mysql.com/doc/connector-j/8.0/en/connector-j-usagenotes-connect-drivermanager.html#connector-j-examples-connection- drivermanager ): 导入 java.sql.*;
public class GSM_Mobile_main {
// this is start of the application
static Connection conn = null;
public static void main(String[] args) {
try {
// The newInstance() call is a work around for some
// broken Java implementations
Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
try {
conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/GSM_Mobile? useSSL=false","root","Capris55");
// Do something with the Connection
Statement stmt = null;
ResultSet rs = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM receipts");
// Now do something with the ResultSet ....
while(rs.next()){
System.out.println("ref ID="+rs.getInt("referenceNumber")+", Name="+rs.getString("customerName"));
}
}
catch (SQLException ex){
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
finally {
// it is a good idea to release
// resources in a finally{} block
// in reverse-order of their creation
// if they are no-longer needed
if (rs != null) {
try {
rs.close();
} catch (SQLException sqlEx) { } // ignore
rs = null;
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException sqlEx) { } // ignore
stmt = null;
}
}
} catch (SQLException ex) {
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
} catch (Exception ex) {
// handle the error
}
}
}
解决方案
推荐阅读
- node.js - 连接到 MongoDB 时,期望在箭头函数的末尾返回一个值
- reactjs - 如何使用 Amplify 或 RNFetchBlob 将视频上传到 S3?
- javascript - 从可读流中获取 Uint16
- javascript - 通过 props.children 映射,查找特定子组件,并在有条件地包装选中的子组件时返回所有内容
- python - anaconda3自带的pip真的是pip3吗?
- java - 确定当前正在使用哪个 NetworkInterface
- c# - 为什么 Object.Equals 的参数不是“in”(输入)?
- inner-classes - 不能将外部类作为内部类c ++中的成员对象
- animation - 如何在 svg on loop 中为动画重绘对象?
- c# - uwp xbinding 因转换器而失败