通过封装JdbcUtils类,实现EmisUtils.getConnection()方法连接数据库,方便进行以后的增删改查工作
添加依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.24</version>
</dependency>
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JdbcUtils {
public static Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
//jdbc:mysql://主机名或域名:端口/数据库名称
String url="jdbc:mysql:///?useUnicode=true&characterEncoding=utf8";
//用户名
String username="";
//密码
String password="";
Connection conn = DriverManager.getConnection(url, username, password);
if (conn!=null){
System.out.println("数据库连接成功! ");
}else {
System.out.println("数据库连接失败! ");
}
return conn;
}
}