使用maven工具----实现JDBC学生管理系统
项目结构Main.javaToolDBLink.javaIRowMapper.javaProperties.java
Propertiesdb.propertieslog4j.properties
jar包1、junit2、javax.servlet3、mysql4、log4j
项目结构
Main.java
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 {
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键以继续");
final Scanner scanner
= new Scanner(System
.in
);
int option
= scanner
.nextInt();
switch (option
) {
case 1:{
System
.out
.println("添加系统");
System
.out
.println("请输入学号");
String id
= scanner
.next();
String sql
= "select id from student where id = ?";
if(new DBLink().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(new DBLink().update(sql
,id
, name
,mobile
,address
)) {
System
.out
.println("添加成功");
break;
}
System
.out
.println("添加失败");
break;
}
case 2:{
System
.out
.println("修改系统");
System
.out
.println("请输入学号");
String id
= scanner
.next();
String sql
= "select id from student where id = ?";
if(new DBLink().exist(sql
,id
)) {
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(new DBLink().update(sql
, name
,mobile
,address
,id
)) {
System
.out
.println("修改成功");
break;
}
}
System
.out
.println("没有找到学号为"+id
+"的学生,请检查学号是否错误");
break;
}
case 3:{
System
.out
.println("欢迎使用查询系统");
System
.out
.println("请输入学号");
String id
= scanner
.next();
String sql
= "select id,name,mobile,address from student where id = ?";
class RowMapper implements IRowMapper{
public void IRowMapper(ResultSet rs
) {
try {
if(rs
.next()) {
String 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();
}
}
}
IRowMapper rowMapper
= new RowMapper();
new DBLink().select(sql
,rowMapper
,id
);
break;
}
case 4:{
System
.out
.println("欢迎使用删除系统");
System
.out
.println("请输入学号");
String id
= scanner
.next();
String sql
= "select id from student where id = ?";
if(new DBLink().exist(sql
,id
)) {
sql
= "delete from student where id = ?";
new DBLink().update(sql
,id
);
System
.out
.println("删除成功");
break;
}
System
.out
.println("未找到学号为'"+id
+"'的学生,删除失败");
break;
}
default:
System
.out
.println("I'm Sorry,there is not the "+option
+" option,please try again.");
}
}
}
Tool
DBLink.java
package com
.zzu
.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
.zzu
.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
,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
.IRowMapper(resultSet
);
} catch (Exception e
) {
logger
.debug(e
.getMessage(), e
);
}finally {
close(resultSet
,preparedStatement
,connection
);
}
}
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
) {
logger
.debug(e
.getMessage(), e
);
}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
) {
logger
.debug(e
.getMessage(), e
);
}finally {
close(preparedstatement
,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
) {
logger
.debug(e
.getMessage(), e
);
}finally {
close(resultSet
,preparedStatement
,connection
);
}
return false;
}
private void close(Statement statement
,Connection connection
) {
try {
if(statement
!=null
) {
statement
.close();
}
} catch (SQLException e
) {
e
.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
);
}
}
IRowMapper.java
package com
.zzu
.tool
.db
;
import java
.sql
.ResultSet
;
public interface IRowMapper {
void IRowMapper(ResultSet rs
);
}
Properties.java
package com
.zzu
.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 String
getvalue(String key
) {
return properties
.getProperty(key
);
}
}
Properties
db.properties
db
.username
=root
db
.password
=root
db
.url
=jdbc
:mysql
://localhost
:3306/test
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
,Console
,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
jar包
1、junit
<dependency
>
<groupId
>junit
</groupId
>
<artifactId
>junit
</artifactId
>
<version
>3.8.1
</version
>
<scope
>test
</scope
>
</dependency
>
2、javax.servlet
<dependency
>
<groupId
>javax.servlet
</groupId
>
<artifactId
>javax.servlet-api
</artifactId
>
<version
>3.1.0
</version
>
<scope
>provided
</scope
>
</dependency
>
3、mysql
<dependency
>
<groupId
>mysql
</groupId
>
<artifactId
>mysql-connector-java
</artifactId
>
<version
>5.1.42
</version
>
</dependency
>
4、log4j
<dependency
>
<groupId
>log4j
</groupId
>
<artifactId
>log4j
</artifactId
>
<version
>1.2.17
</version
>
</dependency
>