Eclipse连接MySql
import java.sql.Connection;
import java.sql.DriverManager;
// 数据库的用户名与密码,需要根据自己的设置
private String dbUser=“root”;
private String dbPassword=“123456”;
//MySQL 8.0 以下版本 - JDBC 驱动名及数据库 URL
private String jdbcName=“com.mysql.jdbc.Driver”;
private String dbUrl=“jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=utf8”;
private Connection connection =null;
public Connection getConnection()
{
try {
// 注册 JDBC 驱动
Class.forName(jdbcName);
connection=DriverManager.getConnection(dbUrl, dbUser, dbPassword);
System.out.println(“数据连接成功”);
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println(“数据连接失败”);
e.printStackTrace();
}
return connection;
}
public static void main(String[] args) {
DbUtil dbUtil =new DbUtil();
dbUtil.getConnection();
}