zoukankan      html  css  js  c++  java
  • 懒汉单例安全basedao

    package Dao;

    import java.sql.*;

    public class BaseDao {
    private String drname = "com.mysql.jdbc.Driver";
    private String url = "jdbc:mysql://localhost:3306/jdhb";
    private String name = "root";
    private String pwd = "root";
    private static Connection conn = null;

    private BaseDao() {
    try {
    Class.forName(drname);
    conn = DriverManager.getConnection(url, name, pwd);
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }

    public static Connection getconn() {
    if (conn == null) {
    new BaseDao();
    }
    return conn;
    }

    public static void closeAll(ResultSet rs, Statement stat) {
    try {
    if (rs != null)
    rs.close();
    if (stat != null)
    stat.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }

    /**
    * 公共增删改方法
    *
    * @param sql sql语句
    * @param objects 语句中各个问号所表达的值
    * @return
    */
    public static int excutesql(String sql, Object... objects) {
    int count = 0;
    Connection con = getconn();
    PreparedStatement pre = null;
    try {
    pre = con.prepareStatement(sql);
    for (int i = 0; i < objects.length; i++) {
    pre.setObject(i + 1, objects[i]);
    }
    count = pre.executeUpdate();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    return count;
    }

    }

  • 相关阅读:
    online ddl与pt-osc详解
    几个重点问题回顾
    死锁及常见死锁模型
    InnoDB中锁的算法(3)
    一个幻读模型引出的记录可见性判断
    jupyter notebook的使用
    l线程池抓取lianjia
    lagou数据爬取
    爬虫代理的设置
    linux如何安装和启动mongdb
  • 原文地址:https://www.cnblogs.com/lenlen/p/10109748.html
Copyright © 2011-2022 走看看