Commit 4ad07e93 by Patryk Czarnik

wstrzykiwanie DataSource

parent 4c23c0a9
...@@ -6,19 +6,24 @@ import java.sql.ResultSet; ...@@ -6,19 +6,24 @@ import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
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;
@Controller @Controller
public class ProductController { public class ProductController {
@Autowired
private DataSource dataSource;
@RequestMapping(path="/products", produces="text/plain") @RequestMapping(path="/products", produces="text/plain")
@ResponseBody @ResponseBody
public String wszystkieProdukty() { public String wszystkieProdukty() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
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")) {
......
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