java+sql server 数据库实现英才大学图书管理系统 第二部分数据库控制实现

下载地址:https://download.csdn.net/download/u011612364/13985581

package YLBlibrary;
//作者 杨丽冰 201831064402

先上数据库设计
在这里插入图片描述
用户
在这里插入图片描述
查询效果:
在这里插入图片描述
在这里插入图片描述

具体代码实现:
图书类Book.java

package YLBlibrary;
//作者 swpu杨丽冰 201831064402
public class Book {
    
    
	private int Bno;
	private String name;//书名
	private String author;//作者
	private String borrowerId;//借阅证号
	private String borrowerName;//借阅者姓名
	private double price;//价格
	public Book() {
    
    
	}
	public Book(String name, double price) {
    
    
		this.name = name;
		this.price = price;
	}
	public int getBno() {
    
    
		return Bno;
	}
	public void setBno(int Bno) {
    
    
		this.Bno = Bno;
	}
	public String getName() {
    
    
		return name;
	}
	public void setName(String name) {
    
    
		this.name = name;
	}
	public double getPrice() {
    
    
		return price;
	}
	public String getAuthor() {
    
    
		return author;
	}
	public void setAuthor(String author) {
    
    
		this.author = author;
	}
	public void setPrice(double price) {
    
    
		this.price = price;
	}
	//借书人id
	public String getBorrowerId() {
    
    
		return borrowerId;
	}
	//借书人姓名
	public void setBorrowerId(String borrowerId) {
    
    
		this.borrowerId = borrowerId;
	}
	public String getBorrowerName() {
    
    
		return borrowerName;
	}
	//借书人姓名
	public void setBorrowerName(String borrower) {
    
    
		this.borrowerName = borrower;
	}
}

用户类

package YLBlibrary;
//作者 杨丽冰 201831064402

import java.util.List;

import javax.swing.JTextArea;

public class User {
    
    
	String ID;// 用户
	String userName;// 密码
	String passwd;
	String phone;
	// 获取ID
	public String getID() {
    
    
		return ID;
	}
	public void setID(String iD) {
    
    
		ID = iD;
	}
	public String getUser() {
    
    
		return userName;
	}
	public void setUser(String userName) {
    
    
		this.userName = userName;
	}
	public String getPasswd() {
    
    
		return passwd;
	}
	public void setPasswd(String passwd) {
    
    
		this.passwd = passwd;
	}
	public String getPhone() {
    
    
		return phone;
	}
	public void setPhone(String phone) {
    
    
		this.phone = phone;
	}
	
	//查询
	public void qury(JTextArea text,String mode,String name) throws Exception {
    
    
		text.setText(" ");
		text.append("书本编号\t书名\t价格\t作者\t借阅证号\t借阅者姓名\n");
		String str = null;
		if(mode.equals("name")) {
    
    
			str ="select "
					+ "Bno, book_name, book_price, book_author,borrower_id, borrower_name "
					+ "from tb_books where book_name like '%"+name+"%'";
		}
		if(mode.equals("aname")) {
    
    
			str ="select "
					+ "Bno, book_name, book_price, book_author,borrower_id, borrower_name "
					+ "from tb_books where book_author like '%"+name+"%'";
		}
		SqlSlove query = new SqlSlove();
		//query.query(text,str);
		List<Book> list = query.query(str);
		for(int i = 0; i < list.size(); i++) {
    
    		
			Book book = (Book)list.get(i);
			String price = String.format("%.2f",book.getPrice());
			String s = book.getBno()+"\t"+book.getName()+"\t"+price+"\t"+book.getAuthor()+"\t"+book.getBorrowerId()+"\t"+book.getBorrowerName();
			System.out.println(s);
			text.append(s+"\n");
		}	   	
	}
	
	//借书
	public int lend(int Bno,String name,String id) throws Exception {
    
    
		String str ="select "
				+ "Bno, book_name, book_price, book_author,borrower_id, borrower_name "
				+ "from tb_books where Bno="+Bno;
		SqlSlove query = new SqlSlove();
		List<Book> list = query.query(str);
		if(list.size() == 0) {
    
    
			return 0;
		}
		else {
    
    
			Book book =(Book)list.get(0);
			book.setBorrowerId(id);
			book.setBorrowerName(name);
			System.out.println(book.getBno()+"\t"+book.getName()+"\t"+book.getPrice()+"\t"+book.getAuthor()+"\t"+book.getBorrowerId()+"\t"+book.getBorrowerName());
			SqlSlove slo=new SqlSlove();
			slo.changeBook(book);
			return 1;
		}
	}
	//还书
	public int borrow(int Bno,String id) throws Exception {
    
    
		String str ="select "
				+ "Bno, book_name, book_price, book_author,borrower_id, borrower_name "
				+ "from tb_books where Bno="+Bno;
		SqlSlove query = new SqlSlove();
		List<Book> list = query.query(str);
		if(list.size() == 0) {
    
    
			return 0;
		}
		else {
    
    
			Book book =(Book)list.get(0);
			if(Bno == book.getBno()&& id.equals(book.getBorrowerId())) {
    
    
				System.out.println(book.getBno()+"\t"+book.getName()+"\t"+book.getPrice()+"\t"+book.getAuthor()+"\t"+book.getBorrowerId()+"\t"+book.getBorrowerName());
				SqlSlove slo=new SqlSlove();
				book.setBorrowerId(null);
				book.setBorrowerName(null);
				slo.changeBook(book);
			}
			return 1;
		}
	}
}

