1. 程式人生 > >簡單的圖書管理系統(練習JDBC+Oracle資料庫)

簡單的圖書管理系統(練習JDBC+Oracle資料庫)

這是一個簡單的圖書管理系統,對部分功能的進行了實現,主要就是練習對資料庫的增刪改查和邏輯的運用!

1、使用的Oracle資料庫,先建資料庫,並插入幾條簡單的資料



2、在MyEclipse中建立Web Project,命名為bookManager,建包結構如下


3、首先寫dao包中的IBaseDao,然後用OracleBaseDao實現方法連線資料庫(getConnection)和關閉連線(closeCon),程式碼如下 

package cn.dao.utils.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import cn.dao.utils.interfaces.IBaseDao;

/**
 * 使用Oracle資料庫
 * 
 * @author CCQ
 * 
 */
public class oracleBaseDao implements IBaseDao {

	static String DRIVER_CLASS = "oracle.jdbc.driver.OracleDriver";
	static String URL = "jdbc:oracle:thin:@localhost:1521:orcl";
	static String USER = "hope";
	static String PWD = "123456";

	@Override
	public Connection getConn() throws Exception {
		// TODO Auto-generated method stub
		Connection conn = null;
		try {
			Class.forName(DRIVER_CLASS);
			conn = DriverManager.getConnection(URL, USER, PWD);
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		}
		return conn;
	}

	@Override
	public void closeConn(ResultSet rs, PreparedStatement ps, Connection conn)
			throws Exception {
		// TODO Auto-generated method stub
		try {
			if (rs != null) {
				rs.close();
			}
			if (ps != null) {
				ps.close();
			}
			if (conn != null) {
				conn.close();
			}
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		}
	}
}


4、書寫dao包中的UserDaoImpl,實現使用者登入,再寫bookDaoImpl,實現對圖書的操作,程式碼如下

package cn.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import cn.dao.interfaces.IUserDao;
import cn.dao.utils.impl.oracleBaseDao;
import cn.dao.utils.interfaces.IBaseDao;
import cn.po.User;
/**
 * 使用者操作實現類
 * @author CCQ
 *
 */
public class UserDaoImpl implements IUserDao {
	IBaseDao baseDao = new oracleBaseDao();

	@Override
	public User login(User user) throws Exception {
		// TODO Auto-generated method stub
		User users = null;
		try {
			Connection conn = baseDao.getConn();
			String sql = "select * from users where username = ? and userpwd = ?";
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setObject(1, user.getUserName());
			ps.setObject(2, user.getUserPwd());
			ResultSet rs = ps.executeQuery();
			if(rs.next()){
				users = new User();
				users.setUserId(rs.getInt("userId"));
				users.setUserName(rs.getString("userName"));
				users.setUserPwd(rs.getString("userPwd"));
			}
			baseDao.closeConn(rs, ps, conn);
		} catch (Exception e) {
			throw new Exception(e);
		}
		return users;
	}

}
package cn.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import cn.dao.interfaces.IBookDao;
import cn.dao.utils.impl.oracleBaseDao;
import cn.dao.utils.interfaces.IBaseDao;
import cn.po.Book;

/**
 * 對圖書操作的實現類
 * 
 * @author CCQ
 * 
 */
public class BookDaoImpl implements IBookDao {
	IBaseDao baseDao = new oracleBaseDao();
	private Connection conn = null;
	private PreparedStatement ps = null;
	private ResultSet rs = null;
	int r = 0;

