Guest User

SpelMapper.java

a guest
Mar 17th, 2016
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.90 KB | None | 0 0
  1. package persistentie;
  2.  
  3. // Imports
  4. import java.sql.*;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7.  
  8. public class SpelMapper {
  9.     // Driver en DB URL
  10.     static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
  11.     static final String DB_URL = "jdbc:mysql://85.10.205.173:3306/doolhofbet";
  12.    
  13.     // DB inlog gegevens
  14.     static final String USER = "doolhofp1g25";
  15.     static final String PASS = "mp6few%";
  16.    
  17.     public List<String> geefSpelers(String spelNaam) {
  18.         List<String> spelers = new ArrayList<String>();
  19.         Connection conn = null;
  20.         Statement stmt = null;    
  21.         int idSpel = 0;
  22.  
  23.         try {
  24.             // Driver registreren
  25.             Class.forName(JDBC_DRIVER);
  26.  
  27.             // Connectie openen
  28.             conn = DriverManager.getConnection(DB_URL,USER,PASS);
  29.                
  30.             // Als de invoer een getal is wordt deze gebruikt als ID, zo niet wordt het het ID opgevraagd a.d.h.v. de spelnaam
  31.             try {
  32.               idSpel = Integer.parseInt(spelNaam);  
  33.             } catch(NumberFormatException nfe) {  
  34.                 // Query uitvoeren
  35.                 stmt = conn.createStatement();
  36.                 String sql = "SELECT idSpel FROM Spel WHERE spelNaam = '"+ spelNaam +"';";
  37.  
  38.  
  39.                 // Data in Result Set steken
  40.                 ResultSet rs = stmt.executeQuery(sql);
  41.                 while(rs.next()){
  42.                     // Binnenhalen via kolom
  43.                     idSpel = rs.getInt("idSpel");  
  44.                 }
  45.                 // Sluiten
  46.                 rs.close();
  47.                 stmt.close();
  48.             } finally {
  49.                 stmt = conn.createStatement();
  50.                 String sql = "SELECT spelerNaam FROM Speler WHERE idSpel = "+ idSpel +";";
  51.  
  52.                 ResultSet rs = stmt.executeQuery(sql);
  53.                 while(rs.next()){
  54.                     String naam = rs.getString("spelerNaam");
  55.                     spelers.add(naam);
  56.  
  57.                 }
  58.                 rs.close();
  59.                 stmt.close();
  60.  
  61.                 // Connectie sluiten
  62.                 conn.close();
  63.             }          
  64.            
  65.            
  66.         }catch(SQLException se){
  67.             // JDBC errors
  68.             se.printStackTrace();
  69.         }catch(Exception e){
  70.             // Class.forName errors
  71.             e.printStackTrace();
  72.         }finally{
  73.             try{
  74.                 if(stmt!=null)
  75.                     stmt.close();
  76.             }catch(SQLException se2){
  77.             }
  78.             try{
  79.                 if(conn!=null)
  80.                     conn.close();
  81.             }catch(SQLException se){
  82.                 se.printStackTrace();
  83.             }
  84.         }
  85.         return spelers;
  86.     }
  87.    
  88.     public List<String> geefSpelLijst() {
  89.         Connection conn = null;
  90.         Statement stmt = null;    
  91.         List<String> spellen = new ArrayList<String>();
  92.  
  93.         try {
  94.             Class.forName(JDBC_DRIVER);
  95.  
  96.             conn = DriverManager.getConnection(DB_URL,USER,PASS);
  97.  
  98.             stmt = conn.createStatement();
  99.             String sql;
  100.             sql = "SELECT spelNaam FROM Spel;";
  101.             ResultSet rs = stmt.executeQuery(sql);
  102.  
  103.             while(rs.next()){
  104.                 String spelNaam = rs.getString("spelNaam");
  105.                 spellen.add(spelNaam);
  106.             }
  107.             rs.close();
  108.             stmt.close();
  109.             conn.close();
  110.         }catch(SQLException se){
  111.             se.printStackTrace();
  112.         }catch(Exception e){
  113.             e.printStackTrace();
  114.         }finally{
  115.             try{
  116.                 if(stmt!=null)
  117.                     stmt.close();
  118.             }catch(SQLException se2){
  119.             }
  120.             try{
  121.                 if(conn!=null)
  122.                     conn.close();
  123.             }catch(SQLException se){
  124.                 se.printStackTrace();
  125.             }
  126.         }
  127.         return spellen;
  128.     }
  129. }
Add Comment
Please, Sign In to add comment