jdbc图书管理系统
图书管理系统实现
1级菜单
1管理员登录2用户注册登录3退出系统
2级菜单
管理员1录入书籍2查看所有书籍3根据图书编号修改价格4删除书籍5根据书名查找书籍6查看所有用户7根据姓名搜索用户8注销用户9管理员退出
用户
1注册2登录3退出
用户2级菜单
1查询所有书籍2修改密码3根据书名搜索4查看注册时间5退出登录
控制类
package bookjdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; public class BookSystem { public static void main(String[] args) throws Exception { Scanner scanner=new Scanner(System.in); finish: while(true) { System.out.println("1管理员登录2用户注册登录3退出系统"); String input=scanner.nextLine(); over:switch (input) { case "1": System.out.println("请输入管理员账号"); String adminName=scanner.nextLine(); System.out.println("请输入管理员密码"); String adminPass=scanner.nextLine(); if(adminName.equals("admin")&&adminPass.equals("123")) { System.out.println("管理员登录成功"); }else {System.out.println("管理员登录失败"); break; } out: while(true) { System.out.println("欢迎进入管理员页面1录入书籍2查看所有书籍3根据图书编号修改价格4删除书籍5根据书名查找书籍6查看所有用户7根据姓名搜索用户8注销用户9管理员退出"); input=scanner.nextLine(); switch (input) { case "1": System.out.println("请输入书籍名称"); String bookname=scanner.nextLine(); System.out.println("请输入书籍作者"); String bookauthor=scanner.nextLine(); System.out.println("请输入书籍价格"); int bookprice=scanner.nextInt(); scanner.nextLine(); Book book=new Book(bookname,bookauthor,bookprice); BookUtils.enter(book); break; case "2": BookUtils.selectAll(); break; case "3": BookUtils.updatePrice(); break; case "4": BookUtils.delete(); break; case "5": BookUtils.selectOne(); break; case "6": UserUtils.selectAll(); break; case "7": UserUtils.selectOne(); break; case "8": UserUtils.delete(); break; case "9": break out; default: break; } } break; case "2": while(true) { System.out.println("1注册2登录3退出"); input=scanner.nextLine(); oo: switch (input) { case "1": System.out.println("请输入姓名"); String userName=scanner.nextLine(); System.out.println("请输入密码"); String userPwd=scanner.nextLine(); User user=new User(userName,userPwd); UserUtils.register(user); break; case "2": System.out.println("请输入姓名"); userName=scanner.nextLine(); System.out.println("请输入密码"); userPwd=scanner.nextLine(); Class.forName("com.mysql.jdbc.Driver"); Connection connection=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/booksys?useUnicode=true&characterEncoding=utf-8","root","root123"); String sql="select * from user_table where user_name=? and user_pwd=?"; PreparedStatement statement=(PreparedStatement)connection.prepareStatement(sql); statement.setString(1, userName); statement.setString(2, userPwd); ResultSet set=statement.executeQuery(); int count=0; while(set.next()) { count++; } if(count>0) { System.out.println("登录成功"); }else {System.out.println("登录失败"); break ; } if(count>0) { while(true) { System.out.println("1查询所有书籍2修改密码3根据书名搜索4查看注册时间5退出登录"); input=scanner.nextLine(); switch (input) { case "1": BookUtils.selectAll(); break; case "2": UserUtils.update(); break; case "3": BookUtils.selectOne(); break; case "4": UserUtils.selectTime(); break; case "5": break oo; default: break;} } } break; case"3": break over; default: break;} } case "3": break finish; default: break; } } } }
数据处理类 图书口
package bookjdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class BookUtils {
static Scanner scanner=new Scanner(System.in);
static Connection connection;
static {
try {
Class.forName("com.mysql.jdbc.Driver");
connection=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/booksys?useUnicode=true&characterEncoding=utf-8","root","root123");
}catch(Exception e){
e.printStackTrace();
}
}
public static void enter(Book book) throws Exception {
String sql="insert into book_table values(null,?,?,?)";
PreparedStatement statement=(PreparedStatement)connection.prepareStatement(sql);
statement.setString(1, book.getBookName());
statement.setString(2, book.getBookAuthor());
statement.setInt(3, book.getBookPrice());
statement.executeUpdate();
System.out.println("录入成功");
}
public static void selectAll() throws Exception {
String sql="select * from book_table";
PreparedStatement statement=(PreparedStatement)connection.prepareStatement(sql);
ResultSet set=statement.executeQuery();
while(set.next()) {
System.out.print(set.getString("book_name")+" ");
System.out.print(set.getString("book_author")+" ");
System.out.print(set.getInt("book_price")+" ");
System.out.println();
}
}
public static void updatePrice() throws Exception {
System.out.println("请输入书籍名称");
String bookname=scanner.nextLine();
System.out.println("请输入要更改的价格");
int bookprice=scanner.nextInt();
scanner.nextLine();
String sql="update book_table set book_price=? where book_name=?";
PreparedStatement statement=(PreparedStatement)connection.prepareStatement(sql);
statement.setInt(1, bookprice);
statement.setString(2, bookname);
statement.executeUpdate();
System.out.println("价格修改成功");
}
public static void delete() throws Exception{
System.out.println("请输入书籍名称");
String bookname=scanner.nextLine();
String sql="delete from book_table where book_name=?";
PreparedStatement statement=(PreparedStatement)connection.prepareStatement(sql);
statement.setString(1, bookname);
statement.executeUpdate();
System.out.println("删除成功");
}
public static void selectOne() throws Exception {
System.out.println("请输入书籍名称");
String bookname=scanner.nextLine();
String sql="select * from book_table where book_name=?";
PreparedStatement statement=(PreparedStatement)connection.prepareStatement(sql);
statement.setString(1,bookname);
ResultSet set=statement.executeQuery();
while(set.next()) {
System.out.print(set.getString("book_name")+" ");
System.out.print(set.getString("book_author")+" ");
System.out.print(set.getInt("book_price")+" ");
System.out.println();
}
}
}
数据处理类 用户口
package bookjdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class UserUtils {
static Scanner scanner=new Scanner(System.in);
static Connection connection;
static {
try {
Class.forName("com.mysql.jdbc.Driver");
connection=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/booksys?useUnicode=true&characterEncoding=utf-8","root","root123");
}catch(Exception e){
e.printStackTrace();
}
}
public static void selectAll() throws Exception {
String sql="select * from user_table";
PreparedStatement statement=(PreparedStatement)connection.prepareStatement(sql);
ResultSet set=statement.executeQuery();
while(set.next()) {
System.out.print(set.getString("user_name")+" ");
System.out.print(set.getString("user_pwd")+" ");
System.out.print(set.getString("user_time")+" ");
System.out.println();
}
}
public static void selectOne() throws Exception {
System.out.println("请输入用户姓名");
String username=scanner.nextLine();
String sql="select * from user_table where user_name=?";
PreparedStatement statement=(PreparedStatement)connection.prepareStatement(sql);
statement.setString(1,username);
ResultSet set=statement.executeQuery();
while(set.next()) {
System.out.print(set.getString("user_name")+" ");
System.out.print(set.getString("user_pwd")+" ");
System.out.print(set.getString("user_time")+" ");
System.out.println();
}
}
public static void selectTime() throws Exception {
System.out.println("请输入用户姓名");
String username=scanner.nextLine();
String sql="select * from user_table where user_name=?";
PreparedStatement statement=(PreparedStatement)connection.prepareStatement(sql);
statement.setString(1,username);
ResultSet set=statement.executeQuery();
while(set.next()) {
System.out.println(set.getString("user_time"));
}
}
public static void delete() throws Exception{
System.out.println("请输入用户名称");
String username=scanner.nextLine();
String sql="delete from user_table where user_name=?";
PreparedStatement statement=(PreparedStatement)connection.prepareStatement(sql);
statement.setString(1, username);
statement.executeUpdate();
System.out.println("删除成功");
}
public static void update() throws Exception {
System.out.println("请输入姓名");
String userName=scanner.nextLine();
System.out.println("请输入要更改的密码");
String userPwd=scanner.nextLine();
String sql="update user_table set user_pwd=? where user_name=?";
PreparedStatement statement=(PreparedStatement)connection.prepareStatement(sql);
statement.setString(1, userPwd);
statement.setString(2, userName);
statement.executeUpdate();
System.out.println("密码修改成功");
}
public static void register(User user) throws Exception {
String sql="insert into user_table values(null,?,?,?)";
PreparedStatement statement=(PreparedStatement)connection.prepareStatement(sql);
statement.setString(1, user.getUserName());
statement.setString(2, user.getUserPwd());
statement.setObject(3, new Date(System.currentTimeMillis()));
statement.executeUpdate();
System.out.println("注册成功");
}
}
图书属性类
package bookjdbc;
public class Book {
String bookName;
String bookAuthor;
int bookPrice;
int bookId;
public int getBookId() {
return bookId;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getBookAuthor() {
return bookAuthor;
}
public void setBookAuthor(String bookAuthor) {
this.bookAuthor = bookAuthor;
}
public int getBookPrice() {
return bookPrice;
}
public void setBookPrice(int bookPrice) {
this.bookPrice = bookPrice;
}
public Book(String bookName, int bookPrice) {
super();
this.bookName = bookName;
this.bookPrice = bookPrice;
}
public Book(String bookName, String bookAuthor, int bookPrice) {
super();
this.bookName = bookName;
this.bookAuthor = bookAuthor;
this.bookPrice = bookPrice;
}
public Book() {
super();
}
@Override
public String toString() {
return "Book [bookName=" + bookName + ", bookAuthor=" + bookAuthor + ", bookPrice=" + bookPrice + "]";
}
}
用户属性类
package bookjdbc;
public class User {
String userName;
String userPwd;
String userTime;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
public String getUserTime() {
return userTime;
}
public void setUserTime(String userTime) {
this.userTime = userTime;
}
public User(String userName, String userPwd, String userTime) {
super();
this.userName = userName;
this.userPwd = userPwd;
this.userTime = userTime;
}
public User(String userName, String userPwd) {
super();
this.userName = userName;
this.userPwd = userPwd;
}
public User() {
}
@Override
public String toString() {
return "User [userName=" + userName + ", userPwd=" + userPwd + ", userTime=" + userTime + "]";
}
}
浙公网安备 33010602011771号