Soluciones ejercicios

package com.trifulcas.country;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Scanner;

public class AddCategory {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("Introduzca el nombre de la categoría");
		String categoria = scanner.nextLine();
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
			Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sakila", "root", "");
			Statement st = con.createStatement();
			String sql = "insert into category (name) values ('" + categoria + "')";
			System.out.println(sql);
			int res = st.executeUpdate(sql);
			System.out.println(res + " registros afectados");
			st.close();
			con.close();
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
	}

}

package com.trifulcas.country;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class UpdatePayments {

	public static void main(String[] args) {
			try {
			Class.forName("com.mysql.cj.jdbc.Driver");
			Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sakila", "root", "");
			Statement st = con.createStatement();
			
			String sql = "update payment set amount=amount+0.5";
			System.out.println(sql);
			int res = st.executeUpdate(sql);
			System.out.println(res + " registros afectados");
			st.close();
			con.close();
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
	}

}

package com.trifulcas.country;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Scanner;

public class AddActors {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("Introduzca el número de actores");
		int cantidad = scanner.nextInt();
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
			Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sakila", "root", "");
			Statement st = con.createStatement();
			for (int i = 1; i <= cantidad; i++) {
				String sql = "insert into actor (first_name,last_name) values ('actor"+i+"','actor"+i+"');";
				System.out.println(sql);
				int res = st.executeUpdate(sql);
				System.out.println(res + " registros afectados");
			}
			st.close();
			con.close();
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
	}

}

Publicado por

Juan Pablo Fuentes

Formador de programación y bases de datos