本项目为一个简易的图书管理系统,用到的技术有Java和MySQL数据库,此项目一共用到了四个jar包,分别为:
apache的commons组件:
commons-dbutils-1.4.jar:封装并简化了JDBC;
commons-dbcp-1.4.jar:apache commons提供的数据库连接池组件,命名为DBCP;
commons.pool-1.3.jar:DBCP连接池依赖该jar包;
mysql-connector-java-5.1.28-bin.jar:MySQL的JDBC驱动包,用JDBC连接MySQL数据库必须使用该JAR包
此项目还用到了项目分层,由于初步了解项目分层,所以理解还不是很好,
项目一共分了7个包
controller包为控制层,用来接收视图层的数据,并将数据传递给service层
dao包主要为操作数据库的代码
entry包为javaBean类
run包为程序的入口
service包为业务层,接收上一层,控制层controller的数据,然后,传递给dao层,操作数据库
utils包为工具类
view包则为视图层,用户看到和操作的界面,数据传递给controller层实现
其中还有一个配置文件,里面存放的是数据库的连接信息:
package utils;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
public class JDBCUtils {
private static BasicDataSource dataSource = new BasicDataSource();
private JDBCUtils(){}
static {
try {
InputStream rs = JDBCUtils.class.getClassLoader().getResourceAsStream("application.properties");//使用类的加载器加载配置文件
Properties p = new Properties();
p.load(rs);//此处出现异常,请检查项目的bin目录下是否拥有配置文件
dataSource.setDriverClassName(p.getProperty("driver"));
dataSource.setUrl(p.getProperty("url"));
dataSource.setUsername(p.getProperty("username"));
dataSource.setPassword(p.getProperty("userpassword"));
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("数据库连接失败");
}
}
public static DataSource getDataSource(){
return dataSource;
}
}
程序的主入口:
package run;
import view.BookManageView;
public class RunApp {
public static void main(String[] args) {
new BookManageView().show();
}
}
程序的视图层:
package view;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;
import controller.BookController;
import controller.UserController;
import entry.Book;
public class BookManageView {
private Scanner sc = new Scanner(System.in);
private BookController bController = new BookController();
private UserController uController = new UserController();
private int key = 0;// 用来记录用户的登录状态,0表示未登录,1表示已登录
public void printMain() {
System.out.println("------欢迎使用图书管理系统------");
System.out.println("1.查询所有图书 2.按类别查询图书 3.添加图书 4.更新图书 5.删除图书");
System.out.println("6.借书 7.还书 ");
System.out.println("8.退出登录 0.退出");
}
public void printloginAndReg() {
System.out.println("1.注册 2.登录");
System.out.println("0.返回");
}
public void selectAll() {
List<Book> books = bController.selectAll();
System.out.println("编号" + "\t" + "书名" + "\t\t" + "价格" + "\t" + "添加时间" + "\t\t\t" + "类型");
for (Book book : books) {
System.out.println(book);
}
}
public void selectByType() {
System.out.println("请输入您要查询的类别:");
String type = sc.next();
List<Book> books = bController.selectAll(type);
if (books.size() == 0)
System.out.println("该类别暂时没有图书");
for (Book book : books) {
System.out.println(book);
}
}
public void addBook() {
System.out.println("请输入图书名字:");
String bname = sc.nextLine();
System.out.println("请输入图书价格:");
Double bprice = sc.nextDouble();
sc.nextLine();
System.out.println("请输入图书类别:");
String type = sc.nextLine();
String addtime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
boolean flag = bController.addBook(bname, bprice, addtime, type);
if (flag) {
System.out.println("添加成功");
} else {
System.out.println("添加失败");
}
}
public void updateBook() {
selectAll();
System.out.println("请输入您想要修改的图书名字:");
String name = sc.nextLine();
System.out.println("请输入新的图书名字:");
String bname = sc.nextLine();
System.out.println("请输入新的价格:");
Double bprice = sc.nextDouble();
sc.nextLine();
System.out.println("请输入图书类型:");
String type = sc.nextLine();
String updatetime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
boolean flag = bController.updateBook(name, bname, bprice, updatetime, type);
if (flag) {
System.out.println("更新成功");
} else {
System.out.println("更新失败");
}
}
public void deleteBook() {
selectAll();
System.out.println("请输入您想要删除的图书的名字:");
String bname = sc.nextLine();
boolean flag = bController.deleteBook(bname);
if (flag)
System.out.println("删除成功");
System.out.println("删除失败");
}
public void login() {
System.out.println("----登录----");
System.out.println("请输入您的用户名:");
String username = sc.nextLine();
System.out.println("请输入您的密码:");
String password = sc.nextLine();
boolean flag = uController.login(username, password);
if (flag) {
System.out.println("登录成功!");
key = 1;
} else {
System.out.println("登录失败!");
}
}
public void reg() {
//也可以使用递归,不用while循环
while (true) {
System.out.println("----注册----");
System.out.println("请输入你要使用的用户名:");
String username = sc.nextLine();
System.out.println("请输入你要使用的密码:");
String password = sc.nextLine();
System.out.println("请再次输入密码:");
String psd = sc.nextLine();
boolean flag = uController.verify(username);
if (flag) {
if (password.equals(psd)) {
uController.register(username, password);
System.out.println("注册成功");
login();
break;
} else {
System.out.println("前后两次输入的密码不一致,请重新进行注册");
continue;
}
} else {
System.out.println("该用户名已被占用,请重新进行注册");
}
}
}
public void borrowBook() {
if (key == 0) {
System.out.println("您还没有登录,登录后才可以借书.");
while (true) {
printloginAndReg();
int choice = choice();
if(choice == 0)
break;
switch (choice) {
case 1:
reg();
borrowBook();
break;
case 2:
login();
borrowBook();
break;
default:
System.out.println("您的选择有误,请重新选择:");
break;
}
}
} else {
selectAll();
System.out.println("请输入你想要借的书的名字:");
String bname = sc.nextLine();
// state表示书本的状态,0表示未借出,1则表示已借出
boolean flag = bController.updateState(1, bname);
if (flag) {
System.out.println("借书成功!");
} else {
System.out.println("借书失败!");
}
}
}
public List<Book> onLoanBook(){
return bController.onLoanBook();
}
public void returnBook() {
List<Book> books = onLoanBook();
if(books.size() == 0){
System.out.println("您暂时没有借的书本.");
}else{
System.out.println("您借的书本有:");
for (Book book : books) {
System.out.println(book);
}
System.out.println("请输入你要还的书的名字:");
String bname = sc.nextLine();
boolean flag = bController.updateState(0, bname);
if (flag) {
System.out.println("还书成功!");
} else {
System.out.println("还书失败!您输入的书名可能不正确!");
}
}
}
public void exit() {
if (key == 0) {
System.out.println("您还没有登录呢!");
} else {
key = 0;
System.out.println("已退出登录!");
}
}
//选择操作
public int choice() {
System.out.println("请选择您要进行的操作:");
try {
int choice = sc.nextInt();
sc.nextLine();
return choice;
} catch (InputMismatchException ie) {
sc.nextLine();
return 10010;
}
}
public void show() {
while (true) {
printMain();
int choice = choice();
switch (choice) {
case 1:
selectAll();
break;
case 2:
selectByType();
break;
case 3:
addBook();
break;
case 4:
updateBook();
break;
case 5:
deleteBook();
break;
case 6:
borrowBook();
break;
case 7:
returnBook();
break;
case 8:
exit();
break;
case 0:
System.out.println("bye");
System.exit(0);
break;
default:
System.out.println("您的选择有误,请重新选择:");
break;
}
}
}
}
代码就不一一放出了,项目已压缩放到附件,包含了数据库的sql文件,使用工具执行即可.
额 突然发现资源没上传附上链接吧:
http://download.csdn.net/download/z3364f/9905018