Maven web项目结构:
JDBC工具包在之前的博客内有
log4j.properties:
# DEBUG\u8BBE\u7F6E\u8F93\u51FA\u65E5\u5FD7\u7EA7\u522B\uFF0C\u7531\u4E8E\u4E3ADEBUG\uFF0C\u6240\u4EE5ERROR\u3001WARN\u548CINFO \u7EA7\u522B\u65E5\u5FD7\u4FE1\u606F\u4E5F\u4F1A\u663E\u793A\u51FA\u6765
log4j
.rootLogger
=DEBUG
,RollingFile
#\u5C06\u65E5\u5FD7\u4FE1\u606F\u8F93\u51FA\u5230\u63A7\u5236\u53F0
log4j
.appender
.Console
=org
.apache
.log4j
.ConsoleAppender
log4j
.appender
.Console
.layout
=org
.apache
.log4j
.PatternLayout
log4j
.appender
.Console
.layout
.ConversionPattern
= [%-5p
]-[%d
{yyyy
-MM
-dd HH
:mm
:ss
}] -%l
-%m
%n
#\u5C06\u65E5\u5FD7\u4FE1\u606F\u8F93\u51FA\u5230\u64CD\u4F5C\u7CFB\u7EDFD\u76D8\u6839\u76EE\u5F55\u4E0B\u7684log
.log\u6587\u4EF6\u4E2D
log4j
.appender
.RollingFile
=org
.apache
.log4j
.DailyRollingFileAppender
log4j
.appender
.RollingFile
.File
=D
://log
.log
log4j
.appender
.RollingFile
.layout
=org
.apache
.log4j
.PatternLayout
log4j
.appender
.RollingFile
.layout
.ConversionPattern
=%d
[%t
] %-5p
%-40.40c
%X
{traceId
}-%m
%n
其中log4j.rootLogger=DEBUG,Console,RollingFile Console表示显示在控制台上 RollingFile表示显示在文件中
db.properties:
db
.username
=root
db
.password
=root
db
.url
=jdbc
:mysql
://127.0.0.1:3306/test
PropertiesTool:
package com
.jd
.tool
;
import java
.io
.IOException
;
import java
.io
.InputStream
;
import java
.util
.Properties
;
public class PropertiesTool {
private static Properties properties
= new Properties();
static {
InputStream inputStream
= PropertiesTool
.class.getClassLoader().getResourceAsStream("db.properties");
try {
properties
.load(inputStream
);
} catch (IOException e
) {
e
.printStackTrace();
}
}
public static void main(String
[] ages
) {
String userName
= properties
.getProperty("db.username");
System
.out
.print(userName
);
}
public static String
getValue(String key
) {
return properties
.getProperty(key
);
}
}
IRowMapper:
package com
.jd
.tool
.db
;
import java
.sql
.ResultSet
;
public interface IRowMapper {
void rowMapper(ResultSet rs
);
}
DBLink:
package com
.jd
.tool
.db
;
import java
.sql
.Connection
;
import java
.sql
.DriverManager
;
import java
.sql
.PreparedStatement
;
import java
.sql
.ResultSet
;
import java
.sql
.SQLException
;
import java
.sql
.Statement
;
import org
.apache
.log4j
.Logger
;
import com
.jd
.tool
.PropertiesTool
;
public class DBLink {
private Logger logger
= Logger
.getLogger(DBLink
.class);
private Connection
getConnection() {
try {
Class
.forName("com.mysql.jdbc.Driver");
String userName
= PropertiesTool
.getValue("db.username");
String password
= PropertiesTool
.getValue("db.password");
String url
= PropertiesTool
.getValue("db.url");
return DriverManager
.getConnection(url
, userName
, password
);
} catch (Exception e
) {
logger
.debug(e
.getMessage(), e
);
}
return null
;
}
public void select(String sql
, IRowMapper rowMapper
) {
Connection connection
= null
;
Statement statement
= null
;
ResultSet resultSet
= null
;
try {
connection
= getConnection();
statement
= connection
.createStatement();
resultSet
= statement
.executeQuery(sql
);
rowMapper
.rowMapper(resultSet
);
} catch (Exception e
) {
e
.printStackTrace();
} finally {
close(resultSet
, statement
, connection
);
}
}
public void select(String sql
, IRowMapper rowMapper
, Object
... params
) {
Connection connection
= null
;
PreparedStatement preparedStatement
= null
;
ResultSet resultSet
= null
;
try {
connection
= getConnection();
preparedStatement
= connection
.prepareStatement(sql
);
for (int i
= 0; i
< params
.length
; i
++) {
preparedStatement
.setObject(i
+ 1, params
[i
]);
}
resultSet
= preparedStatement
.executeQuery();
rowMapper
.rowMapper(resultSet
);
} catch (Exception e
) {
e
.printStackTrace();
} finally {
close(resultSet
, preparedStatement
, connection
);
}
}
public boolean exist(String sql
) {
Connection connection
= null
;
Statement statement
= null
;
ResultSet resultSet
= null
;
try {
connection
= getConnection();
statement
= connection
.createStatement();
resultSet
= statement
.executeQuery("select id,name,mobile,address from first_class");
return resultSet
.next();
} catch (Exception e
) {
e
.printStackTrace();
} finally {
close(resultSet
, statement
, connection
);
}
return false;
}
public boolean exist(String sql
, Object
... params
) {
Connection connection
= null
;
PreparedStatement preparedStatement
= null
;
ResultSet resultSet
= null
;
try {
connection
= getConnection();
preparedStatement
= connection
.prepareStatement(sql
);
for (int i
= 0; i
< params
.length
; i
++) {
preparedStatement
.setObject(i
+ 1, params
[i
]);
}
resultSet
= preparedStatement
.executeQuery();
return resultSet
.next();
} catch (Exception e
) {
e
.printStackTrace();
} finally {
close(resultSet
, preparedStatement
, connection
);
}
return false;
}
public boolean update(String sql
) {
Connection connection
= null
;
Statement statement
= null
;
try {
connection
= getConnection();
statement
= connection
.createStatement();
int result
= statement
.executeUpdate(sql
);
return result
> 0;
} catch (Exception e
) {
e
.printStackTrace();
} finally {
close(statement
, connection
);
}
return false;
}
public boolean update(String sql
, Object
... params
) {
Connection connection
= null
;
PreparedStatement preparedStatement
= null
;
try {
connection
= getConnection();
preparedStatement
= connection
.prepareStatement(sql
);
for (int i
= 0; i
< params
.length
; i
++) {
preparedStatement
.setObject(i
+ 1, params
[i
]);
}
return preparedStatement
.executeUpdate() > 0;
} catch (Exception e
) {
e
.printStackTrace();
} finally {
close(preparedStatement
, connection
);
}
return false;
}
private void close(Statement statement
, Connection connection
) {
try {
if (statement
!= null
) {
statement
.close();
}
} catch (SQLException e1
) {
e1
.printStackTrace();
}
try {
if (connection
!= null
) {
connection
.close();
}
} catch (SQLException e
) {
e
.printStackTrace();
}
}
private void close(ResultSet resultSet
, Statement statement
, Connection connection
) {
try {
if (resultSet
!= null
) {
resultSet
.close();
}
} catch (SQLException e
) {
e
.printStackTrace();
}
close(statement
, connection
);
}
}
Main:
package com
.zzu
.client
;
import java
.sql
.ResultSet
;
import java
.sql
.SQLException
;
import java
.util
.Scanner
;
import com
.zzu
.tool
.db
.DBLink
;
import com
.zzu
.tool
.db
.IRowMapper
;
public class Main {
private static DBLink db
= new DBLink();
public static void main(String
[] args
) {
System
.out
.println("*********************************");
System
.out
.println("*\t\t\t\t*");
System
.out
.println("*\t欢迎使用学生信息管理系统\t*");
System
.out
.println("*\t\t\t\t*");
System
.out
.println("*********************************");
while (true) {
menu();
}
}
static void menu() {
System
.out
.println("1、添加学生信息");
System
.out
.println("2、删除学生信息");
System
.out
.println("3、修改学生信息");
System
.out
.println("4、查询学生信息");
System
.out
.println("请输入操作,以Enter键结束:");
Scanner scanner
= new Scanner(System
.in
);
int option
= scanner
.nextInt();
switch (option
) {
case 1: {
System
.out
.println("请输入学生学号:");
String id
= scanner
.next();
String sql
= "select id from student where id=?";
if (db
.exist(sql
, id
)) {
System
.out
.println("学生学号已存在,请重新输入!");
return;
}
System
.out
.println("请输入学生姓名:");
String name
= scanner
.next();
System
.out
.println("请输入学生电话:");
String mobile
= scanner
.next();
System
.out
.println("请输入学生地址:");
String address
= scanner
.next();
sql
= "insert into student (id,name,mobile,address)values(?,?,?,?)";
if (db
.update(sql
, id
, name
, mobile
, address
)) {
System
.out
.println("添加学生信息成功!");
return;
}
System
.out
.println("添加学生信息失败!");
break;
}
case 2: {
System
.out
.println("请输入要删除的学生学号:");
String id
= scanner
.next();
String sql
= "delete from student where id =?";
if (db
.update(sql
, id
)) {
System
.out
.println("成功删除学生信息!");
return;
}
System
.out
.println("删除失败!学号不存在");
break;
}
case 3: {
System
.out
.println("请输入要修改的学生学号:");
String id
= scanner
.next();
String sql
= "select name from student where id = ?";
if (!db
.exist(sql
, id
)) {
System
.out
.println("学号不存在,操作终止!");
return;
}
System
.out
.println("请输入新的学生姓名:");
String name
= scanner
.next();
System
.out
.println("请输入新的学生电话:");
String mobile
= scanner
.next();
System
.out
.println("请输入新的学生地址:");
String address
= scanner
.next();
sql
= "update student set name=?,mobile=?,address=? where id=?";
if (db
.update(sql
, name
, mobile
, address
,id
)) {
System
.out
.println("修改成功!");
return;
}
System
.out
.println("修改失败!");
break;
}
case 4: {
System
.out
.println("请输入要查询的学生学号:");
String id
= scanner
.next();
String sql
= "select id,name,mobile,address from student where id =?";
db
.select(sql
, (rs
) -> {
String name
;
try {
if (rs
.next()) {
name
= rs
.getString("name");
String mobile
= rs
.getString("mobile");
String address
= rs
.getString("address");
System
.out
.println(id
+ "," + name
+ "," + mobile
+ "," + address
);
return;
}
System
.out
.println("查询失败!");
} catch (SQLException e
) {
e
.printStackTrace();
}
}, id
);
break;
}
default:
System
.out
.println("I'm Sorry,there is not the " + option
+ " option,please try again.");
}
}
}
补充:
在实现查询功能时使用了Lambda表达式,须将创建的Maven web项目升级为JDK8.0+ 可以在pom.xml中添加:
<profiles>
<profile>
<id>jdk1
.8</id
>
<activation>
<activeByDefault>true</activeByDefault
>
<jdk>1.8</jdk
>
</activation
>
<properties>
<maven
.compiler
.source
>1.8</maven
.compiler
.source
>
<maven
.compiler
.target
>1.8</maven
.compiler
.target
>
<maven
.compiler
.compilerVersion
>1.8</maven
.compiler
.compilerVersion
>
</properties
>
</profile
>
</profiles
>
然后选择Maven ——> Update project即可 注:如果创建Maven web项目报错,可能是缺少servlet