使用Java代码关联数据库制作一个简单的图书馆管理系统
1、首先创建一个File ,后缀名为properties方便我们代码中使用关键字,减少代码
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/myschool?useSSL=false
user=root
password=root
2、创建一个自己的工具类,目的同上
package com.huzheng.jdbc;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JDBCUtils {
private static String driver;
private static String url;
private static String user;
private static String password;
// 封装四个参数
static {
//初始化4个参数
Properties properties = new Properties();
try {
properties.load(new FileInputStream("jdbc.properties"));
driver = properties.getProperty("driver");
url = properties.getProperty("url");
user = properties.getProperty("user");
password = properties.getProperty("password");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//加载驱动
static{
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//封装获取连接
public static Connection getConnection() {
try {
return DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
//封装关闭资源
public static void closeAll(ResultSet rs,Statement pstmt,Connection conn) {
try {
if(rs != null) {
rs.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(pstmt != null) {
pstmt.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(conn != null) {
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3、开始写我们的图书馆管理系统
package com.huzheng.demo;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
import com.huzheng.jdbc.JDBCUtils;
public class library {
static Scanner sc = new Scanner(System.in);
//登录
public static void login() {
System.out.println("请输入用户名");
String username = sc.nextLine();
System.out.println("请输入密码");
String password = sc.nextLine();
//获取连接
Connection conn = JDBCUtils.getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null;
//创建SQL执行预平台
String sql = "select * from libraryadmin where uname = ? and pwd = ?";
try {
pstmt = conn.prepareStatement(sql);
//赋值
pstmt.setString(1, username);
pstmt.setString(2, password);
//执行sql
rs = pstmt.executeQuery();
//处理结果
if(rs.next()) {
System.out.println("登陆成功");
menu();
}else {
System.out.println("登陆失败");
login();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JDBCUtils.closeAll(rs, pstmt, conn);
}
}
public static void menu() {
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("0、退出系统");
System.out.println("请输入对应的编号");
String num = sc.nextLine();
switch(num) {
case "1":selectAll();break;
case "2":slectbyID();break;
case "3":add();break;
case "4":change();break;
case "5":delete();break;
case "0":break;
}
}
private static void delete() {
// TODO Auto-generated method stub
System.out.println("请输入你要删除的书id");
String id = sc.nextLine();
Connection conn = JDBCUtils.getConnection();
PreparedStatement pstmt = null;
String sql = "delete from library where id = ?";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, id);
System.out.println("是否确认删除Y/S");
String s = sc.nextLine();
if("y".equalsIgnoreCase(s)) {
int num = pstmt.executeUpdate();
if(num > 0) {
System.out.println("删除成功");
selectAll();
}else {
System.out.println("删除失败");
}
}else {
menu();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JDBCUtils.closeAll(null, pstmt, conn);
}
}
private static void change() {
// TODO Auto-generated method stub
System.out.println("请输入要修改的书id");
String id = sc.nextLine();
Connection conn = JDBCUtils.getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "select * from library where id = ?";
try {
pstmt = conn.prepareCall(sql);
pstmt.setString(1, id);
rs = pstmt.executeQuery();
if(rs.next()) {
System.out.println(rs.getInt("id")+"\t"+rs.getString("name")+"\t"+rs.getString("writer")+"\t"+rs.getString("price"));
show(id);
}else {
System.err.println("查无此书");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JDBCUtils.closeAll(rs, pstmt, conn);
}
}
private static void show(String id) {
// TODO Auto-generated method stub
System.out.println("请输入修改后的书名");
String name = sc.nextLine();
System.out.println("请输入修改后的作者");
String writer = sc.nextLine();
System.out.println("请输入修改后的价格");
String price = sc.nextLine();
Connection conn = JDBCUtils.getConnection();
PreparedStatement pstmt = null;
String sql = "update library set where id = ?, name = ?, writer = ?, price = ?";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, id);
pstmt.setString(2,name);
pstmt.setString(3, writer);
pstmt.setString(4, price);
int num = pstmt.executeUpdate();
if(num > 0) {
System.out.println("修改成功");
selectAll();
}else {
System.out.println("修改失败");
menu();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JDBCUtils.closeAll(null, pstmt, conn);
}
}
private static void add() {
// TODO Auto-generated method stub
System.out.println("请输入书名");
String name = sc.nextLine();
System.out.println("请输入作者");
String writer = sc.nextLine();
System.out.println("请输入价格");
String price = sc.nextLine();
Connection conn = JDBCUtils.getConnection();
PreparedStatement pstmt = null;
String sql = "insert into library(name,writer,price) values(?,?,?)";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1,name);
pstmt.setString(2, writer);
pstmt.setString(3, price);
int num = pstmt.executeUpdate();
if(num > 0) {
System.out.println("添加成功");
selectAll();
}else {
System.out.println("添加失败");
menu();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JDBCUtils.closeAll(null, pstmt, conn);
}
}
private static void slectbyID() {
// TODO Auto-generated method stub
System.out.println("请输入要查询的书编号");
String num = sc.nextLine();
Connection conn = JDBCUtils.getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "select * from library where id = ?";
try {
pstmt = conn.prepareCall(sql);
pstmt.setString(1, num);
rs = pstmt.executeQuery();
//处理结果
System.out.println("序号\t书名\t作者\t售价");
if(rs.next()) {
System.out.println(rs.getInt("id")+"\t"+rs.getString("name")+"\t"+rs.getString("writer")+"\t"+rs.getString("price"));
}else {
System.err.println("查无此书");
}
menu();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void selectAll() {
// TODO Auto-generated method stub
//获取连接
Connection conn = JDBCUtils.getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "select * from library";
try {
pstmt = conn.prepareCall(sql);
rs = pstmt.executeQuery();
//处理结果
System.out.println("序号\t书名\t作者\t售价");
while(rs.next()) {
System.out.println(rs.getInt("id")+"\t"+rs.getString("name")+"\t"+rs.getString("writer")+"\t"+rs.getString("price"));
}
menu();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JDBCUtils.closeAll(rs, pstmt, conn);
}
}
public static void main(String[] args) {
login();
}
}