JDBC初学,DAO模式

一、用eclipse连接mysql数据库:

要先添加一个文件:https://www.cnblogs.com/caiwenjing/p/8079227.html

二、开始写连接数据库的代码:

因为连接数据库的操作一般是公用的,所以写在叫做util的包里

像实体类一般写在叫做entity包里面

测试类(JUnit里讲解过怎么使用)写在叫做test的包里面

package com.util;

import java.sql.*;

public class ConnectionUtil {
    /**
     * 第一步:加载驱动
     * 第二步:链接数据库
     * 第三步:一定要关闭流
     * 第四步:测试是否连接成功
     */
    private static String DRIVER = "com.mysql.cj.jdbc.Driver";         // 数据库驱动
    private static String URL = "jdbc:mysql://localhost:3306/xxxy?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT";    // 访问数据库路径
                                                     //xxxy 是我连接的数据库名称
    private static String NAME = "root";                            // 数据库用户名
    private static String PASSWORD = "root";                        // 数据库密码
 
 
    public static Connection getConnection() {
        Connection connection = null;
        try {
            // 加载驱动
            Class.forName(DRIVER);
            // 连接数据库 
            connection = DriverManager.getConnection(URL, NAME, PASSWORD);
            return connection;
        } catch (Exception e) {
            return null;
        }
    }
 
 
    // 关闭流
    public static void closeConnection(Connection connection) {
        try {
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
 
    public static void closeStatement(Statement statement) {
        try {
            statement.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
 
    public static void closePreparedStatement(PreparedStatement pStatement) {
        try {
            pStatement.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
 
    public static void closeResultSet(ResultSet rs) {
        try {
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // 测试数据库是否链接成功
    /*public static void main(String[] args) {
        System.out.println("good"+getConnection());
    }*/
}

三、写成DAO模式的增删改查操作(写成这样代码更加规范!)

  DAO模式可以把实现数据库表的操作转化为对JAVA类的操作

/* read all data */
    public static List<Users> readDao() {
        String sql = "select * from Journalism";
        Connection connect = null;
        PreparedStatement ptmt = null;
        ResultSet rs = null;
        List<Users> lists = new ArrayList<Users>();
        try {
            connect = ConnectionUtil.getConnection();
            ptmt = connect.prepareStatement(sql);
            rs = ptmt.executeQuery();

            while (rs.next()) {

                Users user = new Users();
                user.setType(rs.getString("type"));
                user.setContent(rs.getString("content"));
                user.setTitle(rs.getString("title"));
                user.setAuthor(rs.getString("author"));
                user.setId(rs.getString("id"));
                user.setImg(rs.getString("img"));
                user.setToday(rs.getTimestamp("today"));

                lists.add(user);
            }
            return lists;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            ConnectionUtil.closeResultSet(rs);
            ConnectionUtil.closePreparedStatement(ptmt);
            ConnectionUtil.closeConnection(connect);
        }
    }

    
    /* delete the data */
    public static boolean deleteDao(Users user) {
        String sql = "delete from Journalism where id = ?";
        Connection connect = null;
        PreparedStatement ptmt = null;
        try {
            connect = ConnectionUtil.getConnection();
            ptmt = connect.prepareStatement(sql);
            ptmt.setString(1, user.getId());
            int i = ptmt.executeUpdate();
            return i > 0 ? true : false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            ConnectionUtil.closePreparedStatement(ptmt);
            ConnectionUtil.closeConnection(connect);
        }
    }

猜你喜欢

转载自www.cnblogs.com/1starfish/p/11488029.html