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;
    }

    }

  • 相关阅读:
    Linux 普通用户su命令切换控制
    Linux上的文件管理类命令(2)
    系统内存管理
    ssh 安全配置
    redhat系统安装部署
    Unity截屏
    Unity场景道具模型拓展自定义编辑器
    Unity优化之减少Drawcall
    Unity安卓连接profile调试
    Unity游戏数据用Json保存
  • 原文地址:https://www.cnblogs.com/lenlen/p/10109748.html
Copyright © 2011-2022 走看看