• jdbc的实例应用:增删查改实现


    //在jdbc中进行增删查改

    //查看所有

    public static void findAll() {
    String url = "jdbc:mysql://localhost:3306/epet";//加载驱动器
    String user = "root";
    String password = "root";
    String sql = "SELECT * FROM dog";
    Connection connection = null;
    Statement statement = null;
    ResultSet resultSet = null;
    try {
    Class.forName("com.mysql.jdbc.Driver");
    connection = DriverManager.getConnection(url, user, password);//加载JDBC驱动器
    statement = connection.createStatement();//与数据库建立连接
    resultSet = statement.executeQuery(sql);//发送SQL语句 ,并且返回结果
    while(resultSet.next()){//处理返回结果
    System.out.println(resultSet.getInt(1));
    System.out.println(resultSet.getString(2));
    System.out.println(resultSet.getInt("health"));
    System.out.println(resultSet.getInt("love"));
    System.out.println(resultSet.getObject(5));
    System.out.println("=================");
    }
    } catch (Exception e) {
    // TODO: handle exception
    }finally{
    try { //关闭资源
    if (null != resultSet) {
    resultSet.close();
    }
    if (null != statement) {
    statement.close();
    }
    if (null != connection) {
    connection.close();
    }
    } catch (Exception e2) {
    // TODO: handle exception
    }

    }
    }

    //插入语句
    public static void insert() {
    String url = "jdbc:mysql://localhost:3306/epet";
    String user = "root";
    String password = "root";
    String sql = "INSERT INTO dog(name,health,love,strain) VALUES ('aaa',90,100, 'bbb')";
    Connection connection = null;
    Statement statement = null;
    try {
    Class.forName("com.mysql.jdbc.Driver");

    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    try {
    connection = DriverManager.getConnection(url, user, password);

    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    try {
    statement = connection.createStatement();
    statement.executeUpdate(sql);
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    try {
    statement.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    try {
    connection.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    //更新数据库
    public static void update() {
    String url = "jdbc:mysql://localhost:3306/epet";
    String user = "root";
    String password = "root";
    String sql = "UPDATE dog SET name='haha' WHERE id=1";
    Connection connection = null;
    Statement statement = null;
    try {
    Class.forName("com.mysql.jdbc.Driver");

    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    try {
    connection = DriverManager.getConnection(url, user, password);
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    try {
    statement = connection.createStatement();
    statement.executeUpdate(sql);
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    try {
    statement.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    try {
    connection.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }


    //删除数据库元素
    public static void delete() {
    Connection connection = null;
    Statement statement = null;

    String url = "jdbc:mysql://localhost:3306/epet";
    String user = "root";
    String password = "root";
    String sql = "DELETE FROM dog WHERE id =1";
    String sql2 = "DELETE FROM dog WHERE id =2";
    try {
    Class.forName("com.mysql.jdbc.Driver");

    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    try {
    connection = DriverManager.getConnection(url, user, password);
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    try {
    statement = connection.createStatement();
    statement.executeUpdate(sql);
    statement.executeUpdate(sql2);
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    try {
    statement.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    try {
    connection.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    }

  • 相关阅读:
    干货 | 日均流量200亿,携程高性能全异步网关实践 https://mp.weixin.qq.com/s/JdbPf_H4pn5PnPH2LIKQlw
    go获取机器的mac地址和ip
    扫清障碍和风险
    如何优雅地记录操作日志
    WebSocket 基础与应用系列(一
    pytest测试框架
    两个循环依赖的类中都有@Async 报错问题
    jvm 分析常用笔记
    Qt5.2~5.8下载地址
    windows的bat脚本中文乱码或者文件中文乱码
  • 原文地址:https://www.cnblogs.com/shen-xiao-jie/p/6179883.html
Copyright © 2020-2023  润新知