利用JDBC连接mysql数据库,获取连接对象的通用格式

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);                      //获取数据库连接对象

原文地址:https://www.cnblogs.com/ljs-666/p/7679065.html