String driverName="com.mysql.jdbc.Driver"; //驱动程序名
String userName="root"; //数据库用户名
String userPwd="123456"; //密码
String dbName="student"; //数据库名
String url1="jdbc:mysql://localhost:3306/"+dbName;
String url2="?user="+userName+"&password="+userPwd;
String url3="&useUnicode=true&characterEncoding=UTF-8";
String url=url1+url2+url3; //形成带数据库读写编码的数据库连接字
Class.forName(driverName); //加载并注册驱动程序
Connection conn=DriverManager.getConnection(url); //获取数据库连接对象
利用JDBC连接mysql数据库,获取连接对象的通用格式
![](https://img2024.cnblogs.com/blog/35695/202403/35695-20240328092537879-1189167464.jpg)