Python 官方文档:入门教程 => 点击学习
目录项目初始流程:数据库的建立于一开始的分层:db.properties:首先创建数据库连接的工具类:DBUtil1:得到配置文件对象:2:获取数据库连接:3:关闭资源:DBUtil
首先说一下pom.xml文件的依赖:
<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();
}
}
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;
}
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;
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();
}
}
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;
}
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();
}
}
}
在写好连接后我们需要写一个测试类来判断数据库是否真正连接成功:
然后编写测试代码:TestDB类:
package com.ftzlovedsj;
import com.ftzlovedsj.util.DBUtil;
import org.junit.Test;
public class TestDB {
@Test
public void testDB(){
System.out.println(DBUtil.getConnetion());
}
}
这里倘若出现了报错有可能的原因是你没有在pom文件里面导入mysql连接包:
运行成功后:
使用日志
配置文件
在 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()));
TestDB:
package com.ftzlovedsj;
import com.ftzlovedsj.util.DBUtil;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TestDB {
// 使用日志工厂类,记录日志
private Logger logger = LoggerFactory.getLogger(TestDB.class);
@Test
public void testDB(){
System.out.println(DBUtil.getConnetion());
//使用日志
logger.info("获取数据库连接:" + DBUtil.getConnetion());
logger.info("获取数据库连接:{}", DBUtil.getConnetion());
}
}
到此这篇关于javaweb 项目初始配置的方法步骤的文章就介绍到这了,更多相关javaweb 项目初始配置内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: javaweb 项目初始配置的方法步骤
本文链接: https://lsjlt.com/news/157323.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0