JDBC的CURD操作

JDBC的CRUD操作

插入操作:

public class JDBC_Demo_bao_cun {

   @Test

   /**

    * 保存操作

    */

   public void demo(){

       Connection conn = null;

       Statement statement = null;

扫描二维码关注公众号,回复: 2804428 查看本文章

       try {

           Class.forName("com.mysql.jdbc.Driver");

           conn = DriverManager.getConnection("jdbc:mysql:///jdbctest","root", "wxhxx520");

           statement = conn.createStatement();

           String sql = "insert goods values(null, 'eee', 123,'hahaha')";

           int i = statement.executeUpdate(sql);

           if (i > 0) {

                System.out.println("保存成功");

           } else {

                System.out.println("保存失败");

           }

       } catch (Exception e){

           e.printStackTrace();

       } finally {

           if (statement != null) {

                try {

                    statement.close();

                } catch (SQLException e) {

                    e.printStackTrace();

                }

                statement = null;

           }

           if (conn != null) {

                try {

                    conn.close();

                } catch (SQLException e) {

                    e.printStackTrace();

               }

                conn = null;

           }

       }

    }

}

更新操作:

public void demo2(){

       Connection conn = null;

       Statement statement = null;

       try {

           Class.forName("com.mysql.jdbc.Driver");

           conn = DriverManager.getConnection("jdbc:mysql:///jdbctest","root", "wxhxx520");

           statement = conn.createStatement();

           String sql = "update goods set name='悠悠球', price=299,desp='好玩!!!' where id = 5";

           int i = statement.executeUpdate(sql);

           if (i > 0) {

                System.out.println("修改成功");

           } else {

                System.out.println("修改失败");

           }

       } catch (Exception e) {

           e.printStackTrace();

       } finally {

           if (statement != null) {

                try {

                    statement.close();

                } catch (SQLException e) {

                    e.printStackTrace();

                }

                statement = null;

           }

           if (conn != null) {

                try {

                    conn.close();

                } catch (SQLException e) {

                    e.printStackTrace();

                }

                conn = null;

           }

       }

}

删除操作:

public void demo3(){

       Connection conn = null;

       Statement statement = null;

       try {

           Class.forName("com.mysql.jdbc.Driver");

           conn = DriverManager.getConnection("jdbc:mysql:///jdbctest","root", "wxhxx520");

           statement =conn.createStatement();

           String sql = "delete from goods where id=5";

           int i = statement.executeUpdate(sql);

           if (i > 0) {

                System.out.println("删除成功");

           } else {

                System.out.println("删除失败");

           }

       } catch (Exception e) {

           e.printStackTrace();

       } finally {

           if (statement != null) {

                try {

                    statement.close();

                } catch (SQLException e) {

                    e.printStackTrace();

                }

                statement = null;

           }

           if (conn != null) {

                try {

                    conn.close();

                } catch (SQLException e) {

                    e.printStackTrace();

                }

                conn = null;

           }

       }

}

查询操作:

public void demo4(){

       Connection conn = null;

       Statement statement = null;

       ResultSet resultSet = null;

       try {

           Class.forName("com.mysql.jdbc.Driver");

           conn = DriverManager.getConnection("jdbc:mysql:///jdbctest","root", "wxhxx520");

           statement = conn.createStatement();

           String sql = "select * from goods";

           resultSet = statement.executeQuery(sql);

           while (resultSet.next()) {

               int id =resultSet.getInt("id");

                String name =resultSet.getString("name");

                float price =resultSet.getFloat("price");

                String desp =resultSet.getString("desp");

                System.out.println(id +"   " + name + "   " + price + "   " + desp);

           }

       } catch (Exception e) {

           e.printStackTrace();

       } finally {

           if (resultSet != null) {

                try {

                    resultSet.close();

                } catch (SQLException e) {

                    e.printStackTrace();

                }

                resultSet = null;

           }

           if (statement != null) {

                try {

                    statement.close();

               } catch (SQLException e){

                    e.printStackTrace();

                }

                statement = null;

           }

           if (conn != null) {

                try {

                    conn.close();

                } catch (SQLException e) {

                    e.printStackTrace();

                }

                conn = null;

           }

       }

}


猜你喜欢

转载自blog.csdn.net/qq_37585236/article/details/80615877