zoukankan      html  css  js  c++  java
  • DbUtil

    DbUtil工具类

    public class DbUtil {
    	private static String url = "jdbc:mariadb://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC";
    	private static String username = "test";
    	private static String password = "test";
    
    	public static Connection getConnection() {
    		Connection conn = null;
    		try {
    			Class.forName("org.mariadb.jdbc.Driver");
    		} catch (ClassNotFoundException e) {
    			e.printStackTrace();
    		}
    		try {
    			conn = DriverManager.getConnection(url, username, password);
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}
    		return conn;
    	}
    
    }
    

    2.Dao类

    public class UserDao {
    
    	//查询所有
    	public List<User> findAll(){
    		 //创建连接
            Connection conn = DbUtil.getConnection();
            //创建SQL执行工具
            QueryRunner queryRunner = new QueryRunner();
            List<User> list = null;
            try {
                //执行SQL查询,并获取结果
            	//BeanListHandler 映射多个对象
                list = queryRunner.query(conn, "select * from user", new BeanListHandler<>(User.class));
            } catch (SQLException e) {
                e.printStackTrace();
            }
            //关闭数据库连接
            DbUtils.closeQuietly(conn);
            return list;
    	}
    
    	//添加
    	public void save(User user) {
    		Connection conn = DbUtil.getConnection();
            //创建SQL执行工具
            QueryRunner queryRunner = new QueryRunner();
            int rows = 0;
            try {
                //执行SQL插入
            	//返回受影响有多少行
                rows = queryRunner.update(conn, "INSERT INTO user(name, age,imgurl) VALUES(?,?,?)",
                		new Object[] {user.getName(),user.getAge(),user.getImgurl()});
                //new Object[] {user.getName(),user.getAge(),user.getImgurl()} 设置参数
            } catch (SQLException e) {
                e.printStackTrace();
            }
            //关闭数据库连接
            DbUtils.closeQuietly(conn);
    	}
    
    	//查找单个
    	public User findOne(int id) {
    		 //创建连接
            Connection conn = DbUtil.getConnection();
            //创建SQL执行工具
            QueryRunner queryRunner = new QueryRunner();
            User user = new User();
            try {
                //执行SQL查询,并获取结果   
            	//BeanHandler 映射成一个对象
            	user = queryRunner.query(conn, "select * from user where id=?", new BeanHandler<>(User.class),
            			new Object[] {id});
            } catch (SQLException e) {
                e.printStackTrace();
            }
            //关闭数据库连接
            DbUtils.closeQuietly(conn);
            return user;
    	}
    
    	//修改
    	public void update(User user) {
    		Connection conn = DbUtil.getConnection();
            //创建SQL执行工具
            QueryRunner queryRunner = new QueryRunner();
            int rows = 0;
            try {
                //执行SQL插入
                rows = queryRunner.update(conn, "update user set name=?,age=?,imgurl=? where id=?",
                		new Object[] {user.getName(),user.getAge(),user.getImgurl(),user.getId()});
                //new Object[] {user.getName(),user.getAge(),user.getImgurl()} 设置参数
            } catch (SQLException e) {
                e.printStackTrace();
            }
            //关闭数据库连接
            DbUtils.closeQuietly(conn);
    	}
    
    	//删除
    	public void delete(int id) {
    		Connection conn = DbUtil.getConnection();
            //创建SQL执行工具
            QueryRunner queryRunner = new QueryRunner();
            int rows = 0;
            try {
                //执行SQL插入
                rows = queryRunner.update(conn,"delete from user where id=?", new Object[] {id});
            } catch (SQLException e) {
                e.printStackTrace();
            }
            //关闭数据库连接
            DbUtils.closeQuietly(conn);
    	}
    }
    

      

  • 相关阅读:
    使用C#实现DHT磁力搜索的BT种子后端管理程序+数据库设计(开源)
    便携版WinSCP在命令行下同步文件夹
    ffmpeg (ffprobe)分析文件关键帧时间点
    sqlite删除数据或者表后,回收数据库文件大小
    ubuntu 20.04下 freeswitch 配合 fail2ban 防恶意访问
    ffmpeg使用nvenc编码的结论记录
    PC版跑跑卡丁车 故事模式 亚瑟传说章节 卡美洛庆典 2阶段 心灵之眼 攻略
    There was an error loading or playing the video
    Nvidia RTX Voice 启动报错修复方法
    火狐浏览器 关闭跨域限制
  • 原文地址:https://www.cnblogs.com/max-hou/p/12199119.html
Copyright © 2011-2022 走看看