Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.19 KB | None | 0 0
  1. package zadatak22;
  2.  
  3. import java.awt.CardLayout;
  4. import java.awt.Dimension;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.MouseAdapter;
  8. import java.awt.event.MouseEvent;
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.PreparedStatement;
  12. import java.sql.ResultSet;
  13. import java.sql.SQLException;
  14. import java.sql.Statement;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17.  
  18. import javax.swing.*;
  19.  
  20.  
  21. public class Zadatak22 {
  22.  
  23. static {
  24. try {
  25. Class.forName("com.ibm.db2.jcc.DB2Driver");
  26. } catch (Exception e) {
  27. e.printStackTrace();
  28. }
  29. }
  30.  
  31. // Stampanje greske
  32. public static void printError (SQLException e){
  33. if (e.getErrorCode() == -911 || e.getErrorCode() == -913)
  34. System.out.println("Objekat je zakljucan od strane druge transakcije. Sacekajte.\n");
  35. else
  36. System.out.println("SQLCODE: " + e.getErrorCode() + "\nSQLSTATE: " + e.getSQLState() +
  37. "\nMessage: " + e.getMessage());
  38. }
  39.  
  40. public static void main(String[] args) {
  41.  
  42. final JFrame frame = new JFrame();
  43. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  44. frame.setTitle("zadatak-22");
  45.  
  46. final JPanel cards = new JPanel(new CardLayout());
  47.  
  48. final JPanel card1 = new JPanel();
  49. final JPanel c1panel1 = new JPanel();
  50. final JPanel c1panel2 = new JPanel();
  51. final JPanel c1panel3 = new JPanel();
  52. card1.setLayout(new BoxLayout(card1, BoxLayout.Y_AXIS));
  53.  
  54. final JPanel card2 = new JPanel();
  55. final JPanel c2panel1 = new JPanel();
  56. final JPanel c2panel2 = new JPanel();
  57. c2panel2.setLayout(new BoxLayout(c2panel2, BoxLayout.Y_AXIS));
  58. final JPanel c2panel21 = new JPanel();
  59. final JPanel c2panel22 = new JPanel();
  60. final JPanel c2panel23 = new JPanel();
  61. final JPanel c2panel24 = new JPanel();
  62. final JPanel prijave = new JPanel();
  63. prijave.setLayout(new BoxLayout(prijave, BoxLayout.Y_AXIS));
  64.  
  65. final JTextField c2bodoviPis = new JTextField();
  66. final JTextField c2bodoviUs = new JTextField();
  67. final JTextField c2ocena = new JTextField();
  68. final JLabel polje2 = new JLabel();
  69. final JLabel poljeIndeks = new JLabel();
  70. final JLabel labelBodoviPis = new JLabel("Bodovi sa pismenog dela");
  71. final JLabel labelBodoviUs = new JLabel("Bodovi sa usmenog dela");
  72. final JLabel labelOcena = new JLabel("Ocena");
  73. card2.setLayout(new BoxLayout(card2, BoxLayout.Y_AXIS));
  74.  
  75. final JButton dugmePrijave = new JButton("Prikazi prijave");
  76. final JButton dugmeIzadji = new JButton("Izadji");
  77. final JButton dugmePrekid = new JButton("Prekid");
  78. final JButton dugmeUnos = new JButton("Unos");
  79.  
  80. List<String> smerovi = new ArrayList<>();
  81. List<String> predmeti = new ArrayList<>();
  82. List<String> rokovi = new ArrayList<>();
  83.  
  84. final String url = "jdbc:db2://localhost:50001/VSTUD";
  85. final String username = "student";
  86. final String password = "abcdef";
  87. try{
  88. Connection con = DriverManager.getConnection(url, username, password);
  89.  
  90. // Izvlacenje smerova, predmeta i rokova iz baze
  91. Statement st = con.createStatement();
  92. String sql1 = "select naziv from smer";
  93. String sql2 = "select naziv from predmet";
  94. String sql3 = "select naziv from ispitni_rok";
  95.  
  96. ResultSet rs = st.executeQuery(sql1);
  97. while(rs.next()){
  98. smerovi.add(rs.getString(1).trim());
  99. }
  100. rs = st.executeQuery(sql2);
  101. while(rs.next()){
  102. predmeti.add(rs.getString(1).trim());
  103. }
  104. rs = st.executeQuery(sql3);
  105. while(rs.next()){
  106. rokovi.add(rs.getString(1).trim());
  107. }
  108.  
  109. rs.close();
  110. st.close();
  111. con.close();
  112. }
  113. catch (SQLException e){
  114. printError(e);
  115. }
  116. catch (Exception e) {
  117. e.printStackTrace();
  118. }
  119.  
  120. // Popunjavanje dropdown-a
  121. final JComboBox<Object> cbSmer = new JComboBox<Object>(smerovi.toArray());
  122. final JComboBox<Object> cbPredmet = new JComboBox<Object>(predmeti.toArray());
  123. final JComboBox<Object> cbIspitniRok = new JComboBox<Object>(rokovi.toArray());
  124.  
  125. //Postavljnje velicina komponenti
  126. Dimension dugmeVelicina = new Dimension(200, 25);
  127. cbSmer.setPreferredSize(dugmeVelicina);
  128. cbPredmet.setPreferredSize(dugmeVelicina);
  129. cbIspitniRok.setPreferredSize(dugmeVelicina);
  130. dugmePrijave.setPreferredSize(dugmeVelicina);
  131. dugmeIzadji.setPreferredSize(dugmeVelicina);
  132.  
  133. Dimension poljeVelicina = new Dimension(25, 25);
  134. c2bodoviPis.setPreferredSize(poljeVelicina);
  135. c2bodoviUs.setPreferredSize(poljeVelicina);
  136. c2ocena.setPreferredSize(poljeVelicina);
  137.  
  138. // Postavljanje dropdown-a na nedefinisano
  139. cbSmer.setSelectedIndex(-1);
  140. cbPredmet.setSelectedIndex(-1);
  141. cbIspitniRok.setSelectedIndex(-1);
  142.  
  143. //Dodajemo komponente na panel
  144. c1panel1.add(cbSmer);
  145. c1panel1.add(cbPredmet);
  146. c1panel1.add(cbIspitniRok);
  147. c1panel2.add(dugmePrijave);
  148. c1panel2.add(dugmeIzadji);
  149. c1panel3.add(prijave);
  150.  
  151. card1.add(c1panel1);
  152. card1.add(c1panel2);
  153. card1.add(c1panel3);
  154.  
  155. c2panel1.add(dugmePrekid);
  156. c2panel1.add(dugmeUnos);
  157. c2panel21.add(polje2);
  158. c2panel22.add(labelBodoviPis);
  159. c2panel22.add(c2bodoviPis);
  160. c2panel23.add(labelBodoviUs);
  161. c2panel23.add(c2bodoviUs);
  162. c2panel24.add(labelOcena);
  163. c2panel24.add(c2ocena);
  164.  
  165. c2panel2.add(c2panel21);
  166. c2panel2.add(c2panel22);
  167. c2panel2.add(c2panel23);
  168. c2panel2.add(c2panel24);
  169.  
  170. card2.add(c2panel1);
  171. card2.add(c2panel2);
  172.  
  173. cards.add(card1, "card-1");
  174. cards.add(card2, "card-2");
  175. final CardLayout cardLayout = (CardLayout) cards.getLayout();
  176.  
  177. // Event za dugme za listanje prijava
  178. dugmePrijave.addActionListener(new ActionListener() {
  179.  
  180. @Override
  181. public void actionPerformed(ActionEvent event) {
  182. try {
  183. Connection con = DriverManager.getConnection(url, username, password);
  184. int uspesno = 0;
  185.  
  186. // Izvlacenje vrednosti iz dropdown-a
  187. String smer = (String)cbSmer.getSelectedItem();
  188. String predmet = (String)cbPredmet.getSelectedItem();
  189. String irok = (String)cbIspitniRok.getSelectedItem();
  190.  
  191. String sql = null;
  192. PreparedStatement prep;
  193. ResultSet rs = null;
  194.  
  195. //Provera da li su izabrane pojedinacne stavkee
  196. if(cbSmer.getSelectedIndex() == -1){
  197. JOptionPane.showMessageDialog(cards, "Nije izabran smer!");
  198. cbSmer.requestFocus();
  199. }
  200. else if(cbPredmet.getSelectedIndex() == -1){
  201. JOptionPane.showMessageDialog(cards, "Nije izabran predmet!");
  202. cbPredmet.requestFocus();
  203. }
  204. else if(cbIspitniRok.getSelectedIndex() == -1){
  205. JOptionPane.showMessageDialog(cards, "Nije izabran ispitni rok!");
  206. cbIspitniRok.requestFocus();
  207. }
  208. else{ // Izabrane su sve stavke
  209. sql = "select d.ime, d.prezime, d.indeks, i.bodovi_pismenog, i.bodovi_usmenog, i.ocena " +
  210. "from ispit i join dosije d on d.indeks = i.indeks " +
  211. "join predmet p on p.id_predmeta = i.id_predmeta " +
  212. "join smer sm on sm.id_smera = d.id_smera " +
  213. "join ispitni_rok ir on ir.oznaka = i.oznaka_roka " +
  214. "and ir.godina = i.godina_roka " +
  215. "where sm.naziv = ? and p.naziv = ? and ir.naziv = ? " +
  216. "and i.status_prijave in ('o', 'p')";
  217.  
  218.  
  219. // Pripremanje upita
  220. prep = con.prepareStatement(sql);
  221. prep.setString(1, smer);
  222. prep.setString(2, predmet);
  223. prep.setString(3, irok);
  224.  
  225. // Hvatanje exceptiona u slucaju visekorisnickog okruzenja
  226. try {
  227. rs = prep.executeQuery();
  228. uspesno = 1;
  229. } catch (SQLException e) {
  230. if (e.getErrorCode() == -911 || e.getErrorCode() == -913) {
  231. JOptionPane.showMessageDialog(cards, "Objekat je zakljucan od strane druge" +
  232. " transakcije. Sacekajte.");
  233. con.rollback();
  234. System.out.println("Rollback");
  235. }
  236. else {
  237. System.out.println("SQLCODE: " + e.getErrorCode() + "\nSQLSTATE: " + e.getSQLState() +
  238. "\nMessage: " + e.getMessage());
  239. }
  240. }
  241. catch (Exception e) {
  242. e.printStackTrace();
  243. }
  244.  
  245. // Brisanje prethodnog sadrzaja liste prijava
  246. frame.setResizable(true);
  247. prijave.removeAll();
  248. prijave.revalidate();
  249. prijave.repaint();
  250.  
  251. if(uspesno == 1){
  252. int i = 1; // broj studenata u panelu zbog podesavanja velicine prozora
  253. while(rs.next()){
  254. final String ime = rs.getString(1).trim();
  255. final String prezime = rs.getString(2).trim();
  256. final String indeks = rs.getString(3);
  257. final String bodoviPis = rs.getString(4);
  258. final String bodoviUs = rs.getString(5);
  259. final String ocena = rs.getString(6);
  260. JLabel polje = new JLabel(ime + " " + prezime + " " + indeks);
  261. polje.addMouseListener(new MouseAdapter(){
  262. @Override
  263. public void mouseClicked(MouseEvent e){
  264. polje2.setText(ime + " " + prezime + "\n");
  265. poljeIndeks.setText(indeks);
  266. c2bodoviPis.setText(bodoviPis);
  267. c2bodoviUs.setText(bodoviUs);
  268. c2ocena.setText(ocena);
  269.  
  270. cardLayout.show(cards, "card-2");
  271. }
  272. });
  273. prijave.add(polje);
  274.  
  275. c1panel3.setSize(170, 15*i);
  276. frame.setSize(620, 15*i+150);
  277. i++;
  278. }
  279. }
  280.  
  281. rs.close();
  282. prep.close();
  283. con.close();
  284. }
  285. } catch (SQLException e) {
  286. printError(e);
  287. }
  288. catch (Exception e) {
  289. e.printStackTrace();
  290. }
  291. }
  292. });
  293.  
  294. dugmeIzadji.addActionListener(new ActionListener() {
  295. @Override
  296. public void actionPerformed(ActionEvent event) {
  297. System.exit(0);
  298. }
  299. });
  300. dugmePrekid.addActionListener(new ActionListener() {
  301. @Override
  302. public void actionPerformed(ActionEvent event) {
  303. cardLayout.show(cards, "card-1");
  304. }
  305. });
  306. dugmeUnos.addActionListener(new ActionListener() {
  307. @Override
  308. public void actionPerformed(ActionEvent event) {
  309. //Provera da li su popunjena pojedinacna polja
  310. if(c2bodoviPis.getText().equals("")){
  311. JOptionPane.showMessageDialog(cards, "Polje za bodove sa pismenog je prazno!");
  312. c2bodoviPis.requestFocus();
  313. }
  314. else if(c2bodoviUs.getText().equals("")){
  315. JOptionPane.showMessageDialog(cards, "Polje za bodove sa usmenog je prazno!");
  316. c2bodoviUs.requestFocus();
  317. }
  318. else if(c2ocena.getText().equals("")){
  319. JOptionPane.showMessageDialog(cards, "Polje za ocenu je prazno!");
  320. c2ocena.requestFocus();
  321. }
  322. else { // Sva tri polja su popounjena
  323. String sql = null;
  324. PreparedStatement prep;
  325. int uspesno = 0;
  326.  
  327. try {
  328. Connection con = DriverManager.getConnection(url, username, password);
  329. con.setAutoCommit(false);
  330.  
  331. sql = "update ispit i1 " +
  332. "set i1.bodovi_pismenog = ?, i1.bodovi_usmenog = ?, i1.bodovi = ?, i1.ocena = ? " +
  333. "where exists (select i2.indeks " +
  334. "from ispit i2 " +
  335. "join dosije d on d.indeks = i2.indeks " +
  336. "join predmet p on p.id_predmeta = i2.id_predmeta " +
  337. "join smer sm on sm.id_smera = d.id_smera " +
  338. "join ispitni_rok ir on ir.oznaka = i2.oznaka_roka " +
  339. "and ir.godina = i2.godina_roka " +
  340. "where sm.naziv = ? and p.naziv = ? and ir.naziv = ? and i2.indeks = ? " +
  341. "and i1.indeks = i2.indeks " +
  342. "and i1.godina_roka = i2.godina_roka " +
  343. "and i1.oznaka_roka = i2.oznaka_roka " +
  344. "and i1.id_predmeta = i2.id_predmeta)";
  345.  
  346. // Pripremanje upita
  347. prep = con.prepareStatement(sql);
  348. int bodPis = Integer.parseInt(c2bodoviPis.getText());
  349. int bodUs = Integer.parseInt(c2bodoviUs.getText());
  350. prep.setInt(1, bodPis);
  351. prep.setInt(2, bodUs);
  352. prep.setInt(3, bodPis + bodUs);
  353. prep.setInt(4, Integer.parseInt(c2ocena.getText()));
  354. prep.setString(5, (String)cbSmer.getSelectedItem());
  355. prep.setString(6, (String)cbPredmet.getSelectedItem());
  356. prep.setString(7, (String)cbIspitniRok.getSelectedItem());
  357. prep.setInt(8, Integer.parseInt(poljeIndeks.getText()));
  358.  
  359. // Hvatanje exceptiona u slucaju visekorisnickog okruzenja
  360. try {
  361. prep.executeUpdate();
  362. JOptionPane.showMessageDialog(cards, "Podaci obradjeni!");
  363. uspesno = 1;
  364. } catch (SQLException e) {
  365. if (e.getErrorCode() == -911 || e.getErrorCode() == -913) {
  366. JOptionPane.showMessageDialog(cards, "Objekat je zakljucan od strane druge" +
  367. " transakcije. Sacekajte.");
  368. con.rollback();
  369. System.out.println("Rollback");
  370. }
  371. else {
  372. System.out.println("SQLCODE: " + e.getErrorCode() + "\nSQLSTATE: " + e.getSQLState() +
  373. "\nMessage: " + e.getMessage());
  374. }
  375. }
  376. catch (Exception e) {
  377. e.printStackTrace();
  378. }
  379.  
  380. // Commit samo ako je uspeo upit
  381. if (uspesno == 1) con.commit();
  382. prep.close();
  383. con.close();
  384.  
  385. } catch (SQLException e) {
  386. printError(e);
  387. }
  388. catch (Exception e) {
  389. e.printStackTrace();
  390. }
  391. }
  392. }
  393. });
  394.  
  395. frame.add(cards);
  396. frame.pack();
  397. frame.setVisible(true);
  398. frame.setLocationRelativeTo(null);
  399. }
  400.  
  401. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement