Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.51 KB | None | 0 0
  1. package DatabaseAccesslayer;
  2.  
  3. import java.sql.*;
  4. import java.util.*;
  5.  
  6.  
  7. import connector.ConnectDb;
  8. import model.*;
  9.  
  10. public class DAL {
  11.  
  12. private String sqlString;
  13. private Student student;
  14. private String sPnr;
  15. private String sName;
  16. private String sAdress;
  17. private Course course;
  18. private String cCode;
  19. private String cName;
  20. private int cPoints;
  21.  
  22. // Find a specific student with sPnr********************************
  23. public Student findStudent(String sPnr) throws SQLException {
  24.  
  25. Connection con = ConnectDb.startConnection();
  26.  
  27. sqlString = "select * from student where spnr = '" + sPnr + "'";
  28. Statement stmt = con.createStatement();
  29. ResultSet rs = stmt.executeQuery(sqlString);
  30. rs.next();
  31. sPnr = rs.getString(1);
  32. sName = rs.getString(2);
  33. sAdress = rs.getString(3);
  34.  
  35. student = new Student(sPnr, sName, sAdress);
  36.  
  37. return student;
  38.  
  39. }
  40.  
  41. // Find a specific course with cCode ****************************
  42. public Course findCourse(String cCode) throws SQLException {
  43.  
  44. Connection con = ConnectDb.startConnection();
  45.  
  46. sqlString = "select * from course where ccode = '" + cCode + "'";
  47. Statement stmt = con.createStatement();
  48. ResultSet rs = stmt.executeQuery(sqlString);
  49. rs.next();
  50. cCode = rs.getString(1);
  51. cName = rs.getString(2);
  52. cPoints = rs.getInt(3);
  53.  
  54. course = new Course(cCode, cName, cPoints);
  55.  
  56. return course;
  57. }
  58.  
  59. // Add student
  60. public void addStudent(String sPnr, String sName, String sAdress) throws SQLException {
  61. Connection con = ConnectDb.startConnection();
  62.  
  63. Statement stmt = con.createStatement();
  64.  
  65. String sqlString = "insert into student values('" + sPnr + "','" + sName + "','" + sAdress + "')";
  66. stmt.executeUpdate(sqlString);
  67. stmt.close();
  68. }
  69.  
  70. // Add course
  71. public void addCourse(String cCode, String cName, int cPoints) throws SQLException {
  72. Connection con = ConnectDb.startConnection();
  73.  
  74. Statement stmt = con.createStatement();
  75.  
  76. String sqlString = "insert into course values('" + cCode + "','" + cName + "','" + cPoints + "')";
  77. stmt.executeUpdate(sqlString);
  78. stmt.close();
  79. }
  80.  
  81. public populateStudent() throws SQLException{
  82.  
  83. try{
  84. Connection con = ConnectDb.startConnection();
  85. Statement stmt = con.createStatement();
  86. String sqlString ="select * from student";
  87. ResultSet rs = stmt.executeQuery(sqlString);
  88. Default
  89.  
  90. ResultSetMetaData rsmd = rs.getMetaData();
  91. int columns = rsmd.getColumnCount();
  92.  
  93.  
  94. } catch(SQLException e) {
  95. e.printStackTrace();
  96. }
  97.  
  98.  
  99. }
  100.  
  101. }
  102.  
  103.  
  104.  
  105. package controller;
  106.  
  107. import java.sql.*;
  108. import model.*;
  109. import DatabaseAccesslayer.*;
  110.  
  111. public class Controller {
  112.  
  113. private Student student;
  114. private Course course;
  115. private DAL dal;
  116.  
  117.  
  118. public Controller(){
  119. dal = new DAL();
  120. }
  121.  
  122. public Student findStudent(String sPnr) throws SQLException{
  123. student = dal.findStudent(sPnr);
  124. return student;
  125. }
  126.  
  127. public void addStudent (String sPnr, String sName, String sAdress) throws SQLException{
  128. dal.addStudent(sPnr, sName, sAdress);
  129. }
  130.  
  131. public Course findCourse(String cCode) throws SQLException{
  132. course = dal.findCourse(cCode);
  133. return course;
  134. }
  135.  
  136. public void addCourse (String cCode, String cName, int cPoints) throws SQLException{
  137. dal.addCourse(cCode, cName, cPoints);
  138. }
  139.  
  140.  
  141.  
  142.  
  143.  
  144. }
  145.  
  146.  
  147. package connector;
  148.  
  149. import java.sql.*;
  150.  
  151. public class ConnectDb {
  152.  
  153. public static Connection startConnection() {
  154. Connection con = null;
  155. try {
  156. DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
  157. } catch (Exception e) {
  158. System.out.println("Kan inte database driver class: " + e);
  159. }
  160. try {
  161. con = DriverManager.getConnection(
  162. "jdbc:sqlserver://localhost:1433;" + "databaseName=Uppgift1;user=sa;password=kalle;");
  163. if (con == null)
  164. System.out.println("No driver found!");
  165. else
  166. System.out.println("Connection Established - SUCCESS!");
  167.  
  168. } catch (Exception e) {
  169. System.out.println("Kaaaaaaaan inte hitta database driver class: " + e);
  170. }
  171. return con;
  172.  
  173. }
  174.  
  175. }
  176.  
  177.  
  178. package view;
  179.  
  180. import java.awt.EventQueue;
  181.  
  182. import javax.swing.JFrame;
  183. import javax.swing.JTabbedPane;
  184. import javax.swing.JLayeredPane;
  185. import javax.swing.JLabel;
  186. import java.awt.Font;
  187. import javax.swing.JTextField;
  188. import javax.swing.table.DefaultTableModel;
  189.  
  190. import controller.Controller;
  191.  
  192. import javax.swing.JTable;
  193. import javax.swing.JScrollPane;
  194. import javax.swing.JComboBox;
  195. import javax.swing.DefaultComboBoxModel;
  196. import javax.swing.JButton;
  197. import javax.swing.SwingConstants;
  198. import java.awt.event.ActionListener;
  199. import java.sql.SQLException;
  200. import java.awt.event.ActionEvent;
  201.  
  202. public class DBStartView {
  203.  
  204. Controller controller = new Controller();
  205. private JFrame frameSchool;
  206. private JTextField textField_SName;
  207. private JTextField textField_SPnr;
  208. private JTextField textField_SAdress;
  209. private JTable table_Student;
  210. private JTable table_ThisStudent;
  211. private JTextField textField_CAdress;
  212. private JTextField textField_CPoints;
  213. private JTextField textField_CName;
  214. private JTextField textField_CCode;
  215. private JTable table_Course;
  216. private JTable table_ThisCourse;
  217. private JTextField textField_HNewName;
  218. private JTextField textField_HNewAdress;
  219. private JTextField textField_NewPnr;
  220. private JTextField textField_TotalPoints;
  221. private JTextField textField_HPoints;
  222.  
  223. /**
  224. * Launch the application.
  225. */
  226. public static void main(String[] args) {
  227. EventQueue.invokeLater(new Runnable() {
  228. public void run() {
  229. try {
  230. DBStartView window = new DBStartView();
  231. window.frameSchool.setVisible(true);
  232. } catch (Exception e) {
  233. e.printStackTrace();
  234. }
  235. }
  236. });
  237. }
  238.  
  239. /**
  240. * Create the application.
  241. */
  242. public DBStartView() {
  243. initialize();
  244. }
  245.  
  246. /**
  247. * Initialize the contents of the frame.
  248. */
  249. private void initialize() {
  250. frameSchool = new JFrame();
  251. frameSchool.setTitle("School");
  252. frameSchool.setBounds(100, 100, 818, 835);
  253. frameSchool.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  254. frameSchool.getContentPane().setLayout(null);
  255.  
  256. JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
  257. tabbedPane.setBounds(15, 30, 754, 696);
  258. frameSchool.getContentPane().add(tabbedPane);
  259.  
  260. JLayeredPane tab_Home = new JLayeredPane();
  261. tabbedPane.addTab("Home", null, tab_Home, null);
  262.  
  263. JLabel lblCreateNew = new JLabel("Create new ");
  264. lblCreateNew.setFont(new Font("Tahoma", Font.PLAIN, 20));
  265. lblCreateNew.setBounds(15, 15, 145, 42);
  266. tab_Home.add(lblCreateNew);
  267.  
  268. JLabel lblMessage = new JLabel("Message:");
  269. lblMessage.setBounds(15, 742, 754, 20);
  270. frameSchool.getContentPane().add(lblMessage);
  271.  
  272. JComboBox<String> comboBox_New = new JComboBox<String>();
  273. comboBox_New.setFont(new Font("Tahoma", Font.PLAIN, 20));
  274. comboBox_New.setModel(new DefaultComboBoxModel<String>(new String[] { "Student", "Course" }));
  275. comboBox_New.setBounds(170, 20, 120, 30);
  276. tab_Home.add(comboBox_New);
  277.  
  278. JLabel lblNewName = new JLabel("Name");
  279. lblNewName.setBounds(25, 100, 115, 20);
  280. tab_Home.add(lblNewName);
  281.  
  282. textField_HNewName = new JTextField();
  283. textField_HNewName.setBounds(145, 100, 146, 26);
  284. tab_Home.add(textField_HNewName);
  285. textField_HNewName.setColumns(10);
  286.  
  287. JLabel lblNewAdress = new JLabel("Adress");
  288. lblNewAdress.setBounds(25, 140, 115, 20);
  289. tab_Home.add(lblNewAdress);
  290.  
  291. textField_HNewAdress = new JTextField();
  292. textField_HNewAdress.setBounds(145, 140, 146, 26);
  293. tab_Home.add(textField_HNewAdress);
  294. textField_HNewAdress.setColumns(10);
  295.  
  296. JLabel lblNewPnr = new JLabel("Pnr");
  297. lblNewPnr.setBounds(25, 60, 115, 20);
  298. tab_Home.add(lblNewPnr);
  299.  
  300. textField_NewPnr = new JTextField();
  301. textField_NewPnr.setBounds(145, 60, 146, 26);
  302. tab_Home.add(textField_NewPnr);
  303. textField_NewPnr.setColumns(10);
  304.  
  305. JButton btnSave = new JButton("Save");
  306. btnSave.addActionListener(new ActionListener() {
  307. public void actionPerformed(ActionEvent e) {
  308.  
  309. String sPnr = textField_NewPnr.getText();
  310. String sName = textField_HNewName.getText();
  311. String sAdress = textField_HNewAdress.getText();
  312.  
  313. try {
  314. controller.addStudent(sPnr, sName, sAdress);
  315. lblMessage.setText("Wooohooo fucking hooo");
  316. } catch (SQLException e1) {
  317. e1.printStackTrace();
  318. lblMessage.setText("Student not added");
  319.  
  320. }
  321. }
  322. });
  323. btnSave.setBounds(170, 244, 120, 29);
  324. tab_Home.add(btnSave);
  325.  
  326. JLabel lblHPoints = new JLabel("Points");
  327. lblHPoints.setBounds(25, 180, 69, 20);
  328. tab_Home.add(lblHPoints);
  329.  
  330. textField_HPoints = new JTextField();
  331. textField_HPoints.setBounds(145, 180, 146, 26);
  332. tab_Home.add(textField_HPoints);
  333. textField_HPoints.setColumns(10);
  334.  
  335. JLayeredPane tab_Student = new JLayeredPane();
  336. tabbedPane.addTab("Student", null, tab_Student, null);
  337.  
  338. JLabel lblSPnr = new JLabel("Pnr");
  339. lblSPnr.setBounds(25, 60, 115, 20);
  340. tab_Student.add(lblSPnr);
  341.  
  342. JLabel lblSAdress = new JLabel("Adress");
  343. lblSAdress.setBounds(25, 140, 115, 20);
  344. tab_Student.add(lblSAdress);
  345.  
  346. JLabel lblSName = new JLabel("Name");
  347. lblSName.setBounds(25, 100, 115, 20);
  348. tab_Student.add(lblSName);
  349.  
  350. textField_SName = new JTextField();
  351. textField_SName.setColumns(10);
  352. textField_SName.setBounds(145, 100, 146, 26);
  353. tab_Student.add(textField_SName);
  354.  
  355. JLabel lblSInformation = new JLabel("Information Student");
  356. lblSInformation.setFont(new Font("Tahoma", Font.PLAIN, 20));
  357. lblSInformation.setBounds(15, 15, 199, 42);
  358. tab_Student.add(lblSInformation);
  359.  
  360. textField_SPnr = new JTextField();
  361. textField_SPnr.setBounds(145, 60, 146, 26);
  362. tab_Student.add(textField_SPnr);
  363. textField_SPnr.setColumns(10);
  364.  
  365. textField_SAdress = new JTextField();
  366. textField_SAdress.setBounds(145, 140, 146, 26);
  367. tab_Student.add(textField_SAdress);
  368. textField_SAdress.setColumns(10);
  369.  
  370. JScrollPane scrollPane_ThisStudent = new JScrollPane();
  371. scrollPane_ThisStudent.setBounds(15, 470, 276, 174);
  372. tab_Student.add(scrollPane_ThisStudent);
  373.  
  374. JLayeredPane tab_Course = new JLayeredPane();
  375. tabbedPane.addTab("Course", null, tab_Course, null);
  376.  
  377. JLabel lblCInformation = new JLabel("Information Course");
  378. lblCInformation.setFont(new Font("Tahoma", Font.PLAIN, 20));
  379. lblCInformation.setBounds(15, 15, 276, 42);
  380. tab_Course.add(lblCInformation);
  381.  
  382. JLabel lblCourseCode = new JLabel("Course Code");
  383. lblCourseCode.setBounds(25, 60, 115, 20);
  384. tab_Course.add(lblCourseCode);
  385.  
  386. JLabel lblCourseName = new JLabel("Course Name");
  387. lblCourseName.setBounds(25, 100, 115, 20);
  388. tab_Course.add(lblCourseName);
  389.  
  390. JLabel lblPoints = new JLabel("Points");
  391. lblPoints.setBounds(25, 180, 115, 20);
  392. tab_Course.add(lblPoints);
  393.  
  394. JLabel lblCAdress = new JLabel("Adress");
  395. lblCAdress.setBounds(25, 140, 115, 20);
  396. tab_Course.add(lblCAdress);
  397.  
  398. textField_CAdress = new JTextField();
  399. textField_CAdress.setBounds(145, 140, 146, 26);
  400. tab_Course.add(textField_CAdress);
  401. textField_CAdress.setColumns(10);
  402.  
  403. textField_CPoints = new JTextField();
  404. textField_CPoints.setText("");
  405. textField_CPoints.setBounds(145, 180, 146, 26);
  406. tab_Course.add(textField_CPoints);
  407. textField_CPoints.setColumns(10);
  408.  
  409. textField_CName = new JTextField();
  410. textField_CName.setText("");
  411. textField_CName.setBounds(145, 100, 146, 26);
  412. tab_Course.add(textField_CName);
  413. textField_CName.setColumns(10);
  414.  
  415. textField_CCode = new JTextField();
  416. textField_CCode.setText("");
  417. textField_CCode.setBounds(145, 60, 146, 26);
  418. tab_Course.add(textField_CCode);
  419. textField_CCode.setColumns(10);
  420.  
  421. table_ThisStudent = new JTable();
  422. scrollPane_ThisStudent.setViewportView(table_ThisStudent);
  423.  
  424. JScrollPane scrollPane_Student = new JScrollPane();
  425. scrollPane_Student.setBounds(315, 60, 410, 585);
  426. tab_Student.add(scrollPane_Student);
  427.  
  428. table_Student = new JTable();
  429. scrollPane_Student.setColumnHeaderView(table_Student);
  430.  
  431. JScrollPane scrollPane_ThisCourse = new JScrollPane();
  432. scrollPane_ThisCourse.setViewportView(table_ThisCourse);
  433.  
  434. scrollPane_ThisCourse.setBounds(15, 470, 276, 174);
  435. tab_Course.add(scrollPane_ThisCourse);
  436.  
  437. table_ThisCourse = new JTable();
  438. scrollPane_ThisCourse.setColumnHeaderView(table_ThisCourse);
  439.  
  440. JScrollPane scrollPane_Course = new JScrollPane();
  441. scrollPane_Course.setBounds(315, 60, 410, 585);
  442. tab_Course.add(scrollPane_Course);
  443.  
  444. table_Course = new JTable();
  445. scrollPane_Course.setColumnHeaderView(table_Course);
  446.  
  447. DefaultTableModel Dmt_Student = new DefaultTableModel();
  448. String colum_s[] = { "Pnr", "Name" };
  449. table_Student = new JTable(Dmt_Student);
  450. scrollPane_Student.setViewportView(table_Student);
  451. Dmt_Student.setColumnIdentifiers(colum_s);
  452.  
  453. DefaultTableModel Dmt_ThisStudent = new DefaultTableModel();
  454. String colum_ts[] = { "Code", "Name", "Grade" };
  455. table_ThisStudent = new JTable(Dmt_ThisStudent);
  456. scrollPane_ThisStudent.setViewportView(table_ThisStudent);
  457.  
  458. JComboBox<?> comboBox_sortThisStudent = new JComboBox();
  459. comboBox_sortThisStudent.setModel(new DefaultComboBoxModel(new String[] { "All", "Current", "Completed" }));
  460. comboBox_sortThisStudent.setBounds(145, 430, 146, 25);
  461. tab_Student.add(comboBox_sortThisStudent);
  462.  
  463. JLabel lblSSortBy = new JLabel("Sort by ");
  464. lblSSortBy.setHorizontalAlignment(SwingConstants.RIGHT);
  465. lblSSortBy.setBounds(60, 430, 80, 25);
  466. tab_Student.add(lblSSortBy);
  467.  
  468. JLabel lblSNumberOfCourses = new JLabel("Number of courses:");
  469. lblSNumberOfCourses.setBounds(25, 220, 266, 20);
  470. tab_Student.add(lblSNumberOfCourses);
  471.  
  472. JLabel lblTotalPoints = new JLabel("Total points ");
  473. lblTotalPoints.setBounds(25, 180, 115, 20);
  474. tab_Student.add(lblTotalPoints);
  475.  
  476. textField_TotalPoints = new JTextField();
  477. textField_TotalPoints.setBounds(145, 180, 146, 26);
  478. tab_Student.add(textField_TotalPoints);
  479. textField_TotalPoints.setColumns(10);
  480.  
  481. JLabel lblSGradeStatistics = new JLabel("Grade statistics (%)");
  482. lblSGradeStatistics.setBounds(25, 260, 150, 20);
  483. tab_Student.add(lblSGradeStatistics);
  484.  
  485. JLabel lblSGradeA = new JLabel("A");
  486. lblSGradeA.setHorizontalAlignment(SwingConstants.RIGHT);
  487. lblSGradeA.setBounds(222, 260, 69, 20);
  488. tab_Student.add(lblSGradeA);
  489.  
  490. JLabel lblSGradeB = new JLabel("B");
  491. lblSGradeB.setHorizontalAlignment(SwingConstants.RIGHT);
  492. lblSGradeB.setBounds(222, 285, 69, 20);
  493. tab_Student.add(lblSGradeB);
  494.  
  495. JLabel lblSGradeC = new JLabel("C");
  496. lblSGradeC.setHorizontalAlignment(SwingConstants.RIGHT);
  497. lblSGradeC.setBounds(222, 310, 69, 20);
  498. tab_Student.add(lblSGradeC);
  499.  
  500. JLabel lblSGradeD = new JLabel("D");
  501. lblSGradeD.setHorizontalAlignment(SwingConstants.RIGHT);
  502. lblSGradeD.setBounds(222, 335, 69, 20);
  503. tab_Student.add(lblSGradeD);
  504.  
  505. JLabel lblSGradeE = new JLabel("E");
  506. lblSGradeE.setHorizontalAlignment(SwingConstants.RIGHT);
  507. lblSGradeE.setBounds(222, 360, 69, 20);
  508. tab_Student.add(lblSGradeE);
  509.  
  510. JLabel lblSGradeU = new JLabel("U");
  511. lblSGradeU.setHorizontalAlignment(SwingConstants.RIGHT);
  512. lblSGradeU.setBounds(222, 385, 69, 20);
  513. tab_Student.add(lblSGradeU);
  514. Dmt_ThisStudent.setColumnIdentifiers(colum_ts);
  515.  
  516. DefaultTableModel Dmt_Course = new DefaultTableModel();
  517. String colum_c[] = { "Code", "Name" };
  518. table_Course = new JTable(Dmt_Course);
  519. scrollPane_Course.setViewportView(table_Course);
  520. Dmt_Course.setColumnIdentifiers(colum_c);
  521.  
  522. JButton btnSUpdate = new JButton("Update");
  523. btnSUpdate.setBounds(199, 24, 92, 29);
  524. tab_Student.add(btnSUpdate);
  525.  
  526. JButton btnCUpdate = new JButton("Update");
  527. btnCUpdate.setBounds(199, 24, 92, 29);
  528. tab_Course.add(btnCUpdate);
  529.  
  530. DefaultTableModel Dmt_ThisCourse = new DefaultTableModel();
  531. String colum_tc[] = { "Pnr", "Name", "Grade" };
  532. table_ThisCourse = new JTable(Dmt_ThisCourse);
  533. scrollPane_ThisCourse.setViewportView(table_ThisCourse);
  534. Dmt_ThisCourse.setColumnIdentifiers(colum_tc);
  535.  
  536. JComboBox<?> comboBox_sortThisCourse = new JComboBox();
  537. comboBox_sortThisCourse.setModel(new DefaultComboBoxModel(new String[] { "All", "Current", "Completed" }));
  538. comboBox_sortThisCourse.setBounds(145, 430, 146, 25);
  539. tab_Course.add(comboBox_sortThisCourse);
  540.  
  541. JLabel lblCSortBy = new JLabel("Sort by ");
  542. lblCSortBy.setHorizontalAlignment(SwingConstants.RIGHT);
  543. lblCSortBy.setBounds(60, 430, 80, 25);
  544. tab_Course.add(lblCSortBy);
  545.  
  546. JLabel lblCNumberOfStudents = new JLabel("Number of students:");
  547. lblCNumberOfStudents.setBounds(25, 220, 266, 20);
  548. tab_Course.add(lblCNumberOfStudents);
  549.  
  550. JLabel lblCGradeStatistics = new JLabel("Grade statistics (%)");
  551. lblCGradeStatistics.setBounds(25, 260, 150, 20);
  552. tab_Course.add(lblCGradeStatistics);
  553.  
  554. JLabel lblCGradeA = new JLabel("A");
  555. lblCGradeA.setHorizontalAlignment(SwingConstants.RIGHT);
  556. lblCGradeA.setBounds(222, 260, 69, 20);
  557. tab_Course.add(lblCGradeA);
  558.  
  559. JLabel lblCGradeB = new JLabel("B");
  560. lblCGradeB.setHorizontalAlignment(SwingConstants.RIGHT);
  561. lblCGradeB.setBounds(222, 285, 69, 20);
  562. tab_Course.add(lblCGradeB);
  563.  
  564. JLabel lblCGradeC = new JLabel("C");
  565. lblCGradeC.setHorizontalAlignment(SwingConstants.RIGHT);
  566. lblCGradeC.setBounds(222, 310, 69, 20);
  567. tab_Course.add(lblCGradeC);
  568.  
  569. JLabel lblCGradeD = new JLabel("D");
  570. lblCGradeD.setHorizontalAlignment(SwingConstants.RIGHT);
  571. lblCGradeD.setBounds(222, 335, 69, 20);
  572. tab_Course.add(lblCGradeD);
  573.  
  574. JLabel lblCGradeE = new JLabel("E");
  575. lblCGradeE.setHorizontalAlignment(SwingConstants.RIGHT);
  576. lblCGradeE.setBounds(222, 360, 69, 20);
  577. tab_Course.add(lblCGradeE);
  578.  
  579. JLabel lblCGradeU = new JLabel("U");
  580. lblCGradeU.setHorizontalAlignment(SwingConstants.RIGHT);
  581. lblCGradeU.setBounds(222, 385, 69, 20);
  582. tab_Course.add(lblCGradeU);
  583.  
  584. }
  585. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement