Advertisement
Guest User

alen

a guest
May 3rd, 2017
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. TeamController:
  2. public void create(String teamName) throws SQLException {
  3. Connection con = Database.getConnection();
  4. try {
  5. PreparedStatement pstmt = con.prepareStatement("INSERT INTO teams (name) VALUES (?)");
  6. pstmt.setString(1, teamName);
  7. pstmt.executeUpdate();
  8. } catch (SQLException e) {
  9. System.err.print("T." + e);
  10. }
  11. }
  12.  
  13. public Integer findByName(String teamName) throws SQLException {
  14. Connection con = Database.getConnection();
  15. try (Statement stmt = con.createStatement()) {
  16. ResultSet rs = stmt.executeQuery("SELECT id FROM teams WHERE name = '" + teamName + "'");
  17. Integer id = rs.next() ? rs.getInt("id") : null;
  18. rs.close();
  19. return id;
  20. }
  21. }
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29. PlayerController
  30. public void create(String playerName, int teamID) throws SQLException {
  31. Connection con = Database.getConnection();
  32. try {
  33. PreparedStatement pstmt = con.prepareStatement("INSERT INTO players (name, team_id) VALUES (?, ?)");
  34. pstmt.setString(1, playerName);
  35. pstmt.setInt(2, teamID);
  36. pstmt.executeUpdate();
  37. } catch (SQLException e) {
  38. System.err.print("P." + e);
  39. }
  40. }
  41.  
  42. /** Listeaza jucatorii unei echipe*/
  43. public void list(Integer teamID) throws SQLException {
  44. Connection con = Database.getConnection();
  45. try (Statement stmt = con.createStatement()) {
  46. ResultSet rs = stmt.executeQuery("SELECT name FROM players WHERE team_id = " + teamID);
  47. while(rs.next()) {
  48. String name = rs.getString("name");
  49. System.out.println(name);
  50. }
  51. }
  52. }
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60. Clasa Database: // Aici trebuie modificat cate ceva, ca nush cum e in oracleSQL
  61. public class Database {
  62. private static final String URL = "jdbc:mysql://localhost/football";
  63. private static final String USER = "root";
  64. private static final String PASSWORD = "";
  65. private static Connection connection = null;
  66. private Database() { }
  67.  
  68. public static Connection getConnection() throws SQLException {
  69. if (connection == null) {
  70. createConnection();
  71. connection.setAutoCommit(false);
  72. }
  73. return connection;
  74. }
  75. public static void createConnection() {
  76. try {
  77. Class.forName("com.mysql.jdbc.Driver");
  78. connection = DriverManager.getConnection(URL, USER, PASSWORD);
  79. } catch (ClassNotFoundException e) {
  80. System.err.print("ClassNotFoundException: " + e);
  81. } catch(SQLException e) {
  82. System.err.println("SQLException: " + e);
  83. }
  84. }
  85.  
  86. public static void closeConnection() {
  87. try {
  88. connection.close();
  89. } catch (SQLException e) {
  90. System.err.print("D: " + e);
  91. }
  92. }
  93.  
  94. public static void commit() {
  95. try {
  96. connection.commit();
  97. } catch (SQLException e) {
  98. System.err.print("D: " + e);
  99. }
  100. }
  101.  
  102. public static void rollback() {
  103. try {
  104. connection.rollback();
  105. } catch (SQLException e) {
  106. System.err.print("D: " + e);
  107. }
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement