本地mysql驱动程序在哪里找
时间: 2023-09-28 10:07:35 浏览: 43
你可以从 MySQL 官方网站上下载 MySQL Connector/J 驱动程序。你需要选择适用于你的操作系统和 MySQL 版本的驱动程序。下载后,将驱动程序文件添加到你的项目中,并在代码中加载驱动程序。下面是一个 Java 代码示例:
```
import java.sql.*;
public class Example {
public static void main(String[] args) {
try {
// 加载 MySQL 数据库驱动程序
Class.forName("com.mysql.jdbc.Driver");
// 创建数据库连接
String url = "jdbc:mysql://localhost:3306/mydatabase";
String username = "root";
String password = "mypassword";
Connection conn = DriverManager.getConnection(url, username, password);
// 执行 SQL 查询
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM mytable");
// 处理查询结果
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
System.out.println("id: " + id + ", name: " + name);
}
// 关闭数据库连接
rs.close();
stmt.close();
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
```
请注意,上述示例中的驱动程序类名为 "com.mysql.jdbc.Driver",这是 MySQL Connector/J 驱动程序的默认类名。如果你使用的是其他驱动程序,请将类名更改为相应的名称。