如何将常用的jdbc方法封装起来???

你是否还在为每次新建项目连接数据库而烦恼???(教你一次代码,简单完成每次连接)

1.建立maven项目

还没下载安装或者不会建立maven项目的可以看这里哦:maven的下载安装与配置环境变量!!!(全网最详细)_明天更新的博客-CSDN博客

2.编写配置文件。

XML 复制代码
  <dependencies>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>4.0.1</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.servlet.jsp</groupId>
                <artifactId>jsp-api</artifactId>
                <version>2.2</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>
            <!-- com.mysql/mysql-connector-j -->
            <dependency>
                <groupId>com.mysql</groupId>
                <artifactId>mysql-connector-j</artifactId>
                <version>8.1.0</version>
            </dependency>
        </dependencies>

3.在项目的src/main/resources文件下建立db.properties文件,并写入一下代码。

XML 复制代码
db.driver=com.mysql.cj.jdbc.Driver
db.url=jdbc:mysql:/book
db.username=root
db.password=

4.编写工具类。

java 复制代码
/*
 * Copyright (c) 2020, 2023,  All rights reserved.
 *
 */
package cn;


import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.*;

/**
 * <p>Project: jdbcUnitls - Untils</p>
 * <p>Powered by scl On 2023-08-15 19:12:41</p>
 * <p>描述:<p>
 *
 * @author 孙臣龙 [1846080280@qq.com]
 * @version 1.0
 * @since 17
 */
public class Untils {
    private String driver;
    private String url;
    private String username;
    private String password;
    private Connection con;

    //初始化自动建立连接
    public Untils() {
        connect();
    }

    //初始化传参可以连接自定义数据库
    public Untils(String url, String username, String password) {
        this.url = url;
        this.username = username;
        this.password = password;
        connect(url, username, password);
    }

    //建立连接,默认数据库
    public void connect() {
        Class<Untils> until = Untils.class;
        InputStream db = until.getClassLoader().getResourceAsStream("db.properties");
        Properties prop = new Properties();
        try {
            prop.load(db);
            this.driver = prop.getProperty("db.driver", "com.mysql.cj.jdbc.Driver");
            Class.forName(this.driver);
            this.url = prop.getProperty("db.url", "jdbc:mysql:/mysql");
            this.username = prop.getProperty("db.username", "root");
            this.password = prop.getProperty("db.password", "");
            con = DriverManager.getConnection(this.url, this.username, this.password);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //建立连接,指定数据库
    public void connect(String url, String username, String password) {
        try {
            Class.forName(driver);
            con = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //获取版本号
    public String version() {
        String ver = "";
        try {
            ver = this.con.getMetaData().getDatabaseProductVersion();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ver;
    }

    //建立数据库
    public void creatdatabase(String dbname) {
        try {
            String sql = "create database if not exists " + dbname;
            PreparedStatement ps = con.prepareStatement(sql);
            ps.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //删除数据库
    public void deletedatabase(String dbname) {
        try {
            String sql = "drop database if exists " + dbname;
            PreparedStatement ps = con.prepareStatement(sql);
            ps.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //查看所有数据库(排除系统数据库)
    public Set<String> showdatabase() {
        Set<String> set = new HashSet<>();
        try {
            String sql = "show databases";
            PreparedStatement ps = con.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            Set<String> exclude = new HashSet<>(List.of("information_schema", "performance_schema", "test", "sys", "mysql"));
            while (rs.next()) {
                String database = rs.getString(1);
                if (exclude.contains(database)) continue;
                set.add(rs.getString(1));
            }
            rs.close();
            ps.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return set;
    }

    //查看指定数据库的所有表
    public List<String> showtables(String dbname) {
        List<String> list = new ArrayList<>();
        try {
            String sql = "show tables ";
            if (dbname != null && dbname.length() > 0) {
                sql = "show tables from " + dbname;
            }
            PreparedStatement ps = con.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                list.add(rs.getString(1));
            }
            ps.close();
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    //查看使用数据库的表
    public List<String> showtables() {
        String dbname = "";
        try {
            String sql = "select database()";
            PreparedStatement ps = con.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            if (rs.isBeforeFirst()) {
                rs.next();
                dbname = rs.getString(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return showtables(dbname);
    }


    //关闭连接
    public void close() {
        try {
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public String getDriver() {
        return driver;
    }

    public void setDriver(String driver) {
        this.driver = driver;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    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;
    }

    public Connection getCon() {
        return con;
    }

    public void setCon(Connection con) {
        this.con = con;
    }
}

5.将你写好的项目进行打包。

不会打包的可以作为参考:maven如何打包你会吗?_明天更新的博客-CSDN博客

6.将打包文件复制到常用架包文件夹中。

7.使用架包(一行代码连接数据库)

有小伙伴就有疑问啦: 这就结束了????多少是有点简单了

后续功能你可以根据自己所学进行添加。

相关推荐
CC呢5 分钟前
基于单片机的智能婴儿床监护系统多功能婴儿床摇篮系统
数据库·mongodb
一个不秃头的 程序员11 分钟前
代码加入SFTP JAVA ---(小白篇3)
java·python·github
丁总学Java23 分钟前
--spring.profiles.active=prod
java·spring
上等猿30 分钟前
集合stream
java
java1234_小锋34 分钟前
MyBatis如何处理延迟加载?
java·开发语言
菠萝咕噜肉i35 分钟前
MyBatis是什么?为什么有全自动ORM框架还是MyBatis比较受欢迎?
java·mybatis·框架·半自动
嵌入(师)38 分钟前
嵌入式驱动开发详解21(网络驱动开发)
网络·驱动开发
林的快手1 小时前
209.长度最小的子数组
java·数据结构·数据库·python·算法·leetcode
诸葛小猿1 小时前
maven的scope详解
maven·scope
向阳12181 小时前
mybatis 缓存
java·缓存·mybatis