Advertisement
Guest User

Untitled

a guest
May 1st, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.93 KB | None | 0 0
  1.     public static DataSource getMySQLDataSource() { // Funkar denna som connect
  2.         MysqlDataSource mysqlDS = null;
  3.  
  4.         try {
  5.             mysqlDS = new MysqlDataSource();
  6.             mysqlDS.setURL("jdbc:mysql://localhost:3306/mydb");
  7.             mysqlDS.setUser("root");
  8.             mysqlDS.setPassword("");
  9.  
  10.         } catch (Exception e) {
  11.             e.printStackTrace();
  12.         }
  13.         return mysqlDS;
  14.     }
  15.  
  16.     public Connection getConnection() throws SQLException {
  17.         Connection connection = null;
  18.  
  19.         // Get the MySqlDataSource
  20.  
  21.         DataSource dataSource = getMySQLDataSource();
  22.  
  23.         // Get connection from the database
  24.  
  25.         connection = dataSource.getConnection();
  26.  
  27.         return connection;
  28.  
  29.     }
  30.  
  31.     private int getUserID(int inputUserID) {
  32.  
  33.         PreparedStatement prstmt = null;
  34.         int userID = 0;
  35.         Connection connection = null;
  36.  
  37.         try {
  38.             connection = getConnection();
  39.             // Execute query
  40.  
  41.             // egentligen ska prepareStatement innehålla "GET_USERNAME
  42.  
  43.             String sql = "SELECT userName FROM USER WHERE userID = ?";
  44.             prstmt = connection.prepareStatement(sql);
  45.  
  46.             prstmt.setInt(1, inputUserID);
  47.  
  48.             ResultSet rs = prstmt.executeQuery();
  49.  
  50.             while (rs.next()) {
  51.  
  52.                 // Retrieve by column name
  53.  
  54.                 userID = rs.getInt("userID");
  55.  
  56.                 // Display values
  57.  
  58.                 System.out.println("userID" + userID);
  59.  
  60.             }
  61.             rs.close();
  62.         } catch (SQLException se) {
  63.             se.printStackTrace();
  64.         } catch (Exception e) {
  65.             e.printStackTrace();
  66.         }
  67.  
  68.         finally {
  69.             // Finally block used to close resources
  70.                 closePrstmt(prstmt);
  71.                 closeConnection(connection);
  72.         }
  73.         return userID;
  74.     }
  75.    
  76.     public void closePrstmt(PreparedStatement prstmt){
  77.         try {
  78.             if (prstmt != null) {
  79.                 prstmt.close();
  80.             }
  81.         } catch (SQLException sqlException) {
  82.             sqlException.printStackTrace();
  83.         }
  84.  
  85.     }
  86.    
  87.     public void closeConnection(Connection connection){
  88.         try {
  89.             if (connection != null) {
  90.                 connection.close();
  91.             }
  92.         } catch (SQLException sqlException) {
  93.             sqlException.printStackTrace();
  94.         }
  95.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement