你是否还在为每次新建项目连接数据库而烦恼???(教你一次代码,简单完成每次连接)
1.建立maven项目
还没下载安装或者不会建立maven项目的可以看这里哦:maven的下载安装与配置环境变量!!!(全网最详细)_明天更新的博客-CSDN博客
2.编写配置文件。
<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文件,并写入一下代码。
db.driver=com.mysql.cj.jdbc.Driver
db.url=jdbc:mysql:/book
db.username=root
db.password=
4.编写工具类。
/** 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.使用架包(一行代码连接数据库)
有小伙伴就有疑问啦: 这就结束了????多少是有点简单了
后续功能你可以根据自己所学进行添加。