Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.75 KB | None | 0 0
  1. package com.fortech.officetime;
  2.  
  3. import java.sql.*;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import javax.ejb.Stateless;
  7. import com.microsoft.sqlserver.jdbc.*;
  8.  
  9. /**
  10.  * Controller class for the Access Control database. Provides the connection to
  11.  * the SQL Server database of the Access Card System using a
  12.  * SQLServerConnectionPoolDataSource.
  13.  *
  14.  * @author david.rus
  15.  *
  16.  */
  17. @Stateless
  18. public class AccessControlDbConnector {
  19.  
  20.     /**
  21.      * Method used to establish a connection to the database
  22.      *
  23.      * @throws SQLException
  24.      */
  25.     public ResultSet connectToDb(String query) {
  26.  
  27.         // Declare the JDBC objects.
  28.         Connection con = null;
  29.         ResultSet result = null;
  30.         Statement stmt = null;
  31.  
  32.         try {
  33.             // Establish the connection.
  34.             System.out.println("Establishing the connection...");
  35.             SQLServerConnectionPoolDataSource ds = new SQLServerConnectionPoolDataSource();
  36.             ds.setUser(Props.DB_USERNAME);
  37.             ds.setPassword(Props.DB_PASSWORD);
  38.             ds.setServerName(Props.DB_SERVERNAME);
  39.             ds.setDatabaseName(Props.DB_DATABASENAME);
  40.             ds.setPortNumber(Props.DB_PORTNUMBER);
  41.             con = ds.getConnection();
  42.             System.out.println("Connection established");
  43.             System.out.println();
  44.             stmt = con.createStatement();
  45.             result = stmt.executeQuery(query);
  46.         }
  47.  
  48.         // Handle any errors that may have occurred.
  49.         catch (Exception e) {
  50.             e.printStackTrace();
  51.         }
  52.  
  53.         finally {
  54.             if (stmt != null)
  55.                 try {
  56.                     stmt.close();
  57.                 } catch (Exception e) {
  58.                 }
  59.             if (con != null)
  60.                 try {
  61.                     con.close();
  62.                 } catch (Exception e) {
  63.                 }
  64.         }
  65.         return result;
  66.     }
  67.  
  68.     /**
  69.      * Method used to retrieve the employees
  70.      * @return a String with the employees
  71.      */
  72.     public String getEmployees() {
  73.  
  74.         List<String> firstNameList = new ArrayList<String>();
  75.         List<String> lastNameList = new ArrayList<String>();
  76.         String query = "SELECT TOP (1000) [iEmployeeNum]\r\n" + "      ,[tLastName]\r\n"
  77.                 + "      ,[tFirstName]\r\n" + "  FROM [axtraxdb].[dbo].[tblEmployees]";
  78.        
  79.         AccessControlDbConnector connect = new AccessControlDbConnector();
  80.         ResultSet result = connect.connectToDb(query);
  81.         // Iterate through the data in the result set and display it.
  82.         try {
  83.             while (result.next()) {
  84.                 firstNameList.add(result.getString("tFirstName"));
  85.                 lastNameList.add(result.getString("tLastName"));
  86.             }
  87.         } catch (SQLException e) {
  88.             e.printStackTrace();
  89.         } finally {
  90.             if (result != null)
  91.                 try {
  92.                     result.close();
  93.                 } catch (Exception e) {
  94.                 }
  95.             }
  96.         StringBuffer sb = new StringBuffer();
  97.         for (int i = 0; i < firstNameList.size(); i++) {
  98.             sb.append("Name: " + firstNameList.get(i) + " " + lastNameList.get(i));
  99.             sb.append(System.getProperty("line.separator"));
  100.         }
  101.         String str = sb.toString();
  102.         return str;
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement