ajanuskevicius
8/16/2016 - 1:22 PM

Java - JDBC - callBatchProcessing

Java - JDBC - callBatchProcessing

public void callBatchProcessing () {
			
		String sql = "call addNewCity(?, ?, ?, ?)";
		
		String option = "";
			
		try (
			
				Connection conn = DB_Util.getConn(DB_Types.MYSQLDB);
			
				CallableStatement callStat = conn.prepareCall(sql);
				)
		{
			do {
			
					System.out.print("Enter City Name: ");
					callStat.setString(1, scanner.nextLine());
			
					System.out.print("Enter City Country Code: ");
					callStat.setString(2, scanner.nextLine());
			
					System.out.print("Enter City District: ");
					callStat.setString(3, scanner.nextLine());
			
					System.out.print("Enter City Population: ");
					callStat.setInt(4, scanner.nextInt());
		
					callStat.addBatch();
		
					System.out.print("Would You like to add another new City? yes/no: ");
					
					option = scanner.nextLine();
					
					System.out.println("Opt: " + option);
			} 
			while (option.equals("yes")); 
			
			if (option.equals("no")) {
			
				int []updateCounts = callStat.executeBatch();
				
				System.out.println("Total new Cities added: " + updateCounts.length);
			}
		}
		catch (SQLException e) {
				
			System.out.println("Error: " + e);
		}
	}