<dependencies>
<!-- junit 测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- web servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- 日志打印相关的jar -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.2</version>
</dependency>
<!-- mysql 驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<!-- json 依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
<!-- hutool工具集 -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.4.7</version>
</dependency>
<!-- commons-io 依赖 -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<!-- 添加百度编辑器ueditor支持 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<!-- jstl依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- lombok插件 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>lezijie-note</finalName>
<plugins>
<!-- 编译环境插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- Tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>8080</port> <!-- 启动端口 默认:8080 -->
<path>/note</path> <!-- 项目的站点名,即对外访问路径 -->
<server>tomcat7</server> <!-- 服务器名称 -->
</configuration>
</plugin>
</plugins>
</build>
</project>
# 连接MYSQL数据库的配置文件 注:等号的前后不要写空格
# 驱动名
jdbcName=com.mysql.cj.jdbc.Driver
# 数据库连接 ( db_lezijie_note是数据库的名称)
dbUrl=jdbc:mysql://localhost:3306/db_lezijie_note?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
# 数据库的连接账号 (账号基本上都是root)
dbName=root
# 数据库的连接密码 (每个人的数据库密码可能不一致,需要修改)
dbPwd=root
/** * 得到配置文件对象 */
private static Properties properties = new Properties();
static {
try {
//加载配置文件(输入流)
InputStream in = DBUtil.class.getClassLoader().getResourceAsStream("db.properties");
// 通过load()方法将数日六的内容加载到配置文件对象中
properties.load(in);
// 通过配置文件对象的getProperty()方法获取驱动名,拿到驱动名之后加载驱动
Class.forName(properties.getProperty("jdbcName"));
} catch (Exception e) {
e.printStackTrace();
}
}
/** * 获取数据库连接 * @return */
public static Connection getConnection(){
Connection connection = null;
try {
//得到数据库链接的相关性息
String dbUrl = properties.getProperty("dbUrl");
String dbName = properties.getProperty("dbName");
String dbPwd = properties.getProperty("dbPwd");
connection = DriverManager.getConnection(dbUrl,dbName,dbPwd);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return connection;
}
/** * 关闭资源 * @param resultSet * @param preparedStatement * @param connection */
public static void close(ResultSet resultSet,
PreparedStatement preparedStatement,
Connection connection){
//判断资源对象如果不为空则关闭
try {
if (resultSet != null) {
resultSet.close();
}
if (preparedStatement != null) {
preparedStatement.close();
}
if (connection != null) {
connection.close();
}
}catch (SQLException throwables) {
throwables.printStackTrace();
}
}
package com.ftzlovedsj.util;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
/** * @author ${范涛之} * @Description * @create 2021-11-14 19:02 */
public class DBUtil {
/** * 得到配置文件对象 */
private static Properties properties = new Properties();
static {
try {
//加载配置文件(输入流)
InputStream in = DBUtil.class.getClassLoader().getResourceAsStream("db.properties");
// 通过load()方法将数日六的内容加载到配置文件对象中
properties.load(in);
// 通过配置文件对象的getProperty()方法获取驱动名,拿到驱动名之后加载驱动
Class.forName(properties.getProperty("jdbcName"));
} catch (Exception e) {
e.printStackTrace();
}
}
/** * 获取数据库连接 * @return */
public static Connection getConnection(){
Connection connection = null;
try {
//得到数据库链接的相关性息
String dbUrl = properties.getProperty("dbUrl");
String dbName = properties.getProperty("dbName");
String dbPwd = properties.getProperty("dbPwd");
connection = DriverManager.getConnection(dbUrl,dbName,dbPwd);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return connection;
}
/** * 关闭资源 * @param resultSet * @param preparedStatement * @param connection */
public static void close(ResultSet resultSet,
PreparedStatement preparedStatement,
Connection connection){
//判断资源对象如果不为空则关闭
try {
if (resultSet != null) {
resultSet.close();
}
if (preparedStatement != null) {
preparedStatement.close();
}
if (connection != null) {
connection.close();
}
}catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
package com.ftzlovedsj;
import com.ftzlovedsj.util.DBUtil;
import org.junit.Test;
/** * @author ${范涛之} * @Description * @create 2021-11-14 19:30 */
public class TestDB {
/** * 单元测试方法 * 1. 方法的返回值,建议使用void,一般没有返回值 * 2. 参数列表,建议空参,一般是没有参数 * 3. 方法上需要设置@Test注解 * 4. 每个方法都能独立运行 * * 判定结果: * 绿色:成功 * 红色:失败 */
@Test
public void testDB(){
System.out.println(DBUtil.getConnetion());
}
}
在 src/main/resources 目录下,新建 log4j.properties 文件
### 设置###stdout,
log4j.rootLogger = all,D,E,stdout
### 输出信息到控制台 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
### 输出DEBUG 级别以上的日志到=D://logs/log.log ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = D://logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
### 输出ERROR 级别以上的日志到=E://logs/error.log ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =E://logs/error.log
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ]
// 使用日志工厂类,记入日志
private static Logger logger = LoggerFactory.getLogger(DBUtil.class);
// 记录日志
logger.info("在{}时,获取数据库连接", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
package com.ftzlovedsj;
import com.ftzlovedsj.util.DBUtil;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** * @author ${范涛之} * @Description * @create 2021-11-14 19:30 */
public class TestDB {
// 使用日志工厂类,记录日志
private Logger logger = LoggerFactory.getLogger(TestDB.class);
/** * 单元测试方法 * 1. 方法的返回值,建议使用void,一般没有返回值 * 2. 参数列表,建议空参,一般是没有参数 * 3. 方法上需要设置@Test注解 * 4. 每个方法都能独立运行 * * 判定结果: * 绿色:成功 * 红色:失败 */
@Test
public void testDB(){
System.out.println(DBUtil.getConnetion());
//使用日志
logger.info("获取数据库连接:" + DBUtil.getConnetion());
logger.info("获取数据库连接:{}", DBUtil.getConnetion());
}
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/justleavel/article/details/121321526
内容来源于网络,如有侵权,请联系作者删除!