Commit 8e8325f5 by Bogdan Andjelkovic

lokacija i database

parent c799f62f
package com.example.SkuciSe.model.lokacija;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Lokacija
{
int lokacijaId;
String naziv;
}
package com.example.SkuciSe.repository;
import org.springframework.stereotype.Component;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
@Component
public class DataBase
{
Connection connection = null;
Statement statement = null;
public DataBase()
{
try {
connection = DriverManager.getConnection("jdbc:mariadb://localhost/skucise","root","");
statement = connection.createStatement();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
package com.example.SkuciSe.repository;
import com.example.SkuciSe.model.lokacija.Lokacija;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@Component
public class LokacijaRepository
{
@Autowired
DataBase dataBase;
public List<Lokacija> findAll()
{
String sql = "select * from lokacija";
List<Lokacija> list = new ArrayList<Lokacija>();
ResultSet rs = null;
try {
rs = dataBase.statement.executeQuery( sql);
while( rs.next())
{
list.add( new Lokacija( rs.getInt("lokacijaid"), rs.getString("naziv")));
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
return list;
}
public Lokacija findById(int lokacijaId)
{
String sql = "select * from lokacija where lokacijaid = " + lokacijaId;
ResultSet rs = null;
try {
rs = dataBase.statement.executeQuery( sql);
while( rs.next())
{
return new Lokacija( rs.getInt("lokacijaid"), rs.getString("naziv"));
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
return null;
}
}
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