编写一个java程序,从1加到100,结果是5050。 每做一次加法,就休眠1秒 当程序运行过程中,杀死这个程序。 再次运行,还能从上次的计算结果继续下去。

给大家推荐个靠谱的公众号程序员探索之路,大家一起加油https://i-blog.csdnimg.cn/blog_migrate/93320939ba8f8b0a898e29429753f496.png ​ 

package com.qf.second.day08_jdbc;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
/*
 * 编写一个java程序,从1加到100,结果是5050。
每做一次加法,就休眠1秒
当程序运行过程中,杀死这个程序。
再次运行,还能从上次的计算结果继续下去。
 * */
public class ZzhHomeWork {
	static int i;
	static int sum;
	/**
	 * 采用匿名内部类的方式创建线程
	 */
	static Runnable r = new Runnable() {		
		@Override
		
		public void run() {
			sum();
		}
		
	};
	
	/**
	 * 创建线程并开启
	 * @param args
	 */
	public static void main(String[] args) {
		Thread t1 = new Thread(r);
		t1.start();
		
	}
	/*
	 * 每次执行是 先 查询表中的数据  
	 * 如果没有值 i=1 sum=0
	 * 如果有值
	 * 		把表中 number 的最后一个+1 赋值给i
	 * 		把表中 sum 的最后一个 给sum
	 * 如果 i==5 或 i==10 休眠1秒
	 */
	public static void sum(){
		String sql ="select * from day08_HomeWork";
		List<Map<Object,Object>> list = DatabaseTools.selectDataBases(sql);
		if(list.size() == 0){
			i = 1;
			sum = 0;
		}else{
			for(Map.Entry<Object, Object> m : list.get(list.size() - 1).entrySet()){
				Integer ii = (Integer)m.getKey();
				Integer ss = (Integer)m.getValue();
				
				i = ii.intValue()+1;
				sum = ss.intValue();
			}

		}
		
		for(;i <= 100;i++){
			sum += i;
			sql="insert into day08_HomeWork values("+i+","+sum+")";
			DatabaseTools.updateDataBases(sql);
			if(i == 5 || i == 10){
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(i==100){
				System.out.println("存入完成");
			}
		}
	}
}
//数据库操作工具类
DataBasesTools
package com.qf.second.day08_jdbc;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.ResourceBundle;
import java.util.Set;


import com.sun.faces.mgbean.ManagedBeanInfo.MapEntry;


//连接数据库  增删查改 数据库工具类
public class DatabaseTools {
	
