代码:

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);
        }
    }

运行结果:

 

Logo

本社区面向用户介绍CSDN开发云部门内部产品使用和产品迭代功能,产品功能迭代和产品建议更透明和便捷

更多推荐