JDBC数据库简单实现图书系统管理(增删改查)

package Gadgets;
import java.sql.*;
import java.util.ArrayList;

//数据库管理图书馆增删改查
public class Library {
        public static String url="jdbc:mysql://localhost:3306/jdbc";
        public static String username="root";
        public static String password="root";
        static Connection conn=null;
        static Statement stmt=null;
        static PreparedStatement preStmt=null;
        static ResultSet rs=null;

        //主函数
       public static void main(String[] args) throws SQLException{
            try{
                conn=DriverManager.getConnection(url,username,password);
//                //虽然可以直接用create table创建,但是我喜欢这样
//                ArrayList arrayList=new ArrayList<>();
//                arrayList.add("《放学后》");
//                arrayList.add("《秘密》");
//                arrayList.add("《名侦探的守则》");
//                arrayList.add("《单恋》");
//                arrayList.add("《白夜行》");
//                arrayList.add("《幻夜》");
//                arrayList.add("《流星之拌》");
//                arrayList.add("《嫌疑人X的献身》");
//                arrayList.add("《解忧杂货店》");
//                arrayList.add("《新参者》");
//                for(int i=0;i
//                    Insert(i+1, arrayList.get(i));
//                }
                //Taverse();
                Insert(11, "《美丽新世界》");
                Delete(6);
                Upgrade(5,"《黑夜行》");
                //Taverse();
            }catch (Exception e){
                    e.printStackTrace();
            }finally {
                if(conn!=null) conn.close();
                if(stmt!=null) stmt.close();
                if(preStmt!=null) preStmt.close();
                if(rs!=null) rs.close();
            }
        }


        //这里因为要用变量,所以要预编译SQL语句,改用PreparedStatement接口(踩了半天坑才知道这玩意)

        //增
        public static void Insert(int num,String bname) throws SQLException {
            String sql="INSERT INTO Library VALUES(?,?)";
            preStmt = conn.prepareStatement(sql);       //用占位符预编译
            preStmt.setInt(1,num);
            preStmt.setString(2,bname);
            preStmt.executeUpdate();                    //设置好了参数就执行
            System.out.println("插入成功!");
        }
        //删
        public static void Delete(int num) throws SQLException {
            String sql="DELETE FROM Library WHERE id=?";
            preStmt= conn.prepareStatement(sql);
            preStmt.setInt(1,num);
            preStmt.executeUpdate();
            System.out.println("删除成功!");
        }
        //改
       public static void Upgrade(int num, String bname) throws SQLException{
           String sql="UPDATE Library SET name=? WHERE id=?";
           preStmt= conn.prepareStatement(sql);
           preStmt.setString(1,bname);
           preStmt.setInt(2,num);
           preStmt.executeUpdate();
           System.out.println("修改成功!");;
       }
        //查
        public static void Taverse() throws SQLException {
            stmt=conn.createStatement();
            rs=stmt.executeQuery("select * from library");
            int id;String name;
            while(rs.next()){
                id=rs.getInt("id");
                name=rs.getString("name");
                System.out.println(id+" "+name);
            }
            System.out.println("查找完成!");
        }

}

你可能感兴趣的:(数据库,mysql,sql,java,jdbc)