Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package prvizadatakkolokvijum;
- import java.sql.*;
- public class PrviZadatak {
- public static void main(String[] args) {
- try {
- String dbUrl = "jdbc:mysql://localhost:3306/itsbaza";
- String user = "root";
- String pass = "";
- Class.forName("com.mysql.jdbc.Driver");
- Connection veza = DriverManager.getConnection(dbUrl, user, pass);
- Statement stmt = veza.createStatement();
- String upit = "SELECT * FROM student";
- ResultSet rezultat = null;
- try {
- rezultat = stmt.executeQuery(upit);
- } catch (SQLException sqle) {
- System.out.println("Greska izvrsenja upita" + sqle);
- }
- System.out.println("Sadrzaj tabele Student:");
- while (rezultat.next()) {
- System.out.println(rezultat.getString("brind")+
- " " + rezultat.getString("ime") +
- " " + rezultat.getString("prezime") +
- " " + rezultat.getString("smer"));
- }
- stmt.close();
- veza.close();
- } catch (ClassNotFoundException cnfe) {
- System.out.println("Nije ucitan upravljacki program: " + cnfe );
- } catch(SecurityException se) {
- System.out.println("Nedozvoljena operacija: " + se);
- } catch(SQLException sqle) {
- System.out.println("Greska konekcije: " + sqle);
- }
- }
- }
- ------------------------------------------------------------------------------------------------------------------
- package prvizadatakkolokvijum;
- import java.sql.*;
- import java.util.Properties;
- public class Brisnje {
- public static void main(String []args){
- try {
- String dbUrl = "jdbc:mysql://localhost:3306/itsbaza";
- String user = "root";
- String pass = "";
- Class.forName("com.mysql.jdbc.Driver");
- Connection veza = DriverManager.getConnection(dbUrl, user, pass);
- String upit = "delete from student where ime=?";
- PreparedStatement PSTATEMENT = veza.prepareStatement(upit);
- PSTATEMENT.setString(1, new String("Mika"));
- try {
- PSTATEMENT.executeUpdate();
- } catch (SQLException sqle) {
- System.out.println("izuzetak" + sqle);
- }
- PSTATEMENT.close();
- veza.close();
- } catch (ClassNotFoundException cnfe) {
- System.out.println("Nije ucitan upravljacki program: " + cnfe );
- } catch(SQLException sqle) {
- System.out.println("Greska konekcije: " + sqle);
- }
- }
- }
- --------------------------------------------------------------------------------------------------------------
- package prvizadatakkolokvijum;
- import java.sql.*;
- import java.util.Properties;
- public class Unos {
- public static void main(String []args){
- try {
- String dbUrl = "jdbc:mysql://localhost:3306/itsbaza";
- String user = "root";
- String pass = "";
- Class.forName("com.mysql.jdbc.Driver");
- Connection veza = DriverManager.getConnection(dbUrl, user, pass);
- Statement stmt = veza.createStatement();
- String upit = "Insert into student(brind,ime,prezime,smer) values(?,?,?,?)";
- PreparedStatement PSTATEMENT = veza.prepareStatement(upit);
- PSTATEMENT.setInt(1, 300);
- PSTATEMENT.setString(2, new String("Mika"));
- PSTATEMENT.setString(3, new String("Mikic"));
- PSTATEMENT.setString(4, new String("IT"));
- try {
- PSTATEMENT.executeUpdate();
- } catch (SQLException sqle) {
- System.out.println("izuzetak" + sqle);
- }
- PSTATEMENT.close();
- veza.close();
- } catch (ClassNotFoundException cnfe) {
- System.out.println("Nije ucitan upravljacki program: " + cnfe );
- }catch(SQLException sqle) {
- System.out.println("Greska konekcije: " + sqle);
- }
- }
- }
- -----------------------------------------------------------------------------------------------------------------
- -----------------------------------------------------------------------------------------------------------------
- <%@page contentType="text/html" pageEncoding="UTF-8"%>
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Pocetna</title>
- </head>
- <body>
- <a href="unos.jsp">unos</a>
- <a href="ServletAuto?ispis=lista">lista</a>
- <a href="ServletAuto?ispis=jedan&br=3">jedan</a>
- </body>
- </html>
- -----------------------------------------------------------------------------------------------------------------
- -----------------------------------------------------------------------------------------------------------------
- <%@page contentType="text/html" pageEncoding="UTF-8"%>
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>unos</title>
- </head>
- <body>
- <form name="unosForma" action="ServletAuto" method="post">
- <table border="1" width="100" cellspacing="5" cellpadding="5">
- <thead>
- <tr>
- <th colspan="2">Unos</th>
- </tr>
- </thead>
- <tbody>
- <tr>
- <td>marka: </td>
- <td><input type="text" name="txtMarka"></td>
- </tr>
- <tr>
- <td>cena: </td>
- <td><input type="text" name="txtCena"></td>
- </tr>
- <tr>
- <td>godiste: </td>
- <td><input type="text" name="txtGodiste"></td>
- </tr>
- <tr>
- <td colspan="2"><input type="submit" name="unosDugme" value="potvrdi"></td>
- </tr>
- <%
- String msg = (String)request.getAttribute("msg");
- if(msg != null && msg.length() > 0){
- %>
- <tr>
- <td colspan="2"><%= msg %></td>
- </tr>
- <%
- }
- %>
- </tbody>
- </table>
- </form>
- </body>
- </html>
- -----------------------------------------------------------------------------------------------------------------
- -----------------------------------------------------------------------------------------------------------------
- <%@page contentType="text/html" pageEncoding="UTF-8"%>
- <%@page import="Beans.Auto" %>
- <% Auto auto = (Auto)request.getAttribute("auto"); %>
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Auto</title>
- </head>
- <body>
- <table border="1" width="100" cellspacing="5" cellpadding="5">
- <thead>
- <tr>
- <th colspan="3"> podaci o automobilu</th>
- </tr>
- </thead>
- <tbody>
- <tr>
- <td>marka</td>
- <td>cena</td>
- <td>godiste</td>
- </tr>
- <tr>
- <td><%= auto.getMarka()%></td>
- <td><%= auto.getCena()%></td>
- <td><%= auto.getGodiste()%></td>
- </tr>
- </tbody>
- </table>
- </body>
- </html>
- -----------------------------------------------------------------------------------------------------------------
- -----------------------------------------------------------------------------------------------------------------
- <%@page contentType="text/html" pageEncoding="UTF-8"%>
- <%@page import="Beans.Auto" %>
- <%@page import="java.util.ArrayList" %>
- <%
- ArrayList<Auto> automobili = (ArrayList<Auto>)request.getAttribute("listaAuta");
- %>
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Lista automobila</title>
- </head>
- <body>
- <table border="1" width="100" cellspacing="5" cellpadding="5">
- <thead>
- <tr>
- <th colspan="3"> podaci o automobilu</th>
- </tr>
- </thead>
- <tbody>
- <tr>
- <td>marka</td>
- <td>cena</td>
- <td>godiste</td>
- </tr>
- <%
- for(Auto a: automobili){
- %>
- <tr>
- <td><%= a.getMarka()%></td>
- <td><%= a.getCena()%></td>
- <td><%= a.getGodiste()%></td>
- </tr>
- <%
- }
- %>
- </tbody>
- </table>
- </body>
- </html>
- -----------------------------------------------------------------------------------------------------------------
- -----------------------------------------------------------------------------------------------------------------
- package Servleti;
- import Beans.Auto;
- import java.util.ArrayList;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- public class ServletAuto extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- ArrayList<Auto> automobili = new ArrayList<Auto>();
- automobili.add(new Auto("Skoda",2000,1998));
- automobili.add(new Auto("BMW",5800,2004));
- automobili.add(new Auto("Toyota",3500,2002));
- automobili.add(new Auto("Audi",4000,2005));
- String ispis = request.getParameter("ispis");
- if(ispis != null && ispis.length() > 0){
- if (ispis.equals("lista")) {
- request.setAttribute("listaAuta", automobili);
- request.getRequestDispatcher("listaAuta.jsp").forward(request, response);
- } else if(ispis.equals("jedan")){
- String br = request.getParameter("br");
- int brr = Integer.parseInt(br);
- if(br != null && br.length() > 0){
- request.setAttribute("auto", automobili.get(brr));
- request.getRequestDispatcher("auto.jsp").forward(request, response);
- }
- }
- }else{
- request.setAttribute("msg", "parametri nisu dobrog formata");
- request.getRequestDispatcher("index.jsp").forward(request, response);
- }
- }
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- String markaStr = request.getParameter("txtMarka");
- String cenaStr = request.getParameter("txtCena");
- String godisteStr = request.getParameter("txtGodiste");
- if(markaStr != null && markaStr.length()>0 && cenaStr != null && cenaStr.length()>0 && godisteStr != null && godisteStr.length()>0){
- try {
- int cena = Integer.parseInt(cenaStr);
- int godiste = Integer.parseInt(godisteStr);
- Auto auto = new Auto(markaStr,cena,godiste);
- request.setAttribute("auto", auto);
- request.getRequestDispatcher("auto.jsp").forward(request, response);
- } catch (Exception e) {
- request.setAttribute("msg", "cena, godiste imaju los format");
- request.getRequestDispatcher("unos.jsp").forward(request, response);
- }
- }else{
- request.setAttribute("msg", "popunite sva polja");
- request.getRequestDispatcher("unos.jsp").forward(request, response);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement