Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.ResultSetMetaData;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Scanner;
- public class mainn {
- public static void main(String[] args)
- {
- Scanner input = new Scanner(System.in);
- String url = "jdbc:mysql://localhost:3306/test2db"+
- "?verifyServerCertificate=false"+
- "&useSSL=true";
- String user = "root";
- String pass = "mrainko";
- String password, izbor = "aa";
- System.out.print("Lozinka: ");
- password = input.nextLine();
- if (!(pass.equals(password)))
- {
- System.out.println("Netocna lozinka!");
- System.exit(0);
- }
- while(izbor != "k")
- {
- System.out.printf("Izbor[u-unos, b-brisanje, up-update, k-kraj]: ");
- izbor = input.nextLine();
- switch(izbor)
- {
- case "u": // Unos
- Unos(url, user, password);
- break;
- case "i": // Ispis
- break;
- case "b": // Brisanje
- Brisanje(url, user, password);
- break;
- case "up": // update
- Update(url, user, password);
- break;
- case "k":
- System.out.println("Kraj!");
- System.exit(0);
- default:
- System.out.println("Netocan izbor!");
- break;
- }
- }
- input.close();
- }
- public static void Unos(String url, String user, String password)
- {
- Connection myConn = null;
- Scanner input = new Scanner(System.in);
- String ime="aa", ime1 = "admin", prezime, adresa, brojTel, godRod, sql;
- try
- {
- myConn = DriverManager.getConnection(url, user, password);
- sql = "insert into korisnici"
- + "(Ime, Prezime, Adresa, BrojTelefona, GodinaRođenja)"
- + "values(?, ?, ?, ?, ?)";
- while((!ime1.equals(ime)))
- {
- System.out.print("Ime: ");
- ime = input.nextLine();
- if((!ime1.equals(ime)))
- {
- System.out.print("Prezime: ");
- prezime = input.nextLine();
- System.out.print("Adresa: ");
- adresa = input.nextLine();
- System.out.print("Broj telefona: ");
- brojTel = input.nextLine();
- System.out.print("Godina rođenja: ");
- godRod = input.nextLine();
- PreparedStatement pS = myConn.prepareStatement(sql);
- pS.setString(1, ime);
- pS.setString(2, prezime);
- pS.setString(3, adresa);
- pS.setString(4, brojTel);
- pS.setString(5, godRod);
- pS.executeUpdate();
- }
- }
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- finally
- {
- if(myConn != null)
- {
- try
- {
- myConn.close();
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
- }
- }
- public static void Ispis(String url, String user, String password)
- {
- }
- public static void Brisanje(String url, String user, String password)
- {
- Scanner input = new Scanner(System.in);
- Connection myConn = null;
- String sql = "SELECT ID FROM korisnici WHERE Ime= ? and Prezime= ? ";
- String sql2 = "delete from korisnici where ID= ? ";
- String ime, prezime;
- try
- {
- int id = 0;
- myConn = DriverManager.getConnection(url, user, password);
- PreparedStatement pS = myConn.prepareStatement(sql);
- PreparedStatement pS2 = myConn.prepareStatement(sql2);
- System.out.print("Ime: ");
- ime = input.nextLine();
- System.out.print("Prezime: ");
- prezime = input.nextLine();
- pS.setString(1, ime);
- pS.setString(2, prezime);
- ResultSet rs = pS.executeQuery();
- while (rs.next())
- {
- id = rs.getInt("ID");
- }
- pS2.setInt(1, id);
- pS2.executeUpdate();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- public static void Update(String url, String user, String password)
- {
- Connection myConn = null;
- try
- {
- myConn = DriverManager.getConnection(url, user, password);
- String sql = "UPDATE korisnici SET BrojTelefona = ? WHERE ID= ? ";
- PreparedStatement ps = myConn.prepareStatement(sql);
- ps.setInt(2, 1);
- ps.setString(1, "2222");
- ps.executeUpdate();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- finally
- {
- if(myConn != null)
- {
- try
- {
- myConn.close();
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment