Java方向(SSM+SpringBoot)项目实训
Day1(2020.7.20)
前提配置:在C:\Users\Administrator目录下找到.m2文件夹一般作为项目的资源依赖仓库,如果没有则新建并命名为.m2,并向其中添加settings.xml文件修改镜像源;
在Maven项目的pom.xml文件中将 properties~ /dependencies中的代码替换如下:(注:MySQL版本,利用命令行登入数据库即可查询)
// Maven 所需的依赖
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<!-- spring版本号 -->
<spring.version>5.0.2.RELEASE</spring.version>
<!-- mybatis版本号 -->
<mybatis.version>3.2.6</mybatis.version>
<!-- log4j日志文件管理包版本 -->
<slf4j.version>1.7.7</slf4j.version>
<log4j.version>1.2.17</log4j.version>
<c3p0.version>0.9.5.2</c3p0.version>
<taglibs.version>1.1.2</taglibs.version>
</properties>
<dependencies>
<!-- spring核心包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${
spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${
spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${
spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${
spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${
spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${
spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${
spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${
spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${
spring.version}</version>
</dependency>
<!-- mybatis核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${
mybatis.version}</version>
</dependency>
<!-- mybatis/spring包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!-- 导入java ee jar 包 -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<!-- 导入Mysql数据库链接jar包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
<!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<!-- JSTL标签类 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- 日志文件管理包 -->
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${
log4j.version}</version>
</dependency>
<!-- 数据连接池 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>${
c3p0.version}</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>${
taglibs.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${
slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${
slf4j.version}</version>
</dependency>
<!-- 导入servlet-api/jsp -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
点击右侧边栏的Maven,并点击刷新按钮,导入的依赖报错便会自动解决;
并且所有的依赖资源便会下载至C:\Users\Administrator.m2\repository路径下,如有需要可调用。
前提配置:
在C:\Users\Administrator.m2\repository\mysql\mysql-connector-java\8.0.18路径下找到符合自己MySQL版本的jar包导入项目中;
右键所创建的项目,例如jdbc.test→New Directory
创建一个lib文件夹以导入jar包调用;
将之前找到的符合mysql版本的jar包复制至lib文件夹中,点击Refactor;
右键点击导入的jar包,选择Add as Library,使其
名称前方生成一个可点击的三角,此jar包才可被项目所调用。
JDBC连接数据库的两种方法:
方法1:
在src文件夹下创建一个jdbc_test类;
并写如下代码,已建立数据库Connection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class jdbc_test {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
// write your code here
Class.forName("com.mysql.cj.jdbc.Driver");
//获取数据库连接对象
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/project_learn?useSSL-false", "root", "******");
//定义sql语句
String sql = "update user set name = '啦啦啦' where id=2";
//获取执行的sql的对象
Statement stmt = conn.createStatement();
//执行sql,并接收返回结果
int count = stmt.executeUpdate(sql);
//处理结果
System.out.println(count);
//释放资源
stmt.close();
conn.close();
}
}
方法2:
在src文件夹下新建一个Resource Bundle,命名为jdbc.properties;
其中代码如下:
url = jdbc:mysql://localhost:3306/project_learn?useSSL-false
user = root
password = ******
driver = com.mysql.cj.jdbc.Driver
同时创建一个工具类,JDBCUtils;
其中代码如下:
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.Properties;
public class JDBCUtils {
private static String url;
private static String user;
private static String password;
private static String driver;
//读取文件,只需要读取一次即可拿到这些值
static {
try {
//创建Properties集合类
Properties pro = new Properties();
ClassLoader classLoader = JDBCUtils.class.getClassLoader();
URL res = ClassLoader.getSystemResource("jdbc.properties");
String path = res.getPath();
System.out.println(path);
//加载文件
pro.load(new FileReader(path));
//获取数值
url = pro.getProperty("url");
user = pro.getProperty("user");
password = pro.getProperty("password");
driver = pro.getProperty("driver");
Class.forName(driver);
}catch (IOException e){
e.printStackTrace();
}catch (ClassNotFoundException e){
e.printStackTrace();
}
}
//获取连接
public static Connection getConnection() throws SQLException{
return DriverManager.getConnection(url,user,password);
}
//释放资源
public static void close(ResultSet rs, Statement stmt, Connection conn){
if(rs!=null){
try {
rs.close();
}catch (SQLException e){
e.printStackTrace();
}
}
if(stmt!=null){
try {
stmt.close();
}catch (SQLException e){
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
}catch (SQLException e){
e.printStackTrace();
}
}
}
}
同时修改jdbc_test类中的代码为:
import java.sql.*;
import java.util.Scanner;
public class jdbc_test {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
// // write your code here
// Class.forName("com.mysql.cj.jdbc.Driver");
// //获取数据库连接对象
// Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/project_learn?useSSL-false","root","ode/0123");
// //定义sql语句
// String sql = "update user set name = '啦啦啦' where id=2";
// //获取执行的sql的对象
// Statement stmt = conn.createStatement();
// //执行sql,并接收返回结果
// int count = stmt.executeUpdate(sql);
// //处理结果
// System.out.println(count);
// //释放资源
// stmt.close();
// conn.close();
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名:");
String username = sc.nextLine();
System.out.println("请输入密码:");
String password = sc.nextLine();
boolean loginFlag = new jdbc_test().login(username,password);
if (loginFlag){
System.out.println("登陆成功");
}else{
System.out.println("失败");
}
}
public boolean login(String username,String password){
if(username == null || password == null){
return false;
}
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = JDBCUtils.getConnection();
//定义sql
String sql = "select * from user where name = '"+username+"' and password = '"+password+"'";
//获取执行sql对象
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
return rs.next();
}catch (SQLException e){
e.printStackTrace();
}finally {
JDBCUtils.close(rs,stmt,conn);
}
return false;
}
}
#Test
1.利用Navicat连接,并创建一个名为project_learn的数据库,同时创建一个user表,内容如下;
2.向user表中通插入两条假数据,以便验证JDBC连接数据库;
3.test,运行jdbc.test,输入数据库中的假数据进行验证。
JDBC连接数据库成功!