JavaSE与JDBC学完后的java小项目

经历了两周终于使用javaSE和JDBC把图书管理小程序写完了功能:管理员:查看书籍,添加书籍,修改书籍,删除书籍,管理账户,查看 账户,用户:查看所有书籍信息,查看自己想查找的书籍的信息,借阅书籍,归还书籍
摘要由CSDN通过智能技术生成

经历了两周终于使用javaSE和JDBC把图书管理小程序写完了
功能:
管理员:查看书籍,添加书籍,修改书籍,删除书籍,管理账户,查看 账户,
用户:查看所有书籍信息,查看自己想查找的书籍的信息,借阅书籍,归还书籍
Account_Service类

package test01;

import org.junit.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
//提供账号注册、账号登录.
public class Account_Service {

    //查看所有用户
    @Test
    public void allAccount(){
        Connection collection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            collection = JDBCUtils.getCollection();
            //sql语句
            String sql = "select * from account";
            preparedStatement = collection.prepareStatement( sql );
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                String id = resultSet.getString( 1);
                String password = resultSet.getString( 2 );
                System.out.println("id:"+id+"\t"+"password:"+password);
            }
        } catch (SQLException e) {
            e.printStackTrace ();
        }finally {
            JDBCUtils.close(resultSet,preparedStatement,collection);
        }
    }
    //用户注册账户
    @Test
    public void addAccount(String name,String password){
        Connection collection = null;
        PreparedStatement preparedStatement = null;
        try {
            collection = JDBCUtils.getCollection();
            //sql语句
            String sql = "insert into account values(?,?)";
            preparedStatement = collection.prepareStatement( sql );
            //占位符赋值
            preparedStatement.setString( 1,name );
            preparedStatement.setString( 2,password );

            preparedStatement.executeUpdate();
            System.out.println("添加数据成功");
        } catch (SQLException e) {
            e.printStackTrace ();
        }finally {
            JDBCUtils.close(null,preparedStatement,collection);
        }
    }
    @Test
    //用户登录账号
    public boolean loginAccount(String name, String password){
        boolean a = false;
        Connection collection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            collection = JDBCUtils.getCollection();
            //sql语句
            String sql = "select * from account";
            preparedStatement = collection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while(resultSet.next()){
                String Name = resultSet.getString( 1);
                String Password = resultSet.getString( 2 );
               // System.out.println(Name+"   "+Password);
                if (Name.equals(name)){
                    if(Password.equals(password)) {
                        System.out.println( "登录成功!" );
                        a = true;
                    }
                    else {
                        System.out.println( "密码错误!" );
                    }
                }
            }
        } catch (SQLException e) {
            e.printStackTrace ();
        }finally {
            JDBCUtils.close(resultSet,preparedStatement,collection);
        }
        return a;
    }
    //管理员改的密码存入数据库中
    @Test
    public void manageChangePassage(String name,String password){
        Connection collection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            collection = JDBCUtils.getCollection();
            //sql语句
            String sql = "select * from account";
            preparedStatement = collection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();

            while(resultSet.next()){
                String Name = resultSet.getString( 1);
                String Password = resultSet.getString( 2 );
                if (name.equals(Name)){
                    String sql1 = "update account set password = ? where id = ? ";
                    preparedStatement = collection.prepareStatement( sql1 );
                    preparedStatement.setString( 1,password );
                    preparedStatement.setString( 2,name );
                    preparedStatement.executeUpdate();
                    System.out.println("修改成功!");
                    break;
                }
            }

        } catch (SQLException e) {
            e.printStackTrace ();
        }finally {


            JDBCUtils.close(resultSet,preparedStatement,collection);
        }
    }
}

Book_Service类

package test01;

import org.junit.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;


/**
 * @Auther: Gfish
 * @Date: 2022/3/17 - 03 - 17 - 23:39
 * @Description: com.fish.test01
 * @version: 1.0
 */
//图书馆不可能是一本书,,想想使用 什么方法存储书,在那个地方存储书,怎么存储书?
//文件使可以永久的存储的,为了容易读和写书籍,可以分别在自己的方法中创建链表
//主要负责为普通用户提供列出书籍信息,查找书籍、借阅书籍、归还书籍的功能。
public class Book_Servi
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值