	//类加载时就需要的变量
	private static String driverClass = null;
	private static String url = null;
	private static String user = null;
	private static String password = null;
	static{
		ResourceBundle bundle = ResourceBundle.getBundle("zzhdbconfig");
		driverClass = bundle.getString("driverClass");
		url = bundle.getString("url");
		user = bundle.getString("user");
		password = bundle.getString("password");
		
		//加载驱动
		try {
			Class.forName(driverClass);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//连接数据库
	public static Connection getConnect() throws SQLException{
		return DriverManager.getConnection(url,user,password);
	}
	//查询功能
	public static List<Map<Object,Object>> selectDataBases(String sql){
		//用来保存表结构
		List<Map<Object,Object>> list = new ArrayList<Map<Object,Object>>();
		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;
		try {
			conn = getConnect();
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			Map<Object, Object> map1 = new HashMap<Object, Object>();
			int line = rs.getMetaData().getColumnCount();
//			for(int i = 1;i <= line;i++){
//				map1.put(rs.getMetaData().getColumnName(i), "");
//			}
//			list.add(map1);
			while(rs.next()){
				Map<Object, Object> map = new HashMap<Object, Object>();
				for(int i = 1;i <= line;i++){
					map.put(rs.getMetaData().getColumnName(i), rs.getObject(i));
				}				
				list.add(map);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				closeAll(conn, st, rs);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		return list;
	}
	//更新功能  包括  增加   删除   修改
	public static int updateDataBases(String sql){
		Connection conn = null;
		Statement st = null;
		int rs = 0;
		try {
			conn = getConnect();
			st = conn.createStatement();
			rs = st.executeUpdate(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				closeAll(conn,st);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return rs;
		
	}
	//创建表
//	public static void creatTable(String sql){
//		Connection conn = null;
//		Statement st = null;
//		int rs = 0;
//		
//		try {
//			conn = getConnect();
//			st = conn.createStatement();
//
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}
	//关闭资源
	public static void closeAll(Connection conn,Statement st) throws SQLException{
		if(conn != null){
			conn.close();
		}
		
		if(st != null){
			st.close();
		}
		
	}
	public static void closeAll(Connection conn,Statement st,ResultSet rs) throws SQLException{
		closeAll(conn, st);
		if(rs != null){
			rs.close();
		}
		
	}
	//打印结果
	public static void print(List<Map<Object,Object>> list){
		for(int i = 0;i < list.size();i++){
			for(Map.Entry<Object, Object> m : list.get(i).entrySet()){
//				if(i == 0){
//					System.out.print(m.getKey()+"\t\t");
//				}else{
					System.out.print(m.getKey()+"="+m.getValue()+"\t\t");
//				}
			}
			System.out.println();
		}
	}
}
	
	//类加载时就需要的变量
	private static String driverClass = null;
	private static String url = null;
	private static String user = null;
	private static String password = null;
	static{
		ResourceBundle bundle = ResourceBundle.getBundle("zzhdbconfig");
		driverClass = bundle.getString("driverClass");
		url = bundle.getString("url");
		user = bundle.getString("user");
		password = bundle.getString("password");
		
		//加载驱动
		try {
			Class.forName(driverClass);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//连接数据库
	public static Connection getConnect() throws SQLException{
		return DriverManager.getConnection(url,user,password);
	}
	//查询功能
	public static List<Map<Object,Object>> selectDataBases(String sql){
		//用来保存表结构
		List<Map<Object,Object>> list = new ArrayList<Map<Object,Object>>();
		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;
		try {
			conn = getConnect();
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			Map<Object, Object> map1 = new HashMap<Object, Object>();
			int line = rs.getMetaData().getColumnCount();
//			for(int i = 1;i <= line;i++){
//				map1.put(rs.getMetaData().getColumnName(i), "");
//			}
//			list.add(map1);
			while(rs.next()){
				Map<Object, Object> map = new HashMap<Object, Object>();
				for(int i = 1;i <= line;i++){
					map.put(rs.getMetaData().getColumnName(i), rs.getObject(i));
				}				
				list.add(map);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				closeAll(conn, st, rs);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		return list;
	}
	//更新功能  包括  增加   删除   修改
	public static int updateDataBases(String sql){
		Connection conn = null;
		Statement st = null;
		int rs = 0;
		try {
			conn = getConnect();
			st = conn.createStatement();
			rs = st.executeUpdate(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				closeAll(conn,st);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return rs;
		
	}
	//创建表
//	public static void creatTable(String sql){
//		Connection conn = null;
//		Statement st = null;
//		int rs = 0;
//		
//		try {
//			conn = getConnect();
//			st = conn.createStatement();
//
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}
	//关闭资源
	public static void closeAll(Connection conn,Statement st) throws SQLException{
		if(conn != null){
			conn.close();
		}
		
		if(st != null){
			st.close();
		}
		
	}
	public static void closeAll(Connection conn,Statement st,ResultSet rs) throws SQLException{
		closeAll(conn, st);
		if(rs != null){
			rs.close();
		}
		
	}
	//打印结果
	public static void print(List<Map<Object,Object>> list){
		for(int i = 0;i < list.size();i++){
			for(Map.Entry<Object, Object> m : list.get(i).entrySet()){
//				if(i == 0){
//					System.out.print(m.getKey()+"\t\t");
//				}else{
					System.out.print(m.getKey()+"="+m.getValue()+"\t\t");
//				}
			}
			System.out.println();
		}
	}
}
//配置文件
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/myfirstdb
user=root
password=123456

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值