Java使用JDBC操作MySQL的基本实现

什么是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);
      • 2.DaoUtil(把加载驱动、获取链接对象、释放资源)封装成一个类
        •  
          • public class DaoUtil {
            • // 加载驱动
            • static {
              • try {
                • Class.forName("com.mysql.cj.jdbc.Driver");
              • } catch (ClassNotFoundException e) {
                • // TODO Auto-generated catch block
                • e.printStackTrace();
              • }
            • }
            • // 获取链接对象
            • 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();
                • }
              • }
              • if(statm != null) {
                • try {
                  • statm.close();
                • } catch (SQLException e) {
                  • // TODO Auto-generated catch block
                  • e.printStackTrace();
                • }
              • }
              • if(conn != null) {
                • try {
                  • conn.close();
                • } catch (SQLException e) {
                  • // TODO Auto-generated catch block
                  • e.printStackTrace();
                • }
              • }
            • }
      • 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;
            • }

          

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值