• 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操作数据库,理清操作思路,为以后学习更深打好基础

  • 相关阅读:
    HDU 2080 夹角有多大II
    HDU 1412 {A} + {B}
    HDU 2034 人见人爱A-B
    二分查找模版
    Matlab debug
    随机梯度下降(Stochastic gradient descent)和 批量梯度下降(Batch gradient descent )的公式对比、实现对比
    拟牛顿法/Quasi-Newton,DFP算法/Davidon-Fletcher-Powell,及BFGS算法/Broyden-Fletcher-Goldfarb-Shanno
    ActionBar点击弹出下拉框操作
    CentOS 64位上编译 Hadoop 2.6.0
    ViewController的生命周期
  • 原文地址:https://www.cnblogs.com/biehongli/p/5989100.html
Copyright © 2020-2023  润新知