Java JDBC操作Mysql数据库增删改查,分页查询实例详解(源码)

配置信息

public static final String USER_NAME = "root";
public static final String PWD = "123456789";
public static final String DRIVER = "com.mysql.jdbc.Driver";
public static final String URL = "jdbc:mysql://localhost:3306/web_demon";
/** 分页查询默认每页记录条数 */
public static final int PAGE_SIZE_DEFAULT = 10;

获取数据库连接对象

**
   * 获取数据库连接
   * @return 数据库连接对象
   */
  public Connection getConnection(Connection conn) {
    if(conn == null){
      try {
        Class.forName(Config.DRIVER);
        conn = DriverManager.getConnection(Config.URL, Config.USER_NAME, Config.PWD);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    return conn;
  }

封装增删改操作

/**
   * 新增,删除,插入操作
   * @param sql 执行的sql语句
   *      例如:新增语句 "insert into user (name,sex) values (?,?)";
   *       删除语句  "delete from user where id=?";
   *       修改语句  "update user set name=?,sex=? where id=? and sex=?";
   * @param values 对应的参数值
   * @return 影响条数,-1为异常
   */
  private int execute(String sql,Object... values){
    Connection conn = null;
    PreparedStatement pStmt = null;
    try {
      conn = this.getConnection(conn);
      pStmt = conn.prepareStatement(sql);
      //设置参数
      if(pStmt != null && values != null && values.length > 0){
        for (int i = 0; i < values.length; i++) {
          pStmt.setObject(i+1, values[i]);
        }
      }
      int i =pStmt.executeUpdate();
      return i;
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (conn != null) {
        this.closeConnection(conn);
      }
    }
    return -1;
  }

新增,删除,修改方法调用示例

//新增
public static void insert(){
    String sql = "insert into user (name,sex) values (?,?)";
    Object[] values = new Object[]{"李四",0};
    int res = baseImp.execute(sql, values);
    System.out.println("insert res="+res);
  }
//删除
public static void delete(){
    String sql = "delete from user where id=?";
    Object[] values = new Object[]{2};
    int res = baseImp.execute(sql, values);
    System.out.println("delete res="+res);
  }
//更新
public static void update(){
    String sql = "update user set name=?,sex=? where id=? and sex=?";
    Object[] values = new Object[]{"张三",1,1,0};
    int res = baseImp.execute(sql, values);
    System.out.println("update res="+res);
  }

125jz网原创文章。发布者:江山如画,转载请注明出处:http://www.125jz.com/4929.html

(0)
江山如画的头像江山如画管理团队
上一篇 2020年2月19日 下午7:24
下一篇 2020年2月20日 下午5:59

99%的人还看了以下文章

  • Double.valueOf(r).doubleValue();是什么意思

    在一段代码中看到Double.valueOf(“2020”).doubleValue(),先出现了Double.valueOf(),又用了doubleValue(),有点迷惑,为什么这么用呢? <% String s=request.getParameter(“radius”); double r; if(s!=null) {r…

    2019年9月5日
    12.3K0
  • 简!修改Jupyter 默认打开目录的方法

    1、启动cmd,执行以下命令,查看 jupyter 配置文件路径 C:Users41588>jupyter notebook –generate-configWriting default config to: C:Users41588.jupyterjupyter_notebook_config.py 2、找到配置文件 jupyter_n…

    2023年7月19日 编程开发
    8.7K0
  • Pandas读取excel:Excel file format cannot be determined解决方法

    Pandas读取excel时报错,excel表格不能被指定,是什么原因? 这个问题我搞了很久,最后终于搞明白了,网上各种什么utf-8呀,格式化或者另存都不行,我都试过了。 Excel file format cannot be determined解决方法 首先要确定excel已经放在项目目录下,路径是正确的。 一、然后确保安装了所需要的模块 pip in…

    2022年9月11日
    17.0K0
  • python 期末复习-综合练习题

    十八、分别使用for循环和while循环求1+2+3+4+……+856的和 要求: 1、新建一个“sum.py”文件。 2、编写程序。 3、调试程序。 4、排除错误。 十九、输入一个数,判断奇数偶数 要求: 1、新建一个“number.py”文件 2、编写程序。 3、调试程序。 4、排除错误。 二十、分别使用for循环和while循环输出1到100之间的偶数…

    2023年6月15日
    4.9K0
  • itbegin编程教学云课堂使用介绍

    进入https://www.itbegin.com/apps    点击右上角 登录 登录后,点击院校通–院校 点击进入个人中心 点击“web开发技术课堂”,进入相应课程 点击:我的预习,可以看到预习的知识点,时间要求。请同学们一定要在结束时间前完成任务! 点击查看,可以看到需要预习的知识点,预习以观察,查看效果为主。 如要求:观察各标签的使用及…

    2018年3月14日
    7.3K0
  • 两个简单的Pycharm激活方法分享

    一、Pycharm激活码激活 优点:Window、Mac、Ubantu都稳定有效,关键是这种激活方式不会产生其他影响 缺点:需要修改hosts文件 修改hosts文件 将0.0.0.0 account.jetbrains.com添加到hosts文件最后,注意hosts文件无后缀,如果遇到无法修改或权限问题,可以采用覆盖的方法去替换hosts文件 修改后请检查…

    2020年3月14日
    6.1K0

发表回复

登录后才能评论