Commit 068f3644 by Nikola Markovic

sql procedure: insertKorisnik, updateKorisnik

parent 1b92a3a7
...@@ -22,10 +22,23 @@ public class KorisnikRepository ...@@ -22,10 +22,23 @@ public class KorisnikRepository
public void insert( Korisnik korisnik, MultipartFile multipartFile) throws EmailPostoji { public void insert( Korisnik korisnik, MultipartFile multipartFile) throws EmailPostoji {
if( this.findByEmail( korisnik.getEmail()) == null) if( this.findByEmail( korisnik.getEmail()) == null)
{ {
CallableStatement stmt = null;
ResultSet rs = null;
try { try {
String slika = Base64.getEncoder().encodeToString(multipartFile.getBytes()); String slika = Base64.getEncoder().encodeToString(multipartFile.getBytes());
String sql = "insert into korisnik( ime, prezime, telefon, email, sifra, tipId, slika, gradid, enabled) values('"+korisnik.getIme()+"','"+korisnik.getPrezime()+"','"+korisnik.getTelefon()+"','"+korisnik.getEmail()+"','"+new BCryptPasswordEncoder().encode(korisnik.getSifra())+"',1, '"+ slika +"', "+korisnik.getGradId()+",false)"; String sql = "{call insertKorisnik(?,?,?,?,?,?,?,?)}";
dataBase.statement.executeUpdate( sql); //String sql = "insert into korisnik( ime, prezime, telefon, email, sifra, tipId, slika, gradid) values('"+korisnik.getIme()+"','"+korisnik.getPrezime()+"','"+korisnik.getTelefon()+"','"+korisnik.getEmail()+"','"+new BCryptPasswordEncoder().encode(korisnik.getSifra())+"',1, '"+ slika +"', "+korisnik.getGradId()+",false)";
stmt = dataBase.connection.prepareCall( sql);
stmt.setString(1,korisnik.getIme());
stmt.setString(2,korisnik.getPrezime());
stmt.setString(3,korisnik.getTelefon());
stmt.setString(4,korisnik.getEmail());
stmt.setString(5,new BCryptPasswordEncoder().encode(korisnik.getSifra()));
stmt.setInt(6,1);
stmt.setString(7,slika);
stmt.setInt(8,korisnik.getGradId());
stmt.addBatch();
stmt.executeBatch();
} catch (IOException e) { } catch (IOException e) {
System.out.println(e.getMessage()); System.out.println(e.getMessage());
} catch (SQLException e) { } catch (SQLException e) {
...@@ -40,17 +53,20 @@ public class KorisnikRepository ...@@ -40,17 +53,20 @@ public class KorisnikRepository
public void update( Korisnik korisnik) public void update( Korisnik korisnik)
{ {
String sql = " update korisnik " + CallableStatement stmt = null;
" set ime = '" + korisnik.getIme() + String sql = "{call updateKorisnik(?,?,?,?,?,?)}";
"',prezime = '" + korisnik.getPrezime() +
"',email = '" + korisnik.getEmail() +
"',sifra = '" + new BCryptPasswordEncoder().encode(korisnik.getSifra()) +
"',telefon = '" + korisnik.getTelefon() +
"',gradId = '" + korisnik.getGradId() +
"' where korisnikid = " + korisnik.getKorisnikId();
try { try {
dataBase.statement.executeUpdate( sql); stmt = dataBase.connection.prepareCall(sql);
} catch (SQLException e) { stmt.setString(1,korisnik.getIme());
stmt.setString(2,korisnik.getPrezime());
stmt.setString(3,korisnik.getEmail());
stmt.setString(4,korisnik.getTelefon());
stmt.setInt(5,korisnik.getGradId());
stmt.setInt(6,korisnik.getKorisnikId());
System.out.println(stmt.toString());
stmt.execute();
}
catch (SQLException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
...@@ -61,8 +77,7 @@ public class KorisnikRepository ...@@ -61,8 +77,7 @@ public class KorisnikRepository
String sql = null; String sql = null;
try { try {
slika = Base64.getEncoder().encodeToString(file.getBytes()); slika = Base64.getEncoder().encodeToString(file.getBytes());
sql = " update korisnik " + sql = " update korisnik " + " set slika = '" + slika + "' where korisnikid = " + korisnik.getKorisnikId();
" set slika = '" + slika + "' where korisnikid = " + korisnik.getKorisnikId();
dataBase.statement.executeUpdate( sql); dataBase.statement.executeUpdate( sql);
} catch (SQLException e) { } catch (SQLException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
......
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head> <head>
<meta charset="utf-8"/> <meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"/> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"/>
...@@ -93,7 +93,8 @@ ...@@ -93,7 +93,8 @@
<form class="requires-validation" novalidate th:object="${editUser}" method="POST" <form class="requires-validation" novalidate th:object="${editUser}" method="POST"
th:action="@{/profile/password-edit-save}" enctype="multipart/form-data" th:action="@{/profile/password-edit-save}" enctype="multipart/form-data"
onsubmit="return validateForm()"> onsubmit="return validateForm()">
<input th:field="${editUser.korisnikId}" class="form-control" type="hidden"
name="name" placeholder="Ime" required>
<div class="col-md-12"> <div class="col-md-12">
<span style="color:white;">Unesite novu sifru</span> <span style="color:white;">Unesite novu sifru</span>
<input th:field="${editUser.sifra}" type="password" <input th:field="${editUser.sifra}" type="password"
......
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head> <head>
<meta charset="utf-8"/> <meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"/> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"/>
...@@ -94,6 +94,8 @@ ...@@ -94,6 +94,8 @@
<div class="form-items"> <div class="form-items">
<form class="requires-validation" novalidate th:object="${editUser}" method="POST" <form class="requires-validation" novalidate th:object="${editUser}" method="POST"
th:action="@{/profile/edit-save}" enctype="multipart/form-data"> th:action="@{/profile/edit-save}" enctype="multipart/form-data">
<input th:field="${editUser.korisnikId}" class="form-control" type="hidden"
name="name" placeholder="Ime" required>
<div class="col-md-12"> <div class="col-md-12">
<input th:field="${editUser.ime}" class="form-control" type="text" <input th:field="${editUser.ime}" class="form-control" type="text"
name="name" placeholder="Ime" required> name="name" placeholder="Ime" required>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment