Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Servleti;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- public class Unos extends HttpServlet
- {
- String ime;
- String prezime;
- String godStaza;
- int GodStaza;
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException
- {
- response.setContentType("text/html;charset=UTF-8");
- provera(request,response);
- }
- public void provera(HttpServletRequest request,HttpServletResponse response)
- throws ServletException, IOException {
- ime = request.getParameter("ime");
- prezime = request.getParameter("prezime");
- godStaza = request.getParameter("staz");
- if(ime != null && ime.length() > 0 &&
- prezime != null && prezime.length() > 0 &&
- godStaza != null && godStaza.length() > 0) {
- try {
- GodStaza = Integer.parseInt(godStaza);
- upisiUBazu(request,response,GodStaza);
- }
- catch(Exception e) {
- request.setAttribute("msg", "Godine staza moraju biti ceo broj!" + e);
- request.getRequestDispatcher("Upis.jsp").forward(request, response);
- }
- }
- else {
- request.setAttribute("msg", "Sva polja moraju biti popunjena!");
- request.getRequestDispatcher("Upis.jsp").forward(request, response);
- }
- }
- public void upisiUBazu(HttpServletRequest request,HttpServletResponse response,int GodStaza)
- throws ServletException, IOException {
- ime = request.getParameter("ime");
- prezime = request.getParameter("prezime");
- int godine = GodStaza;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/nastava","root","");
- String upit = "INSERT INTO radnik(Ime,Prezime,GodineStaza)VALUES(?,?,?)";
- PreparedStatement ps = conn.prepareStatement(upit);
- ps.setString(1, ime);
- ps.setString(2, prezime);
- ps.setInt(3, godine);
- int izvrseno = 0;
- izvrseno = ps.executeUpdate();
- if(izvrseno > 0) {
- request.setAttribute("msg", "Uspesno ste upisali radnika u bazu!");
- request.getRequestDispatcher("index.jsp").forward(request, response);
- }
- else {
- request.setAttribute("msg", "Radnik nije upisan u bazu!");
- request.getRequestDispatcher("Upis.jsp");
- }
- }
- catch(SQLException sqle) {
- request.setAttribute("msg", "Greska u upitu!" + sqle);
- request.getRequestDispatcher("Upis.jsp");
- } catch (ClassNotFoundException ex)
- {
- request.setAttribute("msg", "Greska sa bazom!" + ex);
- request.getRequestDispatcher("Upis.jsp");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement