Advertisement
Guest User

Untitled

a guest
Feb 2nd, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.58 KB | None | 0 0
  1. package prvizadatakkolokvijum;
  2. import java.sql.*;
  3.  
  4. public class PrviZadatak {
  5.     public static void main(String[] args) {
  6.         try {
  7.             String dbUrl = "jdbc:mysql://localhost:3306/itsbaza";
  8.             String user = "root";
  9.             String pass = "";
  10.             Class.forName("com.mysql.jdbc.Driver");
  11.             Connection veza = DriverManager.getConnection(dbUrl, user, pass);
  12.             Statement stmt = veza.createStatement();
  13.             String upit = "SELECT * FROM student";
  14.             ResultSet rezultat = null;
  15.             try {
  16.                 rezultat = stmt.executeQuery(upit);
  17.             } catch (SQLException sqle) {
  18.                 System.out.println("Greska izvrsenja upita" + sqle);
  19.             }
  20.             System.out.println("Sadrzaj tabele Student:");
  21.             while (rezultat.next()) {                
  22.                 System.out.println(rezultat.getString("brind")+
  23.                         " " + rezultat.getString("ime") +
  24.                         " " + rezultat.getString("prezime") +
  25.                         " " + rezultat.getString("smer"));
  26.             }
  27.             stmt.close();
  28.             veza.close();
  29.         } catch (ClassNotFoundException cnfe) {
  30.             System.out.println("Nije ucitan upravljacki program: " + cnfe );
  31.         } catch(SecurityException se) {
  32.                 System.out.println("Nedozvoljena operacija: " + se);
  33.         } catch(SQLException sqle) {
  34.                 System.out.println("Greska konekcije: " + sqle);
  35.         }      
  36.     }
  37. }
  38. ------------------------------------------------------------------------------------------------------------------
  39. package prvizadatakkolokvijum;
  40. import java.sql.*;
  41. import java.util.Properties;
  42.  
  43. public class Brisnje {
  44.     public static void main(String []args){
  45.         try {
  46.             String dbUrl = "jdbc:mysql://localhost:3306/itsbaza";
  47.             String user = "root";
  48.             String pass = "";
  49.             Class.forName("com.mysql.jdbc.Driver");
  50.             Connection veza = DriverManager.getConnection(dbUrl, user, pass);
  51.        
  52.             String upit = "delete from student where ime=?";
  53.            
  54.             PreparedStatement PSTATEMENT = veza.prepareStatement(upit);
  55.            
  56.             PSTATEMENT.setString(1, new String("Mika"));
  57.                        
  58.             try {
  59.                 PSTATEMENT.executeUpdate();
  60.             } catch (SQLException sqle) {
  61.                 System.out.println("izuzetak" + sqle);
  62.             }
  63.             PSTATEMENT.close();
  64.             veza.close();
  65.            
  66.         } catch (ClassNotFoundException cnfe) {
  67.             System.out.println("Nije ucitan upravljacki program: " + cnfe );
  68.         } catch(SQLException sqle) {
  69.             System.out.println("Greska konekcije: " + sqle);
  70.         }      
  71.     }
  72. }
  73. --------------------------------------------------------------------------------------------------------------
  74. package prvizadatakkolokvijum;
  75. import java.sql.*;
  76. import java.util.Properties;
  77.  
  78. public class Unos {
  79.     public static void main(String []args){
  80.         try {
  81.             String dbUrl = "jdbc:mysql://localhost:3306/itsbaza";
  82.             String user = "root";
  83.             String pass = "";
  84.             Class.forName("com.mysql.jdbc.Driver");
  85.             Connection veza = DriverManager.getConnection(dbUrl, user, pass);
  86.             Statement stmt = veza.createStatement();
  87.             String upit = "Insert into student(brind,ime,prezime,smer) values(?,?,?,?)";
  88.            
  89.             PreparedStatement PSTATEMENT = veza.prepareStatement(upit);
  90.            
  91.             PSTATEMENT.setInt(1, 300);
  92.             PSTATEMENT.setString(2, new String("Mika"));
  93.             PSTATEMENT.setString(3, new String("Mikic"));
  94.             PSTATEMENT.setString(4, new String("IT"));
  95.                        
  96.             try {
  97.                 PSTATEMENT.executeUpdate();
  98.             } catch (SQLException sqle) {
  99.                 System.out.println("izuzetak" + sqle);
  100.             }
  101.             PSTATEMENT.close();
  102.             veza.close();
  103.            
  104.         } catch (ClassNotFoundException cnfe) {
  105.             System.out.println("Nije ucitan upravljacki program: " + cnfe );
  106.         }catch(SQLException sqle) {
  107.             System.out.println("Greska konekcije: " + sqle);
  108.         }      
  109.     }
  110. }
  111. -----------------------------------------------------------------------------------------------------------------
  112. -----------------------------------------------------------------------------------------------------------------
  113. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  114. <!DOCTYPE html>
  115. <html>
  116.     <head>
  117.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  118.         <title>Pocetna</title>
  119.     </head>
  120.     <body>
  121.         <a href="unos.jsp">unos</a>
  122.         <a href="ServletAuto?ispis=lista">lista</a>
  123.         <a href="ServletAuto?ispis=jedan&br=3">jedan</a>      
  124.     </body>
  125. </html>
  126. -----------------------------------------------------------------------------------------------------------------
  127. -----------------------------------------------------------------------------------------------------------------
  128. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  129. <!DOCTYPE html>
  130. <html>
  131.     <head>
  132.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  133.         <title>unos</title>
  134.     </head>
  135.     <body>
  136.         <form name="unosForma" action="ServletAuto" method="post">
  137.             <table border="1" width="100" cellspacing="5" cellpadding="5">
  138.                 <thead>
  139.                     <tr>
  140.                         <th colspan="2">Unos</th>
  141.                     </tr>
  142.                 </thead>
  143.                 <tbody>
  144.                     <tr>
  145.                         <td>marka: </td>
  146.                         <td><input type="text" name="txtMarka"></td>
  147.                     </tr>
  148.                     <tr>
  149.                         <td>cena: </td>
  150.                         <td><input type="text" name="txtCena"></td>
  151.                     </tr>
  152.                     <tr>
  153.                         <td>godiste: </td>
  154.                         <td><input type="text" name="txtGodiste"></td>
  155.                     </tr>
  156.                     <tr>
  157.                         <td colspan="2"><input type="submit" name="unosDugme" value="potvrdi"></td>
  158.                     </tr>
  159.                     <%
  160.                         String msg = (String)request.getAttribute("msg");
  161.                         if(msg != null && msg.length() > 0){
  162.                     %>
  163.                     <tr>
  164.                         <td colspan="2"><%= msg %></td>
  165.                     </tr>
  166.                     <%
  167.                         }
  168.                     %>
  169.                 </tbody>
  170.             </table>
  171.         </form>
  172.     </body>
  173. </html>
  174. -----------------------------------------------------------------------------------------------------------------
  175. -----------------------------------------------------------------------------------------------------------------
  176. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  177. <%@page import="Beans.Auto" %>
  178. <% Auto auto = (Auto)request.getAttribute("auto"); %>
  179. <!DOCTYPE html>
  180. <html>
  181.     <head>
  182.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  183.         <title>Auto</title>
  184.     </head>
  185.     <body>
  186.         <table border="1" width="100" cellspacing="5" cellpadding="5">
  187.             <thead>
  188.                 <tr>
  189.                     <th colspan="3"> podaci o automobilu</th>
  190.                 </tr>
  191.             </thead>
  192.             <tbody>
  193.                 <tr>
  194.                     <td>marka</td>
  195.                     <td>cena</td>
  196.                     <td>godiste</td>
  197.                 </tr>                
  198.                 <tr>
  199.                     <td><%= auto.getMarka()%></td>
  200.                     <td><%= auto.getCena()%></td>
  201.                     <td><%= auto.getGodiste()%></td>
  202.                 </tr>
  203.             </tbody>
  204.         </table>
  205.  
  206.     </body>
  207. </html>
  208. -----------------------------------------------------------------------------------------------------------------
  209. -----------------------------------------------------------------------------------------------------------------
  210. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  211. <%@page import="Beans.Auto" %>
  212. <%@page import="java.util.ArrayList" %>
  213. <%
  214.     ArrayList<Auto> automobili = (ArrayList<Auto>)request.getAttribute("listaAuta");
  215. %>
  216. <!DOCTYPE html>
  217. <html>
  218.     <head>
  219.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  220.         <title>Lista automobila</title>
  221.     </head>
  222.     <body>
  223.         <table border="1" width="100" cellspacing="5" cellpadding="5">
  224.             <thead>
  225.                 <tr>
  226.                     <th colspan="3"> podaci o automobilu</th>
  227.                 </tr>
  228.             </thead>
  229.             <tbody>
  230.                 <tr>
  231.                     <td>marka</td>
  232.                     <td>cena</td>
  233.                     <td>godiste</td>
  234.                 </tr>                
  235.                 <%
  236.                     for(Auto a: automobili){
  237.                 %>
  238.                 <tr>
  239.                     <td><%= a.getMarka()%></td>
  240.                     <td><%= a.getCena()%></td>
  241.                     <td><%= a.getGodiste()%></td>
  242.                 </tr>
  243.                 <%
  244.                     }
  245.                 %>
  246.             </tbody>
  247.         </table>
  248.     </body>
  249. </html>
  250. -----------------------------------------------------------------------------------------------------------------
  251. -----------------------------------------------------------------------------------------------------------------
  252. package Servleti;
  253.  
  254. import Beans.Auto;
  255. import java.util.ArrayList;
  256.  
  257. import java.io.IOException;
  258. import java.io.PrintWriter;
  259. import javax.servlet.ServletException;
  260. import javax.servlet.http.HttpServlet;
  261. import javax.servlet.http.HttpServletRequest;
  262. import javax.servlet.http.HttpServletResponse;
  263.  
  264. public class ServletAuto extends HttpServlet {
  265.  
  266.     @Override
  267.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  268.             throws ServletException, IOException {
  269.        
  270.         ArrayList<Auto> automobili = new ArrayList<Auto>();
  271.        
  272.             automobili.add(new Auto("Skoda",2000,1998));
  273.             automobili.add(new Auto("BMW",5800,2004));
  274.             automobili.add(new Auto("Toyota",3500,2002));
  275.             automobili.add(new Auto("Audi",4000,2005));
  276.        
  277.         String ispis = request.getParameter("ispis");
  278.        
  279.         if(ispis != null && ispis.length() > 0){
  280.             if (ispis.equals("lista")) {
  281.                 request.setAttribute("listaAuta", automobili);
  282.                 request.getRequestDispatcher("listaAuta.jsp").forward(request, response);
  283.             } else if(ispis.equals("jedan")){
  284.                 String br = request.getParameter("br");
  285.                
  286.                 int brr = Integer.parseInt(br);
  287.                
  288.                 if(br != null && br.length() > 0){
  289.                     request.setAttribute("auto", automobili.get(brr));
  290.                     request.getRequestDispatcher("auto.jsp").forward(request, response);              
  291.                 }                      
  292.             }
  293.         }else{
  294.             request.setAttribute("msg", "parametri nisu dobrog formata");
  295.             request.getRequestDispatcher("index.jsp").forward(request, response);
  296.         }
  297.     }
  298.  
  299.     @Override
  300.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  301.             throws ServletException, IOException {
  302.         String markaStr = request.getParameter("txtMarka");
  303.         String cenaStr = request.getParameter("txtCena");
  304.         String godisteStr = request.getParameter("txtGodiste");
  305.        
  306.         if(markaStr != null && markaStr.length()>0 && cenaStr != null && cenaStr.length()>0 && godisteStr != null && godisteStr.length()>0){
  307.             try {
  308.                 int cena = Integer.parseInt(cenaStr);
  309.                 int godiste = Integer.parseInt(godisteStr);
  310.                
  311.                 Auto auto = new Auto(markaStr,cena,godiste);
  312.                 request.setAttribute("auto", auto);
  313.                 request.getRequestDispatcher("auto.jsp").forward(request, response);
  314.             } catch (Exception e) {
  315.                 request.setAttribute("msg", "cena, godiste imaju los format");
  316.                 request.getRequestDispatcher("unos.jsp").forward(request, response);
  317.             }
  318.         }else{
  319.             request.setAttribute("msg", "popunite sva polja");
  320.             request.getRequestDispatcher("unos.jsp").forward(request, response);
  321.         }
  322.     }
  323. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement