Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.sql.*;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- public class ListaaminenJaLisaaminen {
- public static void main(String[] args) throws Exception {
- Scanner lukija = new Scanner(System.in);
- System.out.println("Tervetuloa!");
- while (true) {
- System.out.println("");
- System.out.println("Komennot:");
- System.out.println("1. Listaa kappaleet");
- System.out.println("2. Lisää kappale");
- System.out.println("3. Sulje");
- String komento = lukija.nextLine();
- if (komento.equals("3")) {
- break;
- }
- if (komento.equals("1")) {
- listaa();
- }
- if (komento.equals("2")) {
- lisaa();
- }
- }
- }
- public static void listaa() throws SQLException {
- Connection connection = DriverManager.getConnection("jdbc:sqlite:db/levy.db");
- PreparedStatement statement = connection.prepareStatement("SELECT * FROM Kappale");
- ResultSet resultSet = statement.executeQuery();
- while (resultSet.next()) {
- String nimi = resultSet.getString("nimi");
- String artisti = resultSet.getString("artisti");
- Integer levytysvuosi = resultSet.getInt("levytysvuosi");
- Integer pituus = resultSet.getInt("pituus");
- System.out.println(artisti + ", " + nimi + " (" + pituus + " s), " + levytysvuosi);
- }
- connection.close();
- }
- public static void lisaa() throws SQLException {
- Scanner lukija = new Scanner(System.in);
- System.out.print("Nimi: ");
- String uusiNimi = lukija.nextLine();
- System.out.print("Artisti: ");
- String uusiArtisti = lukija.nextLine();
- System.out.print("Levytysvuosi: :");
- int uusiVuosi = Integer.parseInt(lukija.nextLine());
- System.out.print("Pituus: ");
- int uusiPituus = Integer.parseInt(lukija.nextLine());
- Connection connection = DriverManager.getConnection("jdbc:sqlite:db/levy.db");
- PreparedStatement statement = connection.prepareStatement("INSERT INTO Kappale (nimi, artisti, levytysvuosi, pituus) VALUES (?, ?, ?, ?)");
- statement.setString(1, uusiNimi);
- statement.setString(2, uusiArtisti);
- statement.setInt(3, uusiVuosi);
- statement.setInt(4, uusiPituus);
- int changes = statement.executeUpdate();
- statement.close();
- connection.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement