如何将常用的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.使用架包(一行代码连接数据库)

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

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

相关推荐
tyler_download6 分钟前
golang 实现比特币内核:实现基于椭圆曲线的数字签名和验证
开发语言·数据库·golang
2401_8574396924 分钟前
SpringBoot框架在资产管理中的应用
java·spring boot·后端
怀旧66625 分钟前
spring boot 项目配置https服务
java·spring boot·后端·学习·个人开发·1024程序员节
李老头探索27 分钟前
Java面试之Java中实现多线程有几种方法
java·开发语言·面试
weixin_4493108432 分钟前
高效集成:聚水潭采购数据同步到MySQL
android·数据库·mysql
芒果披萨33 分钟前
Filter和Listener
java·filter
CodingBrother33 分钟前
MySQL 和 PostgreSQL 的使用案例
mysql·adb·postgresql
qq_49244844637 分钟前
Java实现App自动化(Appium Demo)
java
阿华的代码王国1 小时前
【SpringMVC】——Cookie和Session机制
java·后端·spring·cookie·session·会话
Cachel wood1 小时前
Github配置ssh key原理及操作步骤
运维·开发语言·数据库·windows·postgresql·ssh·github