本人是一名大二學生,自學Java做了一個圖書管理系統的數據庫課程設計,能力有限,歡迎大佬指點!
連接數據庫操作
/**
* 建立數據庫連接!!
*/
package util;
import java.sql.*;
public class DBConnection {
public static final String DRIVER ="com.microsoft.sqlserver.jdbc.SQLServerDriver";
public static final String URL="jdbc:sqlserver://127.0.0.1:1433;databaseName=StudentManagement";
public static final String USER="sa";
public static final String PASSWORD="hyc5670526";
Connection conn =null;
public DBConnection(){
try{
Class.forName(DRIVER);
}catch (ClassNotFoundException e) {
e.printStackTrace();
}
try{
conn=DriverManager.getConnection(URL,USER,PASSWORD);
}catch (SQLException e) {
e.printStackTrace();
}
}
public Connection getConnection(){
return this.conn;
}
public void close(){
if(this.conn!=null){
try{
this.conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
DAO類
package dao;
import vo.Borrow;
import util.DateConvert;
import vo.User;
public interface IUserDao {
//插入讀者信息
public boolean doInsert(User user) throws Exception;
//顯示圖書信息
public boolean doSelectAllBook()throws Exception;
//顯示讀者信息
public boolean doSelectAllStudent()throws Exception;
//顯示借閱信息
public boolean doSelectAllBorrow()throws Exception;
//按學號修改讀者信息
public boolean doUpdate(int id,User user)throws Exception;
//按學號刪除讀者信息
public boolean doDelete(int id)throws Exception;
//按圖書編號查找圖書信息
public boolean doFindBook(int id)throws Exception;
//插入借閱信息
public boolean doInsert(Borrow borrow,DateConvert dateconvert)throws Exception;
//刪除借閱信息
public boolean doDelete2(int id)throws Exception;
}
DAOImp類
package dao;
import java.sql.*;
import java.util.Scanner;
import util.DBConnection;
import util.DateConvert;
import vo.Borrow;
import vo.User;
public class IUserDaoImp implements IUserDao{
private DBConnection dbc = null;
private Connection conn = null;
private static String username;
private static String password;
static Scanner input = new Scanner(System.in);
public IUserDaoImp() {
this.dbc = new DBConnection();
this.conn = dbc.getConnection();
}
//插入學生信息!!!
public boolean doInsert(User user) {
boolean flag = false;
PreparedStatement pstmt = null;
String sql = "insert into Student(Student_id,Student_name,Student_phone,Student_department,Borrow_id) values(?,?,?,?,?)";
try {
pstmt = conn.prepareStatement(sql); //模糊查詢
pstmt.setInt(1, user.getId());
pstmt.setString(2, user.getName());
pstmt.setString(3, user.getPhone());
pstmt.setString(4,user.getDepartment());
pstmt.setString(5, user.getB_id());
if (pstmt.executeUpdate() > 0) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return flag;
}
//查找全部圖書信息!!!
public boolean doSelectAllBook(){
Statement stmt =null;
String sql="select * from Book";
try{
stmt=conn.createStatement();
//執行查詢數據庫的SQL語句 ,返回一個結果集(ResultSet)對象。
ResultSet Book = stmt.executeQuery(sql);
System.out.println("圖書編號"+" 書名"+" 作者"+" 出版社");
while(Book.next()){//如果對象中有數據,就會循環打印出來
System.out.printf("%-10s",Book.getString("Book_id"));
System.out.printf("%-30s",Book.getString("Book_name"));
System.out.printf("%-20s",Book.getString("Book_author"));
System.out.printf("%-20s\n",Book.getString("Book_publisher"));
}
}catch (SQLException e) {
e.printStackTrace();
} finally {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return true;
}
//查找全部學生信息!!!
public boolean doSelectAllStudent(){
Statement stmt =null;
String sql="select * from Student";
try{
stmt=conn.createStatement();
ResultSet Student=stmt.executeQuery(sql);
while(Student.next()){
System.out.println("學號:"+Student.getString("Student_id")+"\t"+"姓名:"+Student.getString("Student_name")+"\t"+"聯繫方式:"+Student.getString("Student_phone")+"\t"+"所在院系:"+Student.getString("Student_department")+"\t"+ "借書證號:"+Student.getString("Borrow_id"));
}
}catch(SQLException e){
e.printStackTrace();
}finally{
try{
stmt.close();
}catch(SQLException e){
e.printStackTrace();
}
}
return true;
}
//查找全部借閱信息!!!
public boolean doSelectAllBorrow(){
Statement stmt=null;
String sql="select *from Borrow";
try{
stmt=conn.createStatement();
ResultSet Borrow=stmt.executeQuery(sql);
while(Borrow.next()){
System.out.println("借書證號:"+Borrow.getString("Borrow_id")+"\t"+"學號:"+Borrow.getString("Student_id")+"\t"+"圖書編號:"+Borrow.getString("Book_id")+"\t"+"借書日期:"+Borrow.getString("Borrow_date")+"\t"+"還書日期:"+Borrow.getString("Return_date")+"\t"+"是否續借:"+Borrow.getString("Judge"));
}
}catch(SQLException e){
e.printStackTrace();
}finally{
try{
stmt.close();
}catch(SQLException e){
e.printStackTrace();
}
}
return true;
}
//按學號修改學生信息!!!
public boolean doUpdate(int id,User user){
boolean flag=false;
PreparedStatement pstmt = null;
String sql="UPDATE Student SET Student_name=?,Student_phone=?,Student_department=?,Borrow_id=? WHERE Student_id=?";
try{
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, user.getName());
pstmt.setString(2, user.getPhone());
pstmt.setString(3, user.getDepartment());
pstmt.setString(4, user.getB_id());
pstmt.setInt(5, user.getId());
if (pstmt.executeUpdate() > 0) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return flag;
}
//按學號刪除學生信息!!!
public boolean doDelete(int id){
boolean flag=false;
PreparedStatement pstmt=null;
String sql="DELETE FROM Student WHERE Student_id=?";
try{
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1, id); //接收id傳入sql語句
if (pstmt.executeUpdate() > 0) {// 至少已經更新了一行
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return flag;
}
//按圖書編號查找圖書信息!!!
public boolean doFindBook(int id){
PreparedStatement pstmt=null;
String sql="SELECT Book_id,Book_name,Book_author,Book_publisher FROM Book WHERE Book_id=?";
try{
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1, id);
ResultSet rs=pstmt.executeQuery();
while(rs.next()){
System.out.println("圖書編號:"+rs.getInt(1)+"\t"+"書名:"+rs.getString(2)+"\t"+"圖書作者:"+rs.getString(3)+"\t"+"出版社:"+rs.getString(4));
}
}catch (SQLException e) {
e.printStackTrace();
} finally {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return true;
}
public boolean doInsert(Borrow borrow,DateConvert dateconvert){
boolean flag=false;
PreparedStatement pstmt=null;
String sql="INSERT INTO Borrow(Borrow_id,Student_id,Book_id,Borrow_date,Return_date,Judge) VALUES(?,?,?,?,?,?)";
try{
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1, borrow.getInt1());
pstmt.setInt(2, borrow.getInt2());
pstmt.setInt(3, borrow.getInt3());
pstmt.setString(4, borrow.getDate1());
pstmt.setString(5, borrow.getDate2());
pstmt.setString(6, borrow.getString());
if (pstmt.executeUpdate() > 0) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return flag;
}
public boolean doDelete2(int id){
boolean flag=false;
PreparedStatement pstmt=null;
String sql="DELETE FROM Borrow WHERE Borrow_id=?";
try{
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1, id); //接收id傳入sql語句
if (pstmt.executeUpdate() > 0) {// 至少已經更新了一行
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return flag;
}
public void zhuce() throws SQLException{
System.out.println("請輸入用戶名:");
username=input.next();
System.out.println("請輸入登錄密碼:");
String p1=input.next();
System.out.println("請再次輸入密碼:");
String p2=input.next();
if(p1.equals(p2)){
password=p1;
String sql="INSERT INTO Users(Username,Password) VALUES(?,?)";
PreparedStatement ptmt=conn.prepareStatement(sql);
ptmt.setString(1, username);
ptmt.setString(2, password);
ptmt.execute();
System.out.println("註冊成功!");
}else{
System.out.println("你輸入的兩次密碼不一致,請重新註冊!");
zhuce();
}
}
public void denglu()throws SQLException{
System.out.println("用戶名:");
String username=input.next();
System.out.println("密碼:");
String password=input.next();
String sql="SELECT Username,Password FROM Users WHERE Username=? AND Password=?";
PreparedStatement ptmt=conn.prepareStatement(sql);
ptmt.setString(1, username);
ptmt.setString(2, password);
ResultSet rs=ptmt.executeQuery();
if(rs.next()){
System.out.println("登錄成功!");
}else{
System.out.println("賬號或密碼不正確,請重新登錄!\n");
denglu();
}
}
}
格式化日期
package util;
import java.text.SimpleDateFormat;//SimpleDateFormat 使得可以選擇任何用戶定義的日期-時間格式的模式
import java.util.Calendar;
import java.util.Date;
public class DateConvert {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
public String nowDay(){
Calendar c = Calendar.getInstance();
c.setTime(new Date());
Date d=c.getTime();
String day=format.format(d);
return day;
}
public String nextDay(){
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.MONTH, +1);
Date d = c.getTime();
String month = format.format(d);
return month;
}
}
InputData類
package util;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class InputData {
private BufferedReader buf=null;
public InputData(){
this.buf=new BufferedReader(new InputStreamReader(System.in));
}
public String getString(String info){
String temp=null;
System.out.print(info);
try{
temp=this.buf.readLine();
}catch (IOException e) {
e.printStackTrace();
}
return temp;
}
public int getInt(String info, String err) {
int temp = 0;
String str = null;
boolean flag = true;
while (flag) {
str = this.getString(info);
if (str.matches("^\\d+$")) {
temp = Integer.parseInt(str);//將str轉化爲int
flag = false;
} else {
System.out.println(err);
}
}
return temp;
}
}
JavaBean
package vo;
public class User {
private int student_id;
private String student_name;
private String student_phone;
private String student_department;
private String borrow_id;
public User(){}
public User( int id,String name,String phone,String department,String b_id){
this.student_id=id;
this.student_name=name;
this.student_phone=phone;
this.student_department=department;
this.borrow_id=b_id;
}
public User(int id){
this.student_id=id;
}
public void setId(int id){
this.student_id=id;
}
public int getId(){
return student_id;
}
public void setName(String name){
this.student_name=name;
}
public String getName(){
return student_name;
}
public void setPhone(String phone){
this.student_phone=phone;
}
public String getPhone(){
return student_phone;
}
public void setDepartment(String department){
this.student_department=department;
}
public String getDepartment(){
return student_department;
}
public void setB_id(String b_id){
this.borrow_id=b_id;
}
public String getB_id(){
return borrow_id;
}
public String toString(){
return "學號"+this.getId()+"姓名"+this.getName()+"聯繫方式"+this.student_phone+"院
系"+this.student_department+"借書證號"+this.borrow_id;
}
}
package vo;
public class Borrow {
private int borrow_id;
private int student_id;
private int book_id;
private String borrow_date;
private String return_date;
private String judge;
public Borrow(int borrow_id,int student_id,int book_id,String borrow_date,String return_date,String judge){
this.borrow_id=borrow_id;
this.student_id=student_id;
this.book_id=book_id;
this.borrow_date=borrow_date;
this.return_date=return_date;
this.judge=judge;
}
public void setInt1(int borrow_id){
this.borrow_id=borrow_id;
}
public int getInt1(){
return borrow_id;
}
public void setInt2(int student_id){
this.student_id=student_id;
}
public int getInt2(){
return student_id;
}
public void setInt3(int book_id){
this.book_id=book_id;
}
public int getInt3(){
return book_id;
}
public void setDate1(String borrow_date){
this.borrow_date=borrow_date;
}
public String getDate1(){
return borrow_date;
}
public void setDate2(String return_date){
this.return_date=return_date;
}
public String getDate2(){
return return_date;
}
public void setString(String judge){
this.judge=judge;
}
public String getString(){
return judge;
}
}
Menu類
package view;
import java.util.Scanner;
import java.sql.SQLException;
import java.text.ParseException;
import dao.IUserDaoImp;
import util.InputData;
import vo.User;
import vo.Borrow;
import util.DateConvert;
public class Menu {
IUserDaoImp iu = new IUserDaoImp();
InputData input = new InputData();
DateConvert dc=new DateConvert();
static String MANAGER="hyc";
static String KEY="123456";
public Menu() throws ParseException, SQLException{
while(true){
this.show();
}
}
public void show() throws ParseException, SQLException{
@SuppressWarnings("resource") //不顯示使用了不贊成使用的類或方法時的警告
Scanner iput=new Scanner(System.in);
System.out.println("-----------------------------歡迎使用圖書管理系統------------------------");
System.out.println("1.用戶登錄");
System.out.println("2.用戶註冊");
System.out.println("3.管理員登錄");
int k=iput.nextInt();
switch(k){
case 1:
iu.denglu();
System.out.println("*************************************************************************");
System.out.println("| 1.讀者註冊 |");
System.out.println("| 2.圖書信息 |");
System.out.println("| 3.借書 |");
System.out.println("| 4.還書 |");
System.out.println("| 0.退出系統 |");
System.out.println("*************************************************************************");
System.out.println("請選擇:");
int q=iput.nextInt();
User user=null;
Borrow borrow=null;
DateConvert dateconvert=null;
switch(q){
case 1:
int id=input.getInt("請輸入學號:", "格式不對,請重新輸入!");
String name=input.getString("請輸入人員姓名:");
String phone=input.getString("請輸入聯繫方式:");
String department=input.getString("請輸入院系:");
String b_id=input.getString("請輸入讀書證號:");
user=new User(id,name,phone,department,b_id);
if (iu.doInsert(user)) {
System.out.println("添加成功!");
} else {
System.out.println("添加失敗!");
}break;
case 2:
iu.doSelectAllBook();break;
case 3:
int id5=input.getInt("請輸入借書證號:", "格式不對,請重新輸入!");
int id6=input.getInt("請輸入學號:", "格式不對,請重新輸入!");
int id7=input.getInt("請輸入圖書編號:", "格式不對,請重新輸入!");
String dt1=dc.nowDay();
String dt2=dc.nextDay();
System.out.println("借書時間爲:"+dt1);
System.out.println("還書時間爲:"+dt2);
String judge="否";
borrow=new Borrow(id5,id6,id7,dt1,dt2,judge);
if (iu.doInsert(borrow,dateconvert)) {
System.out.println("借書成功!");
} else {
System.out.println("借書失敗!");
}
break;
case 4:
int id8=input.getInt("請輸入還書同學的借書證號:", "格式不對,請重新輸入!");
if(iu.doDelete2(id8)){
System.out.println("還書成功!");
}else{
System.out.println("還書失敗!");
}
}break;
case 0:
{ System.out.println("謝謝使用!");
System.exit(0);
}
break;
case 2:
iu.zhuce();break;
case 3:
System.out.println("請輸入用戶名:");
String manager=iput.next();
System.out.println("請輸入密碼:");
String key=iput.next();
if(manager.equals(MANAGER)&&key.equals(KEY)){
System.out.println("登入系統成功!");
System.out.println("*************************************************************************");
System.out.println("| 1.圖書信息 |");
System.out.println("| 2.讀者信息 |");
System.out.println("| 3.借閱信息 |");
System.out.println("| 4.修改讀者信息 |");
System.out.println("| 5.刪除讀者信息 |");
System.out.println("| 6.查找圖書信息 |");
System.out.println("| 0.退出系統 |");
System.out.println("*************************************************************************");
System.out.println("請輸入一個數字:");
int p=iput.nextInt();
switch(p){
case 1:
iu.doSelectAllBook();break;
case 2:
iu.doSelectAllStudent();break;
case 3:
iu.doSelectAllBorrow();break;
case 4:
int id2=input.getInt("請輸入學號:", "格式不對,請重新輸入!");
String name2=input.getString("請修改人員姓名:");
String phone2=input.getString("請修改聯繫方式:");
String department2=input.getString("請修改院系:");
String b_id2=input.getString("請修改讀書證號:");
user=new User(id2,name2,phone2,department2,b_id2);
if(iu.doUpdate(id2, user)){
System.out.println("修改成功!");
}else{
System.out.println("修改失敗!");
}break;
case 5:
int id3=input.getInt("請輸入要刪除學生的學號:", "格式不對,請重新輸入!");
if(iu.doDelete(id3)){
System.out.println("刪除成功!");
}else{
System.out.println("刪除失敗!");
}break;
case 6:
int id4=input.getInt("請輸入要查找的圖書編號:", "格式不對,請重新輸入!");
iu.doFindBook(id4);
break;
case 0:
System.out.println("謝謝使用!");
System.exit(0);
break;
default:
System.out.println("請選擇正確的操作!");
}
break;
}
else{
System.out.println("賬號或密碼不正確!");
}
}
}
}
主方法
package driver;
import java.sql.SQLException;
import java.text.ParseException;
import view.Menu;
public class BookManager {
public static void main(String[] args) throws ParseException, SQLException {
new Menu();
}
}
數據庫爲SQL server2008
歡迎各位大神前來指導!!!!!!!!!!!!