数据库查询Util

public class DBUtils {

    private static Dao dao;
    public static String dbUrl;
    public static String dbUserName;
    public static String dbPassword;

    public static void setPassword(String password){
        dbPassword=password;
    }
    public static void  setDbUrl(String dburl){
        dbUrl = dburl;
    }
    public static void setDbUserName(String dbusername){
        dbUserName = dbusername;
    }
    public static Dao getSqlDao() {
        //if(dao==null) {  如果同时查询多个数据库,这里不能为null
            SimpleDataSource dataSource = new SimpleDataSource();
            dataSource.setJdbcUrl(dbUrl);
            dataSource.setUsername(dbUserName);
            dataSource.setPassword(dbPassword);
            dao = new NutDao(dataSource);
       // }

        return dao;
    }
    /*
    *返回查询结果
    * */
    public static List queryRecord(String sql){
        Sql sq = Sqls.queryRecord(sql);
        Dao dao = getSqlDao();
        return  dao.execute(sq).getList(Map.class);
    }

    public static  Map query(String sql){
        Sql sq = Sqls.fetchRecord(sql);
        Dao dao = getSqlDao();
        return   dao.execute(sq).getObject(Map.class);
    }

    /**
     * return rows number
     * @param sql  select count(0) from t_complete ....
     * @return int
     */
    public static int excuteSqlCount(String sql){
        Sql sq = Sqls.create(sql);
        sq.setCallback(Sqls.callback.records());
        getSqlDao().execute(sq);
        String r = sq.getString();
        return  Integer.parseInt(r);
    }

    public static int insertOrUpdate(String sql){
        Sql sq = Sqls.create(sql);
        sq.setCallback(Sqls.callback.records());
        getSqlDao().execute(sq);
        return  sq.getUpdateCount();
    }
    

}

你可能感兴趣的:(test,skills)