Mrain

JavaDBConnection2

Jan 7th, 2016
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.65 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.ResultSetMetaData;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.Scanner;
  9.  
  10. public class mainn {
  11.     public static void main(String[] args)
  12.     {  
  13.         Scanner input = new Scanner(System.in);
  14.         String url = "jdbc:mysql://localhost:3306/test2db"+
  15.                 "?verifyServerCertificate=false"+
  16.                 "&useSSL=true";
  17.         String user = "root";
  18.         String pass = "mrainko";
  19.         String password, izbor = "aa";
  20.        
  21.         System.out.print("Lozinka: ");
  22.         password = input.nextLine();
  23.        
  24.         if (!(pass.equals(password)))
  25.         {
  26.             System.out.println("Netocna lozinka!");
  27.             System.exit(0);
  28.         }
  29.            
  30.        
  31.         while(izbor != "k")
  32.         {
  33.             System.out.printf("Izbor[u-unos, b-brisanje, up-update, k-kraj]: ");
  34.             izbor = input.nextLine();
  35.            
  36.             switch(izbor)
  37.             {
  38.             case "u":  // Unos
  39.                 Unos(url, user, password);
  40.                 break;
  41.                
  42.             case "i": // Ispis
  43.                 break;
  44.                
  45.             case "b": // Brisanje
  46.                 Brisanje(url, user, password);
  47.                 break;
  48.                
  49.             case "up": // update
  50.                 Update(url, user, password);
  51.                 break;
  52.            
  53.             case "k":
  54.                 System.out.println("Kraj!");
  55.                 System.exit(0);
  56.                
  57.             default:
  58.                 System.out.println("Netocan izbor!");
  59.                 break;
  60.             }
  61.         }
  62.        
  63.         input.close();
  64.     }
  65.    
  66.     public static void Unos(String url, String user, String password)
  67.     {
  68.         Connection myConn = null;
  69.         Scanner input = new Scanner(System.in);
  70.        
  71.         String ime="aa", ime1 = "admin", prezime, adresa, brojTel, godRod, sql;
  72.        
  73.         try
  74.         {
  75.             myConn = DriverManager.getConnection(url, user, password);
  76.            
  77.             sql = "insert into korisnici"
  78.                 + "(Ime, Prezime, Adresa, BrojTelefona, GodinaRođenja)"
  79.                 + "values(?, ?, ?, ?, ?)";
  80.            
  81.             while((!ime1.equals(ime)))
  82.             {
  83.                 System.out.print("Ime: ");
  84.                 ime = input.nextLine();
  85.                
  86.                 if((!ime1.equals(ime)))
  87.                 {
  88.                     System.out.print("Prezime: ");
  89.                     prezime = input.nextLine();
  90.                     System.out.print("Adresa: ");
  91.                     adresa = input.nextLine();
  92.                     System.out.print("Broj telefona: ");
  93.                     brojTel = input.nextLine();
  94.                     System.out.print("Godina rođenja: ");
  95.                     godRod = input.nextLine();
  96.                    
  97.                     PreparedStatement pS = myConn.prepareStatement(sql);
  98.                     pS.setString(1, ime);
  99.                     pS.setString(2, prezime);
  100.                     pS.setString(3, adresa);
  101.                     pS.setString(4, brojTel);
  102.                     pS.setString(5, godRod);
  103.                
  104.                     pS.executeUpdate();
  105.                 }
  106.             }
  107.         }
  108.        
  109.         catch (Exception e)
  110.         {
  111.             e.printStackTrace();
  112.         }
  113.        
  114.         finally
  115.         {
  116.             if(myConn != null)
  117.             {
  118.                 try
  119.                 {
  120.                     myConn.close();
  121.                 }
  122.                 catch(Exception e)
  123.                 {
  124.                     e.printStackTrace();
  125.                 }
  126.             }
  127.         }
  128.     }
  129.    
  130.     public static void Ispis(String url, String user, String password)
  131.     {
  132.        
  133.     }
  134.    
  135.     public static void Brisanje(String url, String user, String password)
  136.     {
  137.         Scanner input = new Scanner(System.in);
  138.         Connection myConn = null;
  139.        
  140.         String sql = "SELECT ID FROM korisnici WHERE Ime= ? and Prezime= ? ";
  141.         String sql2 = "delete from korisnici where ID= ? ";
  142.         String ime, prezime;
  143.        
  144.         try
  145.         {
  146.             int id = 0;
  147.             myConn = DriverManager.getConnection(url, user, password);
  148.             PreparedStatement pS = myConn.prepareStatement(sql);
  149.             PreparedStatement pS2 = myConn.prepareStatement(sql2);
  150.            
  151.             System.out.print("Ime: ");
  152.             ime = input.nextLine();
  153.             System.out.print("Prezime: ");
  154.             prezime = input.nextLine();
  155.             pS.setString(1, ime);
  156.             pS.setString(2, prezime);
  157.            
  158.             ResultSet rs = pS.executeQuery();
  159.            
  160.             while (rs.next())
  161.             {
  162.                 id = rs.getInt("ID");
  163.             }
  164.            
  165.             pS2.setInt(1, id);
  166.             pS2.executeUpdate();
  167.         }
  168.        
  169.         catch (Exception e)
  170.         {
  171.             e.printStackTrace();
  172.         }
  173.     }
  174.  
  175.     public static void Update(String url, String user, String password)
  176.     {
  177.         Connection myConn = null;
  178.        
  179.         try
  180.         {
  181.             myConn = DriverManager.getConnection(url, user, password);
  182.            
  183.             String sql = "UPDATE korisnici SET BrojTelefona = ? WHERE ID= ? ";
  184.            
  185.             PreparedStatement ps = myConn.prepareStatement(sql);
  186.             ps.setInt(2, 1);
  187.             ps.setString(1, "2222");
  188.             ps.executeUpdate();
  189.         }
  190.        
  191.         catch (Exception e)
  192.         {
  193.             e.printStackTrace();
  194.         }
  195.        
  196.         finally
  197.         {
  198.             if(myConn != null)
  199.             {
  200.                 try
  201.                 {
  202.                     myConn.close();
  203.                 }
  204.                 catch(Exception e)
  205.                 {
  206.                     e.printStackTrace();
  207.                 }
  208.             }
  209.         }
  210.     }
  211. }
Add Comment
Please, Sign In to add comment