要连接MySQL数据库,首先需要上传MySQL JDBC驱动(mysql-connector-java-x.x.xx.jar)到项目中。在Java代码中使用以下方式建立连接:,,
`java,import java.sql.Connection;,import java.sql.DriverManager;,,public class Main {, public static void main(String[] args) {, String url = "jdbc:mysql://localhost:3306/yourDatabase";, String user = "yourUsername";, String password = "yourPassword";, , try {, Class.forName("com.mysql.cj.jdbc.Driver");, Connection connection = DriverManager.getConnection(url, user, password);, System.out.println("连接成功!");, } catch (Exception e) {, e.printStackTrace();, }, },},
``