实习日记--day3

完整增删改查程序demo

把简单的事情做好,就是不简单。
增删改查应该算是数据库里最最基本的操作了,虽然简单,但是如果能熟练掌握这四个操作,绝对能应付很大一部分的工作需求。这里给出的一个demo基本没有什么实用性可言,但是其中代码的编排还是值得借鉴学习的,也算是抛砖引玉吧,废话不多说,直接整活。

工程目录

完整工程目录如下
在这里插入图片描述
其中bean中放的是实体类,楼主用的表还是前一次的"jdbc_user"表,所以这里的实体类就是"User"类。dao层也叫数据访问层,其中的DBDao类实现的是对数据库数据的访问。util是工具包,DBUtil是数据库工具包,主要包含了需要经常使用的连接和关闭操作,封装静态的工具类可以提高程序的效率,也降低了代码的冗余。

实体类

一般的实体类包括以下的四个部分
1)属性
2)构造方法
3)getter/setter
4)toString()

public class User {
    private int id;
    private String username;
    private String password;

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

dao层

其中的四个方法可以根据实际的需要自行完善,楼主只给出了一个简单的demo

import com.lee.bean.User;
import com.lee.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class DBDao {

    public void Insert(){//添加
        Connection connection=null;
        PreparedStatement statement=null;
        Scanner sc=new Scanner(System.in);
        String username=sc.nextLine();
        String password=sc.nextLine();
        int id;
        try {
            //1.加载驱动和创建连接
            connection=DBUtil.getConnection();
            //2.写sql语句
            String sql="insert into jdbc_user values(null,?,?)";
            //3.执行sql语句
            statement=connection.prepareStatement(sql);
            statement.setString(1,username);
            statement.setString(2,password);
            statement.executeUpdate();
            System.out.println("数据插入成功");
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //4.关闭连接
            DBUtil.close(null,statement,connection);
        }
    }

    public void Delete() {//删除
        Connection connection=null;
        PreparedStatement statement=null;
        Scanner sc=new Scanner(System.in);
        int id=sc.nextInt();
        try {
            //1.加载驱动和创建连接
            connection=DBUtil.getConnection();
            //2.写sql语句
            String sql="delete from jdbc_user where id=?";
            //3.执行sql语句
            statement=connection.prepareStatement(sql);
            statement.setInt(1,id);
            statement.executeUpdate();
            System.out.println("数据删除成功");
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //4.关闭连接
            DBUtil.close(null,statement,connection);
        }
    }

    public void Update(){//修改
        Connection connection=null;
        PreparedStatement statement=null;
        Scanner sc=new Scanner(System.in);
        String username=sc.nextLine();
        String password=sc.nextLine();
        try {
            //1.加载驱动和创建连接
            connection=DBUtil.getConnection();
            //2.写sql语句
            String sql="update jdbc_user set password=? where username=?";
            //3.执行sql语句
            statement=connection.prepareStatement(sql);
            statement.setString(1,password);
            statement.setString(2,username);
            statement.executeUpdate();
            System.out.println("数据修改成功");
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //4.关闭连接
            DBUtil.close(null,statement,connection);
        }
    }

    public List<User> Select() {//查询
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        List<User> userList=new ArrayList<>();
        try {
            //1.加载驱动和创建连接
            connection= DBUtil.getConnection();
            //2.写sql语句
            String sql="select * from jdbc_user";
            //3.执行sql语句
            statement = connection.prepareStatement(sql);
            resultSet =statement.executeQuery();
            //查询结果存入userList中
            while(resultSet.next()){
                int id=resultSet.getInt(1);
                String username=resultSet.getString(2);
                String password=resultSet.getString(3);
                User user=new User(username,password);
                user.setId(id);
                userList.add(user);
            }
            System.out.println(userList);
            System.out.println("数据查询成功成功");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //4.关闭连接
            DBUtil.close(resultSet,statement,connection);
        }
        return userList;
    }

    public static void main(String[] args) {
        DBDao test=new DBDao();
        //test.Insert();//插入测试
        //test.Delete();//删除测试
        //test.Update();//修改测试
        //test.Select();//查询测试
    }
}

工具类

(记得更换自己的数据库密码)

import java.sql.*;

public class DBUtil {
    public static Connection getConnection(){
        Connection connection=null;
        try {
            //1.加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.创建连接
            connection = DriverManager.getConnection
                    ("jdbc:mysql://127.0.0.1:3306/jdbc?useSSL=true&"+
                            "characterEncoding=utf-8&user=root&password=******");//请换成自己数据库的密码
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("创建连接成功");
        return connection;
    }

    public static void close(ResultSet resultSet, PreparedStatement statement,Connection connection){
        //关闭资源
        if(resultSet!=null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(statement!=null)
        {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(connection!=null)
        {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

SSM框架基础

(百度划水了,搭建过程要等明天再说吧,手动狗头。今天实在是肝不动了,昨天晚上没睡好,感觉今天状态很差)
既然是英文的缩写,那就来看一看每个字母的含义,具体如下:

  • 第一个"S"表示Spring 。Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。 简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。
  • 第二个"S"表示SpringMVC。Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring MVC 分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。
  • "M"表示MyBatis。MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。MyBatis是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQL Maps和Data Access Objects(DAO)MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java 的POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。(引用来源

了解众生,就是为了原谅一切。(昨晚的失眠大概也就是这个了)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值