	@Override
	public List<Book> findBooks() throws Exception {
		// TODO Auto-generated method stub
		List<Book> books = new ArrayList<Book>();
		try {
			conn = baseDao.getConn();
			String sql = "select * from books order by bookid";
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				Book book = new Book();
				book.setBookId(rs.getInt("bookId"));
				book.setBookName(rs.getString("bookName"));
				book.setBookAutor(rs.getString("bookAutor"));
				book.setBookType(rs.getString("bookType"));
				book.setBookPrice(rs.getDouble("bookPrice"));
				books.add(book);
			}
			baseDao.closeConn(rs, ps, conn);
		} catch (Exception e) {
			throw new Exception(e);
		}
		return books;
	}

	@Override
	public int addBook(Book book) throws Exception {
		// TODO Auto-generated method stub
		try {
			conn = baseDao.getConn();
			String sql = "insert into books values(?,?,?,?,?)";
			ps = conn.prepareStatement(sql);
			ps.setObject(1, book.getBookId());
			ps.setObject(2, book.getBookName());
			ps.setObject(3, book.getBookAutor());
			ps.setObject(4, book.getBookType());
			ps.setObject(5, book.getBookPrice());
			r = ps.executeUpdate();
			baseDao.closeConn(rs, ps, conn);
		} catch (Exception e) {
			throw new Exception(e);
		}
		return r;
	}

	@Override
	public Book findBookByBookName(String bookName) throws Exception {
		// TODO Auto-generated method stub
		Book book = null;
		try {
			conn = baseDao.getConn();
			String sql = "select * from books where bookname = ?";
			ps = conn.prepareStatement(sql);
			ps.setObject(1, bookName);
			rs = ps.executeQuery();
			if (rs.next()) {
				book = new Book();
				book.setBookId(rs.getInt("bookId"));
				book.setBookName(rs.getString("bookName"));
				book.setBookAutor(rs.getString("bookAutor"));
				book.setBookType(rs.getString("bookType"));
				book.setBookPrice(rs.getDouble("bookPrice"));
			}
			baseDao.closeConn(rs, ps, conn);
		} catch (Exception e) {
			throw new Exception(e);
		}
		return book;
	}

	@Override
	public int deleteBook(String bookName) throws Exception {
		// TODO Auto-generated method stub
		try {
			conn = baseDao.getConn();
			String sql = "delete books where bookname = ?";
			ps = conn.prepareStatement(sql);
			ps.setObject(1, bookName);
			r = ps.executeUpdate();
			baseDao.closeConn(rs, ps, conn);
		} catch (Exception e) {
			throw new Exception(e);
		}
		return r;
	}

	@Override
	public int modifyBook(Book book) throws Exception {
		// TODO Auto-generated method stub
		try {
			conn = baseDao.getConn();
			String sql = "update books set bookname = ?,bookautor = ?,booktype = ?,bookprice = ? where bookid = ?";
			ps = conn.prepareStatement(sql);
			ps.setObject(1, book.getBookName());
			ps.setObject(2, book.getBookAutor());
			ps.setObject(3, book.getBookType());
			ps.setObject(4, book.getBookPrice());
			ps.setObject(5, book.getBookId());
			r = ps.executeUpdate();
			baseDao.closeConn(rs, ps, conn);
		} catch (Exception e) {
			throw new Exception(e);
		}
		return r;
	}
}
5、書寫services包,中的介面類,和實現類,也就是呼叫相應的dao包的中方法(簡單)。

6、書寫一個BookManagerSys類來通過控制檯輸入驗證功能是否正確,程式碼如下:

package cn.test;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import cn.po.Book;
import cn.po.User;
import cn.services.impl.BookServiceImpl;
import cn.services.impl.UserServiceImpl;
import cn.services.interfaces.IBookService;
import cn.services.interfaces.IUserService;

/**
 * 圖書管理系統介面
 * 
 * @author CCQ
 * 
 */
public class BookManagerSys {

	IUserService userService = new UserServiceImpl();
	IBookService bookService = new BookServiceImpl();
	Scanner input = new Scanner(System.in);

	public void menu() throws Exception {
		User user = new User();
		System.out.println("\n\t圖書管理系統登入介面");
		System.out.print("使用者名稱:");
		user.setUserName(input.next());
		System.out.print("密    碼:");
		user.setUserPwd(input.next());
		user = userService.login(user);
		if (user != null) {
			bmenu();
		} else {
			System.out.println("使用者名稱或密碼錯誤,請重新輸入!");
			menu();
		}
	}

	public void bmenu() throws Exception {
		System.out.println("\n\t圖書管理系統主介面");
		System.out.println("1.新增圖書");
		System.out.println("2.檢視所有圖書資訊");
		System.out.println("3.刪除圖書");
		System.out.println("4.修改圖書資訊");
		System.out.println("5.退出系統");
		System.out.print("請選擇(1-5):");
		int op = input.nextInt();
		switch (op) {
		case 1:
			addBook();
			break;
		case 2:
			findBooks();
			break;
		case 3:
			deleteBook();
			break;
		case 4:
			modifyBook();
			break;
		case 5:
			System.out.println("謝謝使用圖書管理系統!");
			System.exit(0);
		default:
			System.out.println("輸入錯誤,請輸入1-5數字");
		}

	}

	public void addBook() throws Exception {
		Book book = new Book();
		System.out.println("\n\t圖書資訊新增介面");
		System.out.print("圖書編號:");
		book.setBookId(input.nextInt());
		System.out.print("圖書名稱:");
		book.setBookName(input.next());
		if (bookService.findBookByBookName(book.getBookName()) == null) {
			System.out.print("圖書作者:");
			book.setBookAutor(input.next());
			System.out.print("圖書類別:");
			book.setBookType(input.next());
			System.out.print("圖書價格:");
			book.setBookPrice(input.nextDouble());
			int r = bookService.addBook(book);
			if (r != 0) {
				System.out.println(book.getBookName() + "新增成功!");
				bmenu();
			}
		} else {
			System.out.println(book.getBookName() + "已經存在,不可重複新增!");
			addBook();
		}

	}

	public void findBooks() throws Exception {
		System.out.println("\n\t\t檢視所有圖書資訊介面");
		System.out.println("圖書編號\t圖書名稱\t作者\t類別\t圖書價格");
		List<Book> books = new ArrayList<Book>();
		books = bookService.findBooks();
		for (Book book : books) {
			System.out.println(book.getBookId() + "\t" + book.getBookName()
					+ "\t" + book.getBookAutor() + "\t" + book.getBookType()
					+ "\t" + book.getBookPrice());
		}
		bmenu();
	}

	public void deleteBook() throws Exception {
		System.out.println("\n\t\t刪除圖書資訊介面");
		System.out.print("請輸入刪除圖書名稱:");
		String bookName = input.next();
		if (bookService.findBookByBookName(bookName) != null) {
			int r = bookService.deleteBook(bookName);
			if(r != 0){
				System.out.println("刪除"+bookName+"成功!");
				bmenu();
			}
		} else {
			System.out.println("您輸入的"+bookName+"不存在!");
			bmenu();
		}
	}

	public void modifyBook() throws Exception {
		Book book = new Book();
		System.out.println("\n\t\t修改圖書資訊介面");
		System.out.print("請輸入修改的圖書名稱:");
		book.setBookName(input.next());
		if (bookService.findBookByBookName(book.getBookName()) != null) {
			book.setBookId(bookService.findBookByBookName(book.getBookName()).getBookId());
			System.out.print("請輸入修改的圖書作者:");
			book.setBookAutor(input.next());
			System.out.print("請輸入修改的圖書類別:");
			book.setBookType(input.next());
			System.out.print("請輸入修改的圖書價格:");
			book.setBookPrice(input.nextDouble());
			int r = bookService.modifyBook(book);
			if(r != 0){
				System.out.println("修改"+book.getBookName()+"成功!");
				bmenu();
			}else{
				System.out.println("修改失敗!");
			}
			
		} else {
			System.out.println("您輸入的"+book.getBookName()+"不存在!");
			bmenu();
		}
	}

}

7、結果展示:





8、總結:

經過這一個星期的學習,具體把Java中的面對物件的思想進行了深刻的掌握,然後學習Oracle中的簡單建表和一些簡單地SQL的增刪改查!感覺掌握的還是不錯的,Oracle資料庫用著還是比較的順手,感覺很好用,下階段我要繼續加油哦!加油!加油!加油!重要的事情說三遍!