管理员类

package YLBlibrary;

import java.sql.SQLException;
import java.util.List;

public class Ad extends User {
    
    
	public Ad() {
    
    
	}
	//添加
	public void addbook(int id,String name,String author,double price )  {
    
     
		SqlSlove addb = new SqlSlove();
		Book book = new Book();
		book.setBno(id);  
	    book.setName(name);
	    book.setPrice(price);
	    book.setAuthor(author);
		try {
    
    
			addb.addBook(book);
		} catch (Exception e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//删除
	public void deletebook(int id) {
    
    
		SqlSlove deleteb = new SqlSlove();
		Book book = new Book();
		book.setBno(id);
		try {
    
    
			deleteb.delBook(id);
		} catch (SQLException e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//更新
	public void updatebook(int id,String name,String price,String author) throws Exception  {
    
     
		String str ="select "
				+ "Bno, book_name, book_price, book_author,borrower_id, borrower_name "
				+ "from tb_books where Bno="+id;
		SqlSlove slo = new SqlSlove();
		List<Book> list = slo.query(str);
		Book book =(Book)list.get(0);
		if(name!=null && !name.equals(" ")) 
			book.setName(name);
		if(price!=null&& !price.equals(" ")) {
    
    
			double pri = Double.parseDouble(price);
			book.setPrice(pri);
		}
		if(author!=null&& !author.equals(" "))
	    book.setAuthor(author);
		try {
    
    
			slo.changeBook(book);
		} catch (Exception e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

登录控制类

package YLBlibrary;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;

//作者 杨丽冰 201831064402
public class Contro {
    
    
	//注册用户
	public int adduser(String id,String passwd,String name,String phone )  {
    
     
		SqlSlove addb = new SqlSlove();
		User user = new User();
		user.setID(id); 
		user.setUser(name);
	    user.setPasswd(passwd);
	    user.setPhone(phone);
		try {
    
    
			addb.addUser(user);
		} catch (Exception e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
		System.out.println("aa"+user.getID()+user.getPasswd()+user.getUser()+user.getPhone());
		return 1;
	}
	
	//登录验证
	public int loginConf(String id,String pwd)  {
    
     
		SqlSlove addb = new SqlSlove();
		int state;
		try {
    
    
			state = addb.conUser(id, pwd);
		} catch (Exception e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
		if(state == 1)
			return 1;
		else
			return 0;
	}
	
	//查找密码
	public void FindPaw(User user) throws Exception {
    
    
		SqlSlove slo = new SqlSlove();
		slo.FindUser(user.getID(), user.getPasswd());
	}
}

数据库操作类,实现了java对数据库的操作

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

import javax.swing.JTextArea;
 
public class SqlSlove {
    
    
	ResultSet rs = null;
	Statement stmt = null;
	//添加图书
    public void addBook(Book book) throws Exception{
    
    
        Connection con = SqlConnect.getConnection();
        String sql="insert into tb_books"
        		+ "(Bno, book_name, book_price, book_author,borrower_id, borrower_name) "
        		+ "values(?,?,?,?,?,?)";
        PreparedStatement psmt = con.prepareStatement(sql);
        // 先对应SQL语句,给SQL语句传递参数
        psmt.setInt(1,book.getBno());
        psmt.setString(2, book.getName());
        psmt.setDouble(3, book.getPrice());
        psmt.setString(4, book.getAuthor()); 
        if (book.getBorrowerId() == null || book.getBorrowerId() == "") {
    
    
        	psmt.setString(5, null);
        }
        else {
    
    
        	  psmt.setString(5, book.getBorrowerId());
        }
        if (book.getBorrowerName() == null || book.getBorrowerName() == "") {
    
    
        	psmt.setString(6, null);
        }
        else {
    
    
        	  psmt.setString(6, book.getBorrowerId());
        }
        psmt.execute();
    }
	//删除图书
      public void delBook(int ID) throws SQLException{
    
    
    	  // 首先拿到数据库的连接
          Connection con=SqlConnect.getConnection();
          String sql="" + 
                  "DELETE FROM tb_books "+               
                  "WHERE Bno = ?";
          PreparedStatement psmt = con.prepareStatement(sql);
          psmt.setInt(1, ID);
          // 执行SQL语句
          psmt.execute();    
      }
	  
      //添加用户
      public void addUser(User user) throws Exception{
    
    
          Connection con = SqlConnect.getConnection();
          String sql="insert into tb_users"
          		+ "(ID,passwd,userName, phone) "
          		+ "values(?,?,?,?)"; 
          PreparedStatement psmt = con.prepareStatement(sql);
          // 先对应SQL语句,给SQL语句传递参数
          psmt.setString(1,user.getID());
          psmt.setString(2, user.getPasswd());
          if (user.getUser() == null|| user.getUser() == "") {
    
    
          	psmt.setString(3, null);
          }
          else {
    
    
          	  psmt.setString(3, user.getUser());
          }
          if (user.getPhone() == null || user.getPhone() == "") {
    
    
            	psmt.setString(4, null);
            }
            else {
    
    
            	  psmt.setString(4, user.getPhone());
            }
          psmt.execute();
      }
      
      //登录确认
      public int conUser(String ID,String pwd) throws Exception{
    
    
          Connection con = SqlConnect.getConnection();
          String sql="select  passwd from tb_users where ID = '"+ID+"'and passwd = '"+pwd+"'";
          Statement stmt = con.createStatement();
          ResultSet rs = stmt.executeQuery(sql);
          if(rs.next()) {
    
    
        	  System.out.println("登录成功");
        	  return 1;
          }
          else {
    
    
        	  System.out.println("登录失败");
        	  return 0;
          }
      }
      
      //修改密码
      public void FindUser(String id,String pwd) throws Exception{
    
    
          Connection con = SqlConnect.getConnection();
          String sql="Update tb_users set  passwd = ?  where ID = ? ";
          PreparedStatement psmt = con.prepareStatement(sql);
          // 先对应SQL语句,给SQL语句传递参数
          psmt.setString(1,pwd);
          psmt.setString(2,id);
          System.out.println("触发修改");
          psmt.execute();
      }
      
	 // 更新图书信息
    public void changeBook(Book book) throws SQLException{
    
    
    	// 首先拿到数据库的连接
        Connection con=SqlConnect.getConnection();
        String sql="update tb_books set Bno = ?, book_name = ? , book_price = ? , book_author = ?,borrower_id = ?, borrower_name = ? where Bno = ?  ";
        PreparedStatement psmt = con.prepareStatement(sql);
        // 先对应SQL语句,给SQL语句传递参数
        System.out.println(book.getAuthor());
        System.out.println(book.getBno());
        psmt.setInt(1,book.getBno());
        psmt.setString(2,book.getName());
        psmt.setDouble(3,book.getPrice());
        psmt.setString(4,book.getAuthor());
        psmt.setString(5,book.getBorrowerId());
        psmt.setString(6,book.getBorrowerName());
        psmt.setInt(7,book.getBno());
        // 执行SQL语句
        psmt.execute();    
    }
 
    //查询图书信息
	public List<Book> query(String sql) throws Exception{
    
    
		Connection con = SqlConnect.getConnection();	        
		Statement stmt = con.createStatement();	
		ResultSet rs = stmt.executeQuery(sql);
		List<Book> bookList = new ArrayList<Book>();
		Book book = null;	   
		// 如果对象中有数据,就会循环打印出来
		while (rs.next()){
    
    	
			String str = rs.getString(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3)+"\t"+rs.getString(4)+"\t"+rs.getString(5)+"\t"+rs.getString(6);
			book = new Book();	     
			book.setBno(rs.getInt("Bno"));
			book.setName(rs.getString("book_name"));	       
			book.setAuthor(rs.getString("book_author"));
			book.setPrice(rs.getFloat("book_price"));
			book.setBorrowerId(rs.getString("borrower_id"));
			book.setBorrowerName(rs.getString("borrower_name"));
			bookList.add(book);	        
		}	       
		return bookList;	  
	}
}

//数据库连接类

package YLBlibrary;
//作者 杨丽冰 201831064402
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class SqlConnect {
    
    
     //System.out.println("数据库原理及应用--杨丽冰 201831064402");  
	 static String connectionUrl = "jdbc:sqlserver://localhost:1433;databaseName=MyDb";  
	 static Connection con = null;  
	 static Statement stmt = null;  
	 static ResultSet rs = null; 
      static{
    
    
    	 System.out.println("正在连接数据库和检索,请等待...");
    	 try {
    
      
         //Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  
         con = DriverManager.getConnection(connectionUrl,"sa","123456");  
      }   
      catch (Exception e) {
    
      
         e.printStackTrace();  
      }  
      }
   // 对外提供一个方法来获取数据库连接	    
 	 public static Connection getConnection(){
    
         
 		 return con;	   
 	 } 
}
          
     

全局类,当前登录用户

package YLBlibrary;

public class Globle {
    
    
	static User userLog = new User();
	void set(User user)
	{
    
    
		userLog = user;
	}
	User get()
	{
    
    
		return userLog;
	}
}

猜你喜欢

转载自blog.csdn.net/u011612364/article/details/111144454