Guest User

Untitled

a guest
Dec 1st, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. package model;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.Vector;
  9.  
  10. import javax.naming.NamingException;
  11. import javax.servlet.http.HttpServletRequest;
  12. import javax.sql.RowSet;
  13. import javax.sql.rowset.CachedRowSet;
  14. import data.DegreeData;
  15.  
  16. import com.sun.rowset.CachedRowSetImpl;
  17.  
  18. import db.DbException;
  19.  
  20.  
  21.  
  22. public class StudentModel {
  23. private static String server_info = "jdbc:postgresql://localhost/postgres";
  24. private static String server_user = "postgres";
  25. private static String server_pass = "1111";
  26. private static String countries = "SELECT id, myname FROM countries ORDER BY id";
  27. private static String location = "SELECT * FROM schoolfrom ORDER BY id";
  28. private static String schools = "SELECT school FROM school_schoolfrom WHERE sfrom = ?";
  29. private static String schoolNames = "SELECT myname FROM schools WHERE id = ?";
  30.  
  31. public static RowSet getAllCountries() throws Exception{
  32. try{
  33.  
  34. Class.forName("org.postgresql.Driver");
  35.  
  36. // Open a connection to the database using DriverManager
  37. Connection conn = DriverManager.getConnection(
  38. server_info,server_user, server_pass);
  39. PreparedStatement pStmt = conn.prepareStatement(countries);
  40. ResultSet allCountires = pStmt.executeQuery();
  41.  
  42. CachedRowSet crsStudents = new CachedRowSetImpl();
  43. crsStudents.populate(allCountires);
  44.  
  45. allCountires.close();
  46. pStmt.close();
  47. conn.close();
  48. return crsStudents;
  49. } catch (SQLException ex) {
  50. throw new Exception(ex);
  51. }
  52.  
  53. }
  54.  
  55. public static RowSet getAllLocations() throws Exception{
  56. try{
  57.  
  58. Class.forName("org.postgresql.Driver");
  59.  
  60. // Open a connection to the database using DriverManager
  61. Connection conn = DriverManager.getConnection(
  62. server_info,server_user, server_pass);
  63. PreparedStatement pStmt = conn.prepareStatement(location);
  64. ResultSet allCountires = pStmt.executeQuery();
  65.  
  66. CachedRowSet location = new CachedRowSetImpl();
  67. location.populate(allCountires);
  68.  
  69. allCountires.close();
  70. pStmt.close();
  71. conn.close();
  72. return location;
  73. } catch (SQLException ex) {
  74. throw new Exception(ex);
  75. }
  76. }
  77.  
  78. public static Vector<String> getSchools(DegreeData degreeData) throws Exception{
  79. try{
  80.  
  81. Vector<String> school = new Vector<String>();
  82. Vector<String> schoolname = new Vector<String>();
  83.  
  84. Class.forName("org.postgresql.Driver");
  85. // Open a connection to the database using DriverManager
  86. Connection conn = DriverManager.getConnection(
  87. server_info,server_user, server_pass);
  88.  
  89. String locationID = degreeData.getLocationID();
  90. System.err.println("locationID is:"+ locationID);
  91.  
  92. PreparedStatement pStmt = conn.prepareStatement(schools);
  93. pStmt.setString(1,locationID);
  94. ResultSet rs = pStmt.executeQuery();
  95. if(rs.next()){
  96. school.add(rs.getString(1));
  97. }
  98.  
  99. pStmt = conn.prepareStatement(schoolNames);
  100. for(int i = 0; i< school.size(); i++){
  101. pStmt.setString(1, school.get(i));
  102. rs = pStmt.executeQuery();
  103. if(rs.next()){
  104. schoolname.add(rs.getString(1));
  105. System.out.println(rs.getString(1));
  106. }
  107. }
  108.  
  109. pStmt.close();
  110. conn.close();
  111. return schoolname;
  112. } catch (SQLException ex) {
  113. throw new Exception(ex);
  114. }
  115. }
  116.  
  117.  
  118. }
Add Comment
Please, Sign In to add comment