Commit 01f715be by Bogdan Andjelkovic

upload slike sa odg oglasid - baza

parent d57093cb
...@@ -117,7 +117,7 @@ public class KorisnikRepository ...@@ -117,7 +117,7 @@ public class KorisnikRepository
public List<Korisnik> findAll() public List<Korisnik> findAll()
{ {
String sql = "select * from korisnik where TipID = 1"; /*String sql = "select * from korisnik where TipID = 1";
List<Korisnik> list = new ArrayList<Korisnik>(); List<Korisnik> list = new ArrayList<Korisnik>();
ResultSet rs = null; ResultSet rs = null;
try { try {
...@@ -129,6 +129,21 @@ public class KorisnikRepository ...@@ -129,6 +129,21 @@ public class KorisnikRepository
} }
catch (SQLException e) { catch (SQLException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
}*/
CallableStatement stmt = null;
ResultSet rs = null;
try {
String sql = "{ call findAllKorisnik}";
stmt = dataBase.connection.prepareCall( sql);
List<Korisnik> list = new ArrayList<Korisnik>();
rs = stmt.executeQuery();
while (rs.next()) {
list.add( new Korisnik( rs.getInt("korisnikId"), rs.getString("ime"), rs.getString("prezime"), rs.getString("email"), rs.getString("sifra"), rs.getString("telefon"), rs.getString("slika"), rs.getInt("tipid"), rs.getInt("gradid"), rs.getBoolean("enabled")));
}
return list;
}
catch (SQLException e) {
throw new RuntimeException(e);
} }
} }
......
...@@ -6,8 +6,7 @@ import org.springframework.stereotype.Component; ...@@ -6,8 +6,7 @@ import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.IOException; import java.io.IOException;
import java.sql.SQLException; import java.sql.*;
import java.sql.Statement;
import java.util.Base64; import java.util.Base64;
@Component @Component
...@@ -19,33 +18,36 @@ public class OglasRepository ...@@ -19,33 +18,36 @@ public class OglasRepository
public void insert( int korisnikId, Oglas newOglas, MultipartFile[] files) public void insert( int korisnikId, Oglas newOglas, MultipartFile[] files)
{ {
int oglasId; int oglasId;
String sql = "insert into oglas( korisnikID, naslov, opis, lokacijaId, cena, vrstaoglasaid, kvadratura, brojsoba, namesten, grejanje, lift, lokacija)" + //String sql = "{ call insertOglas(?,?,?,?,?,?,?,?,?,?,?,?)}";
"values ( " + korisnikId + String sql = "insert into oglas( korisnikID, naslov, opis, lokacijaId, cena, vrstaoglasaid, kvadratura, brojsoba, namesten, grejanje, lift, lokacija) values(?,?,?,?,?,?,?,?,?,?,?,?);";
", '" + newOglas.getNaslov() + PreparedStatement stmt = null;
"','" + newOglas.getOpis() +
"'," + newOglas.getLokacijaId() +
"," + newOglas.getCena() +
"," + newOglas.getVrstaOglasaId() +
"," + newOglas.getKvadratura() +
"," + newOglas.getBrojSoba() +
"," + newOglas.isNamesten() +
"," + newOglas.isGrejanje() +
"," + newOglas.isLift() +
",'" + newOglas.getLokacija() + "')";
System.out.println( sql);
try { try {
oglasId = dataBase.statement.executeUpdate( sql, Statement.RETURN_GENERATED_KEYS); stmt = dataBase.connection.prepareStatement( sql, Statement.RETURN_GENERATED_KEYS);
System.out.println( oglasId); stmt.setInt(1,korisnikId);
stmt.setString(2,newOglas.getNaslov());
for( MultipartFile file: files) stmt.setString(3,newOglas.getOpis());
{ stmt.setInt(4,newOglas.getLokacijaId());
sql = "insert into Slike( oglasid, slika)"; stmt.setDouble(5,newOglas.getCena());
String filename = Base64.getEncoder().encodeToString( file.getBytes()); stmt.setInt(6,newOglas.getVrstaOglasaId());
sql += " values ("+oglasId+",'"+filename+"')"; stmt.setDouble(7,newOglas.getKvadratura());
dataBase.statement.executeUpdate( sql); stmt.setInt(8,newOglas.getBrojSoba());
stmt.setBoolean(9,newOglas.isNamesten());
stmt.setBoolean(10,newOglas.isGrejanje());
stmt.setBoolean(11,newOglas.isLift());
stmt.setString(12,newOglas.getLokacija());
stmt.addBatch();
stmt.executeBatch();
ResultSet rs = stmt.getGeneratedKeys();
if( rs.next()) {
oglasId = rs.getInt(1);
for( MultipartFile file: files)
{
sql = "insert into Slike( oglasid, slika)";
String filename = Base64.getEncoder().encodeToString( file.getBytes());
sql += " values ("+oglasId+",'"+filename+"')";
dataBase.statement.executeUpdate( sql);
}
} }
System.out.println("slike inserted");
} catch (SQLException e) { } catch (SQLException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} catch (IOException e) { } catch (IOException e) {
......
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