Commit 42ab09b6 by Patryk Czarnik

Dodatkowa wersja z DataSource

parent 21b5c472
package sklep.alternatywne_dostepy_do_bazy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.sql.*;
@Controller
@RequestMapping("/alt0")
public class ProductController_v0 {
// W tej wersji samodzielnie nawiązuję połączenie z bazą
@GetMapping(produces = "text/plain;charset=UTF-8")
@ResponseBody
public String readAll() {
StringBuilder sb = new StringBuilder("Lista produktów:\n");
try(Connection c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/sklep", "kurs", "abc123");
Statement stmt = c.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM products ORDER BY product_id")) {
while(rs.next()) {
sb.append("* produkt ")
.append(rs.getString("product_name"))
.append(" za cenę ")
.append(rs.getBigDecimal("price"))
.append('\n');
}
} catch (SQLException e) {
e.printStackTrace();
sb.append("Błąd: ").append(e);
}
return sb.toString();
}
}
package sklep.alternatywne_dostepy_do_bazy; package sklep.alternatywne_dostepy_do_bazy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import javax.sql.DataSource;
import java.sql.*; import java.sql.*;
@Controller @Controller
@RequestMapping("/alt1") @RequestMapping("/alt1")
public class ProductController_v1 { public class ProductController_v1 {
@Autowired
private DataSource dataSource;
// W tej wersji Spring dostarcza nam obiekt DataSource na podstawie konfiguracji w pliku application.properties
@GetMapping(produces = "text/plain;charset=UTF-8") @GetMapping(produces = "text/plain;charset=UTF-8")
@ResponseBody @ResponseBody
public String readAll() { public String readAll() {
StringBuilder sb = new StringBuilder("Lista produktów:\n"); StringBuilder sb = new StringBuilder("Lista produktów:\n");
try(Connection c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/sklep", "kurs", "abc123"); try(Connection c = dataSource.getConnection();
Statement stmt = c.createStatement(); Statement stmt = c.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM products ORDER BY product_id")) { ResultSet rs = stmt.executeQuery("SELECT * FROM products ORDER BY product_id")) {
...@@ -28,7 +33,7 @@ public class ProductController_v1 { ...@@ -28,7 +33,7 @@ public class ProductController_v1 {
} }
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
sb.append("Błąd: " + e); sb.append("Błąd: ").append(e);
} }
return sb.toString(); return sb.toString();
} }
......
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