图书管理系统(JDBC)

public class BookSystem{   //图书系统管理类

 private Scanner sc =new Scanner(System.in);

public static void main(String[] args){

           BookSystem bs = new BookSystem();

           bs.start();

}

public void start(){

while (true){

System.out.println("请输入要执行的命令");
System.out.println("-----------------------------------------------------");
System.out.println("---------------------图书管理系统---------------------");
System.out.println(" 1.增加图书 2.查找图书");
System.out.println(" 3.修改图书 4.删除图书");
System.out.println(" 5.列出所有图书 6.退出系统");
System.out.println("----------------------------------------------------");
int command = sc.nextInt();

if (command==1){
insertNewBook();
}else if (command==2){
updateBook();

}else if (command==3){
deleteBook();

}else if (command==4){
selectBook();

}else if (command==5){
listAll();
}else {
System.out.println("退出系统");
}
public void insertNewBook(){
System.out.println("请输入书的名字");
String bookName = sc.next();
System.out.println("请输入书的作者");
String author = sc.next();
System.out.println("请输入书的ISBN");
String isbn = sc.next();

Book book = new Book();
book.setName(bookName);
book.setAuthor(author);
book.setIsbn(isbn);

//把book对象交给数据库来处理
int result = BookDB.insertBook(book);
if (result>0){
System.out.println("添加成功");
}else {
System.out.println("添加失败");
}
}

public void updateBook(){
System.out.println("请输入修改的书名");
String bookname = sc.next();
Book book =new Book();
book.setName(bookname);
int result = BookDB.updateBook(book);
if(result>0){
System.out.println("修改成功");
}else{
System.out.println("修改失败");
}
}
public void deleteBook(){
System.out.println("请输入要删除的书名");
String newbook = sc.next();
Book book =new Book();
book.setName(newbook);
int result = BookDB.deleteBook(book);
if (result>0){
System.out.println("删除成功");
}else {
System.out.println("删除失败");
}
}

public void selectBook(){
Book book = BookDB.selectBook();
System.out.println(book);

}
public void listAll(){
List<Book> list = BookDB.listAll();
if (list.size() == 0){
System.out.println("暂无书籍信息");
System.out.println("是否要添加新的书籍:Y/N");
String input = sc.next();
if ("y".equals(input.toLowerCase())){
insertNewBook();
}
}
for (Book book : list) {
System.out.println(book.getBid()+":"+book.getName());
}
}

}
public class BookDB { //专门处理图书管理,数据部分

public static int insertBook(Book book) {
String sql = "insert into book(name,author,isbn) values (?,?,?)";
return DBUtils.update(sql,book.getName(),book.getAuthor(),book.getIsbn());
}

public static List<Book> listAll(){
Connection conn = DBUtils.getConn();
String sql ="select * from book";
PreparedStatement stmt=null;
ResultSet rs=null;

List<Book> list = new ArrayList<>();
try {
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
while (rs.next()){
Book book = new Book();
book.setName(rs.getString("name"));
book.setAuthor(rs.getString("author"));
book.setIsbn(rs.getString("isbn"));
book.setBid(rs.getInt("id"));
list.add(book);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtils.closeAll(conn,stmt,rs);
}
return list;
}

public static int updateBook(Book book){
String sql ="update book set name=? where name=? ";
System.out.println("请输入修改的书名");
Scanner sc = new Scanner(System.in);
return DBUtils.update(sql,book.getName(),sc.next());
}

public static Book selectBook(){
Connection conn=null;
PreparedStatement stmt=null;
ResultSet rs=null;
conn = DBUtils.getConn();
String sql ="select * from book where name=?";
try {
stmt = conn.prepareStatement(sql);
Scanner sc =new Scanner(System.in);
System.out.println("请输入查找的书名");
stmt.setString(1,sc.next());
rs = stmt.executeQuery();
while (rs.next()){
Book book1 = new Book();
book1.setName(rs.getString("name"));
book1.setAuthor(rs.getString("author"));
book1.setIsbn(rs.getString("isbn"));
book1.setBid(rs.getInt("id"));
return book1;
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtils.closeAll(conn,stmt,rs);
}
return null;
}

public static int deleteBook(Book book){
String sql ="delete from book where name =?";
System.out.println("请输入删除的书名");
Scanner sc = new Scanner(System.in);
return DBUtils.update(sql,sc.next());
}

}
public class DBUtils {// 工具类
public static void closeAll(Connection conn, PreparedStatement stmt, ResultSet rs) {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
// 创建一个数据库连接(封装方法)
public static Connection getConn() {
// url规定数据库连接的类型,地址,端口,和数据库名,后面可以接参数
String url = "jdbc:mysql://localhost:3306/j0302?useSSL=true";
String username = "root";
String password = "123456";
String driver = "com.mysql.jdbc.Driver";
try {
Class.forName(driver);
Connection conn =
DriverManager.getConnection(url, username, password);
return conn;
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new RuntimeException("驱动未找到");
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("数据库连接失败");
}
}
// 不定长参数
public static int update(String sql, Object... obArray){
Connection conn = DBUtils.getConn();
PreparedStatement stmt=null;
try {
stmt = conn.prepareStatement(sql);
for (int i = 0; i < obArray.length; i++) {
stmt.setObject(i+1,obArray[i]);
}
int result = stmt.executeUpdate();
return result;
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtils.closeAll(conn,stmt,null);
}
// 如果代码执行到这个位置,那么肯定是出了异常
// 这个时候直接返回0;
return 0;
}
}
 
 





原文地址:https://www.cnblogs.com/xuzhendong-0302/p/10627877.html