什么是JDBC:
- A:Java Database Connectivity:Java访问数据库的解觉方案
- B:JDBC定义了一套标准接口即访问数据库的通用API不同的数据库厂商根据各自数据库的特点去实现这些接口
- C:JDBC希望用相同的方式访问不同的数据库让具体的数据库操作与数据库厂商实现无关;从而在不同数据库之间轻易的进行切换。
-
JDBC相关类与接口
-
1.驱动管理类:DriverManager
- 1:加载驱动:
- Class.forName("com.mysql.cj.jdbc.Driver")
-
2.连接接口:Connection
- 1:获取链接对象
- Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/xxxxl?serverTimezone=GMT", "xxx", "xxxx");
-
3.语句对象接口:Statement
- 获取sql语句执行对象
- Statement statm = conn.createStatement();
-
4.结果集接口:ResultSet
- 1:增删改:
- 执行sql语句
- int ret = statm.executeUpdate(sql);
- 2:查:
- 执行sql语句
- ResultSet rs = stat.executeQuery(sql);
- if(r!=null) {
- //解析ResultSet
- while(r.next()){
- int id =r.getInt("sid");
- String name=r.getString("sname");
- Date d=r.getDate("birthday");
- String sex=r.getString("ssex");
- int classid=r.getInt("classid");
- System.out.print(name+" "+id+" "+d+" "+sex+" "+classid);
- 3:释放资源:
- rs.close();
- statm.close();
- conn.close();
-
JDBC的三层架构:
- 5.JDBC的三层架构(封装)
- dao层(数据访问层):将数据库中的数据拿出来或存进去(以Student为例)
- 1.IStudentDao(接口)
-
- public interface IStudentDao {
- // 新增
- public int addStudent(Student s);
- // 删除
- public int delStudent(int sid);
- // 修改
- public int updateStudent(Student s);
- // 查询
- public List findAllStudent();
- public Student findStudentBySid(int sid);
- public interface IStudentDao {
-
- 2.DaoUtil(把加载驱动、获取链接对象、释放资源)封装成一个类
-
- public class DaoUtil {
- // 加载驱动
- static {
- try {
- Class.forName("com.mysql.cj.jdbc.Driver");
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- try {
- }
- // 获取链接对象
- public static Connection getConn() {
- Connection conn = null;
- try {
- conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/myschool?serverTimezone=GMT", "root", "123456");
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return conn;
- }
- // 释放资源
- public static void closeSource(Connection conn, Statement statm, ResultSet rs) {
- if(rs != null) {
- try {
- rs.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- try {
- }
- if(statm != null) {
- try {
- statm.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- try {
- }
- if(conn != null) {
- try {
- conn.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- try {
- }
- if(rs != null) {
- }
- public class DaoUtil {
-
- 3.BaseDao(把增删改查共有的封装成一个类)
-
- public class BaseDao {
- protected Connection conn;
- protected Statement statm;
- protected ResultSet rs;
- // 增 删 改
- protected int update(String sql) {
- int ret = 0;
- try {
- conn = DaoUtil.getConn();
- statm = conn.createStatement();
- ret = statm.executeUpdate(sql);
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally {
- DaoUtil.closeSource(conn, statm, rs);
- }
- return ret;
- }
- // 查询
- protected ResultSet query(String sql) {
- ResultSet rs = null;
- try {
- conn = DaoUtil.getConn();
- statm = conn.createStatement();
- rs = statm.executeQuery(sql);
- } catch (Exception e) {
- // TODO: handle exception
- }
- return rs;
- }
- public class BaseDao {
-
- 1.IStudentDao(接口)
- dao层(数据访问层):将数据库中的数据拿出来或存进去(以Student为例)