Guest User

Untitled

a guest
Nov 24th, 2016
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.44 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.List;
  3. import java.util.ArrayList;
  4.  
  5. public class Professor {
  6.    
  7.     private Connection conn= null;
  8.     private ResultSet rs1, rs2;
  9.     private int profid ;
  10.     private int phone;
  11.     private String name;
  12.     private String surname;
  13.     private String expertise;
  14.     private String selectquery1="SELECT profid FROM professor WHERE username =? AND password =?";
  15.     private String selectqueryABC = "SELECT* FROM COURSE WHERE profid=?";
  16.     private  PreparedStatement stmt1;
  17.    
  18.    
  19.      public static void main (String[] args) {
  20.        
  21.         try {
  22.             Professor p1 = new Professor();
  23.         p1.open();
  24.         int iddd = p1.getID("VasilisPap", "frontistirio123");
  25.         p1.getCourses(iddd);
  26.     }catch(SQLException e) {
  27.         System.out.println(e);
  28.     } catch (Exception e) {
  29.         // TODO Auto-generated catch block
  30.         e.printStackTrace();
  31.     }
  32.     }
  33.     public void  open() throws SQLException{
  34.        
  35.         try{
  36.             String driver = "com.mysql.jdbc.Driver";
  37.             String url = "jdbc:mysql://195.251.249.131:3306/ismgroup25";
  38.             String username = "ismgroup25";
  39.             String password = "zdsp$4";
  40.             Class.forName(driver);
  41.             conn = DriverManager.getConnection(url,username,password);               
  42.            
  43.            
  44.         } catch(Exception e) {
  45.             throw new SQLException("No proper connection " + e);
  46.         }
  47.     }
  48.        
  49.        
  50.    
  51.     public void close() throws SQLException {
  52.         try {
  53.         conn.close();
  54.         } catch(Exception e) {
  55.             throw new SQLException("Something went wrong " + e) ;
  56.             }
  57.      }
  58.    
  59.     public int getID (String username, String password) throws Exception {
  60.    
  61.     stmt1 = conn.prepareStatement(selectquery1);
  62.     stmt1.setString(1, username);
  63.     stmt1.setString(2, password);
  64.     rs1 = stmt1.executeQuery();
  65.     if(rs1.next()) {
  66.     profid = rs1.getInt("profid");
  67.     }
  68.     return profid;
  69.     }
  70.    
  71.     public List<Course> getCourses(int profid) throws Exception {
  72.         try {
  73.         stmt1 = conn.prepareStatement(selectqueryABC);
  74.         stmt1.setInt(1, profid);
  75.         rs1 = stmt1.executeQuery();
  76.         List<Course> courses = new ArrayList<Course>();
  77.         while(rs1.next()) {
  78.             String classN = rs1.getString("class");
  79.             int cid = rs1.getInt("cid");
  80.             String name = rs1.getString("name");
  81.              profid = rs1.getInt("profid");
  82.              System.out.println(profid);
  83.             Course c1 = new Course(classN,cid,name,profid);
  84.             System.out.println(c1.getName());
  85.             courses.add(c1);
  86.            
  87.            
  88.         }
  89.         System.out.println(courses.get(3).getProfid());
  90.         return courses;
  91.        
  92.        
  93.     } catch(Exception e) {
  94.         throw new SQLException("Couldn't create list" + e);
  95.     }
  96.   }
  97. }
Add Comment
Please, Sign In to add comment