Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.39 KB | None | 0 0
  1. public ArrayList<String> getCourseStudents(String course) throws ClassNotFoundException{
  2.         String student = null;
  3.         Class.forName("org.sqlite.JDBC");
  4.         String sql = null;
  5.         Connection connection = null;
  6.         Statement statement = null;
  7.         ResultSet rs = null;
  8.         ArrayList<String> retrieveStudent = new ArrayList<String>();
  9.        
  10.         try {
  11.             // create a database connection
  12.             connection = DriverManager.getConnection("jdbc:sqlite:sample.db");
  13.             statement = connection.createStatement();
  14.             statement.setQueryTimeout(30); // set timeout to 30 sec.
  15.            
  16.        rs = statement.executeQuery("SELECT DISTINCT student FROM endgrade WHERE course = '"+ course +"'");
  17.        
  18.        while(rs.next()){
  19.        student = rs.getString("student");
  20.        
  21.        retrieveStudent.add(student);
  22.        
  23.        }
  24.        
  25.         }
  26.         catch (SQLException e) {
  27.             // if the error message is "out of memory",
  28.             // it probably means no database file is found
  29.             System.err.println(e.getMessage());
  30.            
  31.         } finally {
  32.             try {
  33.                 if (connection != null)
  34.                     connection.close();
  35.             } catch (SQLException e) {
  36.                 // connection close failed.
  37.                 System.err.println(e);
  38.             }
  39.             }
  40.        
  41.         return retrieveStudent;
  42.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement