会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132883个问题
JAVA 全系列/第一阶段:JAVA 快速入门/数组和数据存储 34291楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 34292楼

老师批量删除要怎么做呢?我用了同样的方法貌似行不通 哪里出了问题?

//批量删除
	public void deleteBatch(List<Departments> list) {
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			conn = JdbcUtil.getConnection();
			ps = conn.prepareStatement("delete from departments where department_name=?");
			for(int i=0;i<list.size();i++) {
				ps.setString(1, list.get(i).getDepartmentName());
				//批处理
				ps.addBatch();
			}
			int[] arr =ps.executeBatch();
			for (int i = 0; i < arr.length; i++) {
				System.out.println(i);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			JdbcUtil.closeResoure(ps, conn, null);
			
		}
	}
	
	public static void main(String[] args) {
		PreparedStatementDemon psd = new PreparedStatementDemon();
		//psd.insertDepartments("人力资源2", 12);
		//psd.updateDepartments("科研部", 7, 10);
		/*Departments dept = psd.selectDepartmentsId(9);
		if (dept !=null) {
			System.out.println(dept.getDepartmentId()+" "+dept.getDepartmentName()+" "+ dept.getLocationId());
		}
		*/
		/*List<Departments> list = psd.selectDepartmentsByLike("部");
		for (Departments dept : list) {
			System.out.println(dept.getDepartmentId()+" "+dept.getDepartmentName()+" "+dept.getLocationId());
		}*/
		/*List<Departments> list = new ArrayList<>();
		for(int i=1;i<=10;i++) {
			Departments dept = new Departments();
			dept.setDepartmentName("Dota部"+i);
			dept.setLocationId(10+i);
			list.add(dept);
		}
		psd.addBatch(list);*/
		List<Departments> list = new ArrayList<>();
		for(int i=1;i<=10;i++) {
			Departments dept = new Departments();
			dept.setDepartmentName("Dota部"+i);
			list.add(dept);
		}
		psd.addBatch(list);
		
	}

结果:

Thu Feb 20 18:18:13 CST 2020 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.


JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 34294楼
Python 全系列/第一阶段:Python入门/序列 34295楼
Python 全系列/第一阶段:Python入门/序列 34296楼

from pyspark import SparkConf, SparkContext


def myprint(one):
    print(one)


def top2list(one):
    website = one[0]
    locals = one[1]
    localdic = {}
    for local in locals:
        if local in localdic:
            localdic[local] += 1
        else:
            localdic[local] = 1
    site_locallist = sorted(localdic.items(), key=lambda tp: tp[1], reverse=True)
    returnlist = []
    if (len(site_locallist) > 2):
        for i in range(2):
            returnlist.append(site_locallist[i])
    else:
        returnlist = site_locallist
    return website, returnlist


def get_site_uid_count(one):
    uid = one[0]
    sites = one[1]
    siteDic = {}
    for site in sites:
        if site in siteDic:
            siteDic[site] += 1
        else:
            siteDic[site] = 1
    returnlist = []
    for site, count in siteDic.items():
        returnlist.append((site, (uid, count)))
    return returnlist


def top3list(one):
    website = one[0]
    uid_count_itr = one[1]
    top3lists = ['', '', '']
    for uid_count in uid_count_itr:
        count = uid_count[1]
        for i in range(len(top3lists)):
            if top3lists[i] == "":
                top3lists[i] = uid_count
                break
            elif top3lists[i][1] < count://运行时总是提示这行,str与int之间不能比较,打印type(top3lists[i][1])为int)
                for j in range(2, i, -1):
                    top3lists[j] = top3lists[j - 1]
                top3lists = uid_count
                break

    return website, top3lists


def getApv():
    conf = SparkConf()
    conf.setMaster("local")
    conf.setAppName("pv")
    sc = SparkContext(conf=conf)
    linesRDD = sc.textFile("D:\mypython\date\website")
    # linesRDD.map(lambda line:(line.split("\t")[2],1)).reduceByKey(lambda v1,v2:v1+v2).\
    #     sortBy(lambda tp:tp[1],ascending=False).foreach(lambda one:myprint(one=one))
    # 每个网站访问最多的top2地区
    # site_local = linesRDD.map(lambda line:(line.split("\t")[4],line.split("\t")[2])).groupByKey()
    # site_local.map(lambda one:top2list(one)).foreach(print)
    # 每个网站最活跃的用户top3
    linesRDD.map(lambda line: (line.split("\t")[3], line.split("\t")[4])).groupByKey() \
        .flatMap(lambda one: get_site_uid_count(one)).groupByKey().map(lambda one: top3list(one)).foreach(print)


if __name__ == "__main__":
    getApv()


大数据全系列/(隐藏)第十三阶段:机器学习及推荐系统实战/PySpark及线性回归算法 34297楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 34298楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 34304楼
Python 全系列/下架-第十二阶段:Python_大型电商项目(5天后下架)/Django项目阶段-电商项目(旧) 34305楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637