Advertisement
Guest User

Untitled

a guest
Nov 16th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.84 KB | None | 0 0
  1. package exercise2Lib_8140058;
  2.  
  3. import java.net.URL;
  4. import java.io.*;
  5. import java.sql.*;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8.  
  9. public class DB_EX2_8140058 {
  10.  
  11.     private String errorMessages= "";
  12.  
  13.     private Connection con = null;
  14.  
  15.     private PreparedStatement stmt = null;
  16.  
  17.     private String theQuery = "SELECT * FROM emploee_8xxxxxx WHERE salary >= ? AND salary <= ?;";
  18.  
  19.     private ResultSet rs = null;
  20.  
  21.  
  22.   /**
  23.    * Provides a connection with the Database Server. Initializes JDBC driver
  24.    * for MySQL. Establishes a connection with the Database Server.
  25.    *
  26.    * @throws SQLException
  27.    *             (with the appropriate message) if any driver or connection
  28.    *             error occured.
  29.    */
  30.  
  31.     public void open() throws SQLException{
  32.         //Load the JDBC driver
  33.         try {
  34.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  35.         } catch (Exception e1) {
  36.             errorMessages = "MySQL Driver error: <br>" +e1.getMessage();
  37.             throw new SQLException(errorMessages);
  38.         }
  39.         //Register driver and establish the connection
  40.         try {
  41.             con = DriverManager.getConnection(
  42.                 "jdbc:mysql://195.251.249.131:3306/ismgroup25","ismgroup25","zdsp$4");
  43.         } catch (Exception e2) {
  44.             errorMessages = "Could not establish connection with the Database Server: <br>"
  45.                 +e2.getMessage();
  46.             con = null;
  47.             throw new SQLException(errorMessages);
  48.         }
  49.     }
  50.  
  51.     /**
  52.      * Ends the connection with the database Server. Closes all Statements and
  53.      * ResultSets. Finally, closes the connection with the Database Server.
  54.      *
  55.      * @throws SQLException
  56.      *             (with the appropriate message) if any error occured.
  57.      */
  58.  
  59.     public void close() throws SQLException {
  60.  
  61.         try {
  62.  
  63.             if (stmt != null)
  64.                 stmt.close();
  65.  
  66.             if (rs != null)
  67.                 rs.close();
  68.  
  69.             if (con != null)
  70.                 con.close();
  71.  
  72.         } catch (Exception e3) {
  73.             errorMessages = "Could not close connection with the Database Server: <br>"
  74.                 + e3.getMessage();
  75.             throw new SQLException(errorMessages);
  76.         }
  77.     }
  78.  
  79.     /**
  80.      * Checks if from and to are valide.
  81.      *
  82.      *@return list, with Emploee objects
  83.      */
  84.     public List<Emploee> findEmploeesBySalaryRange(double from, double to) throws Exception {
  85.  
  86.         if (con == null) {
  87.             errorMessages = "You must establish a connection first!";
  88.         } else {
  89.  
  90.             List<Emploee> emploeList = new ArrayList<Emploee>();
  91.  
  92.             try {
  93.                 stmt = con.prepareStatement(theQuery);
  94.                 //replacing the first ? with from and the second ? with to
  95.                 stmt.setDouble(1, from);
  96.                 stmt.setDouble(2, to);
  97.                 //execute query
  98.                 rs = stmt.executeQuery();
  99.  
  100.                 //Add new Emploees
  101.                 while (rs.next()) {
  102.  
  103.  
  104.                     emploeList.add(new Emploee());
  105.  
  106.                 }
  107.  
  108.                 return emploeList;
  109.  
  110.             } catch (Exception e4) {
  111.                 errorMessages = "Error while executing SELECT query: <br>" + e4.getMessage();
  112.                 throw new SQLException(errorMessages);
  113.             }
  114.         }
  115.     }
  116. }
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123. COMPILATION ERROR:
  124. C:\Users\JIM\Desktop\exercise2Lib_8140058\DB_EX2_8140058.java:84: error: cannot find symbol
  125.     public List<Emploee> findEmploeesBySalaryRange(double from, double to) throws Exception {
  126.                 ^
  127.   symbol:   class Emploee
  128.   location: class DB_EX2_8140058
  129. C:\Users\JIM\Desktop\exercise2Lib_8140058\DB_EX2_8140058.java:90: error: cannot find symbol
  130.             List<Emploee> emploeList = new ArrayList<Emploee>();
  131.                  ^
  132.   symbol:   class Emploee
  133.   location: class DB_EX2_8140058
  134. C:\Users\JIM\Desktop\exercise2Lib_8140058\DB_EX2_8140058.java:90: error: cannot find symbol
  135.             List<Emploee> emploeList = new ArrayList<Emploee>();
  136.                                                      ^
  137.   symbol:   class Emploee
  138.   location: class DB_EX2_8140058
  139. C:\Users\JIM\Desktop\exercise2Lib_8140058\DB_EX2_8140058.java:104: error: cannot find symbol
  140.                     emploeList.add(new Emploee());
  141.                                        ^
  142.   symbol:   class Emploee
  143.   location: class DB_EX2_8140058
  144. 4 errors
  145.  
  146. Tool completed with exit code 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement