经历了两周终于使用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