java实现数据库的增删改查

代码:

public static void main(String[] args) {
        Connection con;//驱动程序名
        //URL指向要访问的数据库名--test,(自行更改成自己的库名)
        String url = "jdbc:mysql://localhost:3306/test";
        //MySQL配置时的用户名(自行更改成自己的用户名)
        String user = "root";
        //MySQL配置时的密码(自行更改成自己的密码)
        String password = "";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            // 连续数据库
            con= DriverManager.getConnection(url,user,password);

            //验证是否连接成功
            if (!con.isClosed())
            {
                System.out.println("Succeeded connecting to the Database!");
            }

            //添加:
            String insert="INSERT INTO book VALUES(5,'HarryPotter',45)";
            Statement st=con.createStatement();
            ResultSet rs;
            int i=st.executeUpdate(insert);
            if (i==0)
            {
                System.out.println("添加失败!");
            }
            else {
                System.out.println("添加成功!");
            }
            String sql="SELECT * FROM book WHERE id = ?";
            PreparedStatement pst= (PreparedStatement) con.prepareStatement(sql);
            pst.setObject(1,5);
            rs=pst.executeQuery();
            System.out.println("查询:");
            System.out.println("序号"+"\t\t"+"书名"+"\t\t"+"价格");
            while (rs.next())
            {

                System.out.println(rs.getInt("id")+"\t\t"+rs.getString("name")+"\t\t"+rs.getInt("price"));
            }
            System.out.println("----------------------");

            //删除:
            String delete="DELETE FROM book WHERE id=4";
            i=st.executeUpdate(delete);
            if (i==0)
            {
                System.out.println("删除失败!");
            }
            else{
                System.out.println("删除成功!");
            }
            System.out.println("查询:");
            System.out.println("序号"+"\t\t"+"书名"+"\t\t"+"价格");
            pst.setObject(1,4);
            rs=pst.executeQuery();
            while (rs.next())
            {

                System.out.println(rs.getInt("id")+"\t\t"+rs.getString("name")+"\t\t"+rs.getInt("price"));
            }
            System.out.println("----------------------");


            //更改:
            System.out.println("查询:");
            System.out.println("序号"+"\t\t"+"书名"+"\t\t"+"价格");
            pst.setObject(1,1);
            rs=pst.executeQuery();
            while (rs.next())
            {

                System.out.println(rs.getInt("id")+"\t\t"+rs.getString("name")+"\t\t"+rs.getInt("price"));
            }
            String set="UPDATE book SET price=1 WHERE id=1";
            i=st.executeUpdate(set);
            if (i==0)
            {
                System.out.println("更改失败!");
            }
            else
            {
                System.out.println("更改成功!");
            }
            System.out.println("查询:");
            System.out.println("序号"+"\t\t"+"书名"+"\t\t"+"价格");
            pst.setObject(1,1);
            rs=pst.executeQuery();
            while (rs.next())
            {

                System.out.println(rs.getInt("id")+"\t\t"+rs.getString("name")+"\t\t"+rs.getInt("price"));
            }

            rs.close();
            pst.close();
            con.close();
        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        }
    }

运行结果:

 

  • 8
    点赞
  • 114
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值