Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package preg;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import com.mysql.jdbc.Statement;
- public class Ex3SQL {
- public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException,
- SQLException, IOException {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- String url = "jdbc:mysql://localhost:3306/test";
- Connection con = DriverManager.getConnection(url, "root", null);
- // probabil va fi (url, "root", "root") la test, eu n-am parola la
- // server
- Statement sql;
- sql = (Statement) con.createStatement();
- ResultSet rs;
- // ResultSet folosit doar la comenzile de Select. Comanda
- // executeUpdateQuery returneaza int si poate fi apelata pur si simplu
- int o = 0;
- BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
- do {
- System.out.println("------MENIU------");
- System.out.println("1. Afisare");
- System.out.println("2. Adaugare destinatie");
- System.out.println("3. Actualizare nr obiective pt o statiune data");
- System.out.println("4. Iesire");
- System.out.println("Optiune: ");
- try {
- o = Integer.parseInt(f.readLine());
- } catch (NumberFormatException e) {
- System.out.println("Introduceti un numar! Eroare " + e);
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println();
- switch (o) {
- case 1:
- rs = sql.executeQuery("select * from destinatii_turistice");
- while (rs.next())
- System.out.println(rs.getString("tara") + " " + rs.getString("statiune") + " "
- + rs.getInt("nr_obiective"));
- System.out.println();
- break;
- case 2:
- String tara,
- statiune;
- int nr = 0;
- System.out.println("Tara: ");
- tara = f.readLine();
- System.out.println("Statiunea: ");
- statiune = f.readLine();
- System.out.println("Numar obiective: ");
- try {
- nr = Integer.valueOf(f.readLine());
- } catch (NumberFormatException e) {
- System.out.println("EROARE: Introduceti un numar! " + e);
- e.printStackTrace();
- }
- if (nr != 0)
- // executeUpdateQuery returneaza int cu nr de inregistrari
- // modificate asa ca am pus o
- // verificare:
- if (sql.executeUpdate("insert into destinatii_turistice (id, tara, statiune, nr_obiective) values (NULL, \""
- + tara + "\", \"" + statiune + "\", \"" + nr + "\")") != 0)
- System.out.println("Destinatia a fost introdusa in tabel!");
- else
- // Daca nr ramane 0 cum a fost declarat mai sus...
- System.out.println("Atentie! Destinatia NU a fost introdusa in tabel!");
- break;
- case 3:
- int nr1 = 0;
- System.out.println("Statiunea: ");
- statiune = f.readLine();
- System.out.println("Numar obiective: ");
- try {
- nr1 = Integer.valueOf(f.readLine());
- } catch (NumberFormatException e) {
- System.out.println("EROARE: Introduceti un numar! " + e);
- e.printStackTrace();
- }
- if (nr1 != 0)
- if (sql.executeUpdate("update destinatii_turistice set nr_obiective=" + nr1 + " where statiune=\""
- + statiune + "\"") != 0)
- System.out.println("Numarul de obiective turistice pentru statiunea " + statiune
- + " a fost modificat la " + nr1 + "!");
- break;
- case 4:
- break;
- default:
- System.out.println("Optiune invalida.");
- }
- } while (o != 4);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement