jdbc数据访问层和curd操作

本文主要介绍jdbc的CURD操作,包括Create、Update、Retrieve和Delete,并探讨数据访问层的设计,通过com.openlab.bean.Student实体类、JDBCUtils工具包以及StudentDao接口的实现(StudentDaoImpl和StudentDaoImpl2)来阐述如何进行数据库操作。
摘要由CSDN通过智能技术生成

一.curd操作

增删改查(curd):Create,Update,Retrieve,Delete

package com.openlab.test;

import com.openlab.util.JDBCUtils;

import java.sql.*;

public class Test02_curd {
    public static void main(String agrs[]){
//        create();
//        delete();
//        update();
//        select01();
        select02(804);
    }
//    INSERT INTO s_student VALUES(8,'root08','root08','男',8,'8559876234','西安市太白南路')
    public static void create(){
        JDBCUtils jdbcUtils = new JDBCUtils();
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            Class.forName(JDBCUtils.DRIVER);
            conn = DriverManager.getConnection(JDBCUtils.URL,JDBCUtils.USER,JDBCUtils.PASSWORD);
            String sql = "INSERT INTO s_student VALUES(?,?,?,?,?,?,?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,8);
            pstmt.setString(2,"root08");
            pstmt.setString(3,"root08");
            pstmt.setString(4,"男");
            pstmt.setInt(5,8);
            pstmt.setString(6,"8559876234");
            pstmt.setString(7,"西安市太白南路");
            int rows = pstmt.executeUpdate();
            System.out.println(rows);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }finally {
            jdbcUtils.close(null,pstmt,conn);
        }
    }
//    DELETE FROM s_student WHERE s_no = 8
    public static void delete(){
        JDBCUtils jdbcUtils = new JDBCUtils();
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            Class.forName(JDBCUtils.DRIVER);
            conn = DriverManager.getConnection(JDBCUtils.URL,JDBCUtils.USER,JDBCUtils.PASSWORD);
            String sql = "DELETE FROM s_student WHERE s_no = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,8);
            int rows = pstmt.executeUpdate();
            System.out.println(rows);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }finally {
            jdbcUtils.close(null,pstmt,conn);
        }
    }
    //UPDATE s_student SET s_no = 9, s_user = 'root09' , s_password='root09' WHERE s_no = 6
    public static void update() {
        JDBCUtils jdbcUtils = new JDBCUtils();
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            Class.forName(JDBCUtils.DRIVER);
            conn = DriverManager.getConnection(JDBCUtils.URL, JDBCUtils.USER, JDBCUtils.PASSWORD);
            String sql = "UPDATE s_student SET s_no = ?, s_user = ? , s_password=? WHERE s_no = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, 6);
            pstmt.setString(2, "root06");
            pstmt.setString(3, "root06");
            pstmt.setInt(4, 9);
            int rows = pstmt.executeUpdate();
            System.out.println(rows);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            jdbcUtils.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值