不废话,直接上
先看一下项目列表:
首先创建一个maven工程,然后导入相应的jar包,请参考:使用maven导入jar包
接着在Source Folder创建具体的项目:
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 {
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(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("添加成功!");
return;
}
System
.out
.println("添加失败!");
break;
}
case 2:{
System
.out
.println("请输入要删除学生的学号:");
String id
= scanner
.next();
String sql
= "delete from student where id = ?";
if(new DBLink().update(sql
, id
)) {
System
.out
.println("删除成功!");
return;
}
System
.out
.println("删除失败!");
break;
}
case 3:{
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("修改成功!");
return;
}
System
.out
.println("修改失败!");
return;
}
System
.out
.println("未查询到要修改学生的学号!");
break;
}
case 4:{
System
.out
.println("请输入要查询学生的学号:");
String id
= scanner
.next();
String sql
= "select id from student where id = ?";
if(!new DBLink().exist(sql
, id
)) {
System
.out
.println("学号不存在,无法查询!");
}
sql
= "select id,name,mobile,address from student where id = ?";
class RowMapper implements IRowMapper{
public void rowMapper(ResultSet rs
) {
try {
if(rs
.next()) {
String id1
= rs
.getString("id");
String name
= rs
.getString("name");
String mobile
= rs
.getString("mobile");
String address
= rs
.getString("address");
System
.out
.println("学号:"+id1
+",姓名:"+name
+",手机号:"+mobile
+",家庭住址:"+address
);
}
} catch (SQLException e
) {
e
.printStackTrace();
}
}
};
RowMapper rowMapper
= new RowMapper();
new DBLink().select(sql
, rowMapper
,id
);
break;
}
default:
System
.out
.println("I'm Sorry,there is not the "+option
+" option,please try again.");
}
}
}
DBLink(工具类)
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);
public Connection
getConnection() {
try {
String driver
= PropertiesTool
.getValue("db.driver");
String url
= PropertiesTool
.getValue("db.url");
String userName
= PropertiesTool
.getValue("db.userName");
String password
= PropertiesTool
.getValue("db.password");
Class
.forName(driver
);
return DriverManager
.getConnection(url
, userName
, password
);
} catch (Exception e
) {
logger
.debug(e
.getMessage(), e
);
}
return null
;
}
public boolean exist(String sql
,Object
...params
) {
Connection connection
= null
;
PreparedStatement prepareStatement
= null
;
ResultSet resultset
= null
;
try {
connection
= getConnection();
prepareStatement
= connection
.prepareStatement(sql
);
for (int i
= 0; i
< params
.length
; i
++) {
prepareStatement
.setObject(i
+1, params
[i
]);
}
resultset
= prepareStatement
.executeQuery();
return resultset
.next();
} catch (Exception e
) {
logger
.debug(e
.getMessage(), e
);
} finally {
close(resultset
,prepareStatement
,connection
);
}
return false;
}
public boolean update(String sql
) {
Connection connection
= null
;
Statement statement
= null
;
try {
connection
= getConnection();
statement
= connection
.createStatement();
int affect
= statement
.executeUpdate(sql
);
//如果上面代码出现异常,则该行代码及其下面代码无法执行,所以资源无法释放;比如sql语句语法错误,则statement和connection无法释放
return affect
>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 prepareStatement
= null
;
try {
connection
= getConnection();
prepareStatement
= connection
.prepareStatement(sql
);
for(int i
= 0; i
< params
.length
; i
++) {
prepareStatement
.setObject(i
+1, params
[i
]);
}
int affect
= prepareStatement
.executeUpdate();
return affect
>0;
} catch (Exception e
) {
logger
.debug(e
.getMessage(), e
);
} finally {
close(prepareStatement
,connection
);
}
return false;
}
public Object
getValue(String sql
,String columnName
) {
Connection connection
= null
;
Statement statement
= null
;
ResultSet resultset
= null
;
try {
connection
= getConnection();
statement
= connection
.createStatement();
resultset
= statement
.executeQuery(sql
);
if(resultset
.next()) {
return resultset
.getObject(columnName
);
}
} catch (Exception e
) {
logger
.debug(e
.getMessage(), e
);
} finally {
close(resultset
,statement
,connection
);
}
return null
;
}
public Object
getValue(String sql
,String columnName
,Object
...params
) {
Connection connection
= null
;
PreparedStatement prepareStatement
= null
;
ResultSet resultset
= null
;
try {
connection
= getConnection();
prepareStatement
= connection
.prepareStatement(sql
);
for (int i
= 0; i
< params
.length
; i
++) {
prepareStatement
.setObject(i
+1, params
[i
]);
}
resultset
= prepareStatement
.executeQuery();
if(resultset
.next()) {
return resultset
.getObject(columnName
);
}
} catch (Exception e
) {
logger
.debug(e
.getMessage(), e
);
} finally {
close(resultset
,prepareStatement
,connection
);
}
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
) {
logger
.debug(e
.getMessage(), e
);
} finally {
close(resultset
,statement
,connection
);
}
}
public void select(String sql
,IRowMapper rowMapper
,Object
...params
) {
Connection connection
= null
;
PreparedStatement prepareStatement
= null
;
ResultSet resultset
= null
;
try {
connection
= getConnection();
prepareStatement
= connection
.prepareStatement(sql
);
for (int i
= 0; i
< params
.length
; i
++) {
prepareStatement
.setObject(i
+1, params
[i
]);
}
resultset
= prepareStatement
.executeQuery();
rowMapper
.rowMapper(resultset
);
} catch (Exception e
) {
logger
.debug(e
.getMessage(), e
);
} finally {
close(resultset
,prepareStatement
,connection
);
}
}
private void close(Statement statement
,Connection connection
) {
try {
if (statement
!= null
) {
statement
.close();
}
} catch (SQLException e
) {
logger
.debug(e
.getMessage(), e
);
}
try {
if (connection
!= null
) {
connection
.close();
}
} catch (SQLException e
) {
logger
.debug(e
.getMessage(), e
);
}
}
private void close(ResultSet resultset
,Statement statement
,Connection connection
) {
try {
if (resultset
!= null
) {
resultset
.close();
}
} catch (SQLException e
) {
logger
.debug(e
.getMessage(), e
);
}
close(statement
,connection
);
}
}
IRowMapper接口:
package com
.zzu
.tool
.db
;
import java
.sql
.ResultSet
;
@FunctionalInterface
public interface IRowMapper {
void rowMapper
(ResultSet rs
);
}
PropertiesTool类(获取数据库连接的桥梁):
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
);
}
public static void main(String
[] ages
) {
String userName
= getValue("db.userName");
String password
= getValue("db.password");
String url
= getValue("db.url");
String driver
= getValue("db.driver");
System
.out
.println(userName
);
System
.out
.println(password
);
System
.out
.println(url
);
System
.out
.println(driver
);
}
}
要使用的资源:
db.properties(连接数据库的一些必要信息):
db.userName
= root
db.password
= root
db.url
= jdbc:mysql://127.0.0.1:3306/test
db.driver
= com.mysql.jdbc.Driver
log4j.properties(收集异常信息所需要的一些配置):
log4j.rootLogger
=DEBUG,Console,RollingFile
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
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
slf4j-log4j:
log4j.rootLogger
=DEBUG,RollingFile
log4j.appender.RollingFile
=org.apache.log4j.DailyRollingFileAppender
log4j.appender.RollingFile.File
=d:/logs/sirius.log
log4j.appender.RollingFile.Append
=true
log4j.appender.RollingFile.layout
=org.apache.log4j.PatternLayout
log4j.appender.RollingFile.layout.ConversionPattern
=%d
[%t
] %-5p %-40.40c %X
{traceId
}-%m%n
log4j.appender.RollingFile.Threshold
=DEBUG
log4j.appender.RollingFile.DatePattern
='.'yyyy-MM-dd-HH-mm
'.log'