zoukankan      html  css  js  c++  java
  • Java删除数据库中的数据

    1:删除数据库中数据表中的数据同样也是一个非常用的技术,使用executeUpdate()方法执行用来做删除SQL的语句可以删除数据库表中的数据

    2:本案例使用Statement接口中的executeUpdate()方法,删除数据库中users表中id为1的用户信息

     1 package com.ningmeng;
     2 
     3 import java.sql.*;
     4 /**
     5  * 
     6  * @author biexiansheng
     7  *
     8  */
     9 public class Test06 {
    10 
    11     public static void main(String[] args) {
    12         // TODO Auto-generated method stub
    13         try {
    14             Class.forName("com.mysql.jdbc.Driver");//加载数据库驱动
    15             System.out.println("加载数据库驱动成功");
    16             String url="jdbc:mysql://localhost:3306/test";//声明自己的数据库test的url
    17             String user="root";//声明自己的数据库账号
    18             String password="123456";//声明自己的数据库密码
    19             //建立数据库连接,获得连接对象conn
    20             Connection conn=DriverManager.getConnection(url,user,password);
    21             System.out.println("连接数据库成功");
    22             String sql="delete from users where id=1";//生成一条sql语句
    23             Statement stmt=conn.createStatement();//创建Statement对象
    24             stmt.executeUpdate(sql);//执行sql语句
    25             System.out.println("数据库删除成功");
    26             conn.close();
    27             System.out.println("数据库关闭成功");//关闭数据库的连接
    28         } catch (ClassNotFoundException e) {
    29             // TODO Auto-generated catch block
    30             e.printStackTrace();
    31         } catch (SQLException e) {
    32             // TODO Auto-generated catch block
    33             e.printStackTrace();
    34         }
    35         
    36         
    37     }
    38 
    39 }


     3:批量删除操作

     1 package com.ningmeng;
     2 
     3 import java.sql.*;
     4 /**
     5  * 
     6  * @author biexiansheng
     7  *
     8  */
     9 public class Test06 {
    10 
    11     public static void main(String[] args) {
    12         // TODO Auto-generated method stub
    13         try {
    14             Class.forName("com.mysql.jdbc.Driver");//加载数据库驱动
    15             System.out.println("加载数据库驱动成功");
    16             String url="jdbc:mysql://localhost:3306/test";//声明自己的数据库test的url
    17             String user="root";//声明自己的数据库账号
    18             String password="123456";//声明自己的数据库密码
    19             //建立数据库连接,获得连接对象conn
    20             Connection conn=DriverManager.getConnection(url,user,password);
    21             System.out.println("连接数据库成功");
    22             String sql="delete from users where sex=2";//生成一条sql语句
    23             Statement stmt=conn.createStatement();//创建Statement对象
    24             stmt.executeUpdate(sql);//执行sql语句
    25             System.out.println("数据库删除成功");
    26             conn.close();
    27             System.out.println("数据库关闭成功");//关闭数据库的连接
    28         } catch (ClassNotFoundException e) {
    29             // TODO Auto-generated catch block
    30             e.printStackTrace();
    31         } catch (SQLException e) {
    32             // TODO Auto-generated catch block
    33             e.printStackTrace();
    34         }
    35         
    36         
    37     }
    38 
    39 }

    至此,java中使用jdbc操作数据库的增删改查全部操作完毕,参考者可以在上下篇随笔中参考,熟悉练习和使用jdbc操作数据库,理清操作思路,为以后学习更深打好基础

  • 相关阅读:
    easyui loadFilter 使用
    控件setText与setValue赋值顺序先后区别
    JS选中和取消选中checkbox
    easyui 解决连弹两个dialog时候,第二个dialog居中问题
    bootstrap基础学习【导航条、分页导航】(五)
    bootstrap基础学习【菜单、按钮、导航】(四)
    sublime设置
    《啊哈!算法》笔记
    《编码的奥秘》笔记
    Effective Objective-C 2.0 — 第14条:理解“类对象“的用意
  • 原文地址:https://www.cnblogs.com/biehongli/p/5989100.html
Copyright © 2011-2022 走看看