Advertisement
Goati_

pd

Jul 27th, 2018
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.32 KB | None | 0 0
  1. //QUESTO E' UN BEL MAIN VUOTO
  2.  
  3. package megamarathon2;
  4.  
  5. public class MegaMarathon2 {
  6.  
  7. public static void main(String[] args) {
  8.  
  9. }
  10. }
  11.  
  12.  
  13. __________________________________________________________________________________________
  14. //QUESTA è LA CLASSE DEL FRAME, MegaFrame
  15. package megamarathon2;
  16.  
  17. import javax.swing.JTable;
  18. import javax.swing.table.JTableHeader;
  19. import net.proteanit.sql.DbUtils;
  20.  
  21. /*
  22. * To change this license header, choose License Headers in Project Properties.
  23. * To change this template file, choose Tools | Templates
  24. * and open the template in the editor.
  25. */
  26. /**
  27. *
  28. * @author lemon
  29. */
  30. public class MegaFrame extends javax.swing.JFrame {
  31.  
  32. MegaModel model;
  33.  
  34. MegaDAO dao = new MegaDAO();
  35.  
  36. /**
  37. * Creates new form MegaFrame
  38. */
  39. public MegaFrame() {
  40. initComponents();
  41. model = new MegaModel();
  42. model.readAll();
  43. jTable1.setModel(model);
  44. JTableHeader header = jTable1.getTableHeader();
  45. header.addMouseListener(new TableHeaderMouseListener(jTable1));
  46.  
  47. }
  48.  
  49. /**
  50. * This method is called from within the constructor to initialize the form.
  51. * WARNING: Do NOT modify this code. The content of this method is always
  52. * regenerated by the Form Editor.
  53. */
  54. @SuppressWarnings("unchecked")
  55. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  56. private void initComponents() {
  57.  
  58. jScrollPane1 = new javax.swing.JScrollPane();
  59. jTable1 = new javax.swing.JTable();
  60.  
  61. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  62.  
  63. jTable1.setModel(new javax.swing.table.DefaultTableModel(
  64. new Object [][] {
  65. {null, null, null, null},
  66. {null, null, null, null},
  67. {null, null, null, null},
  68. {null, null, null, null}
  69. },
  70. new String [] {
  71. "Title 1", "Title 2", "Title 3", "Title 4"
  72. }
  73. ));
  74. jScrollPane1.setViewportView(jTable1);
  75.  
  76. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  77. getContentPane().setLayout(layout);
  78. layout.setHorizontalGroup(
  79. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  80. .addGroup(layout.createSequentialGroup()
  81. .addGap(170, 170, 170)
  82. .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
  83. .addContainerGap(241, Short.MAX_VALUE))
  84. );
  85. layout.setVerticalGroup(
  86. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  87. .addGroup(layout.createSequentialGroup()
  88. .addGap(123, 123, 123)
  89. .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
  90. .addContainerGap(151, Short.MAX_VALUE))
  91. );
  92.  
  93. pack();
  94. }// </editor-fold>
  95.  
  96. /**
  97. * @param args the command line arguments
  98. */
  99. public static void main(String args[]) {
  100. /* Set the Nimbus look and feel */
  101. //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  102. /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  103. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  104. */
  105. try {
  106. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  107. if ("Nimbus".equals(info.getName())) {
  108. javax.swing.UIManager.setLookAndFeel(info.getClassName());
  109. break;
  110. }
  111. }
  112. } catch (ClassNotFoundException ex) {
  113. java.util.logging.Logger.getLogger(MegaFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  114. } catch (InstantiationException ex) {
  115. java.util.logging.Logger.getLogger(MegaFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  116. } catch (IllegalAccessException ex) {
  117. java.util.logging.Logger.getLogger(MegaFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  118. } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  119. java.util.logging.Logger.getLogger(MegaFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  120. }
  121. //</editor-fold>
  122.  
  123. /* Create and display the form */
  124. java.awt.EventQueue.invokeLater(new Runnable() {
  125. public void run() {
  126. new MegaFrame().setVisible(true);
  127. }
  128. });
  129. }
  130.  
  131. public JTable getjTable1() {
  132. return jTable1;
  133. }
  134.  
  135. public void setjTable1(JTable jTable1) {
  136. this.jTable1 = jTable1;
  137. }
  138.  
  139. public MegaModel getModel() {
  140. return model;
  141. }
  142.  
  143. public void setModel(MegaModel model) {
  144. this.model = model;
  145. }
  146.  
  147.  
  148. // Variables declaration - do not modify
  149. private javax.swing.JScrollPane jScrollPane1;
  150. private javax.swing.JTable jTable1;
  151. // End of variables declaration
  152. }
  153.  
  154. __________________________________
  155. //QUESTA è LA CLASSE DEL MODELLO, MegaModel
  156.  
  157. package megamarathon2;
  158.  
  159. import java.util.List;
  160. import java.util.ArrayList;
  161. import javax.swing.table.DefaultTableModel;
  162.  
  163. public class MegaModel extends DefaultTableModel {
  164.  
  165. private List<MegaGame> list = new ArrayList<>();
  166.  
  167. @Override
  168. public Object getValueAt(int row, int column) {
  169. MegaGame gam = list.get(row);
  170. switch (column) {
  171. case 0:
  172. return gam.getTitle();
  173. case 1:
  174. return gam.getYear();
  175. case 2:
  176. return gam.getDeveloper();
  177. default:
  178. return "Errore";
  179. }
  180.  
  181. }
  182.  
  183. @Override
  184. public String getColumnName(int column) {
  185. switch (column) {
  186. case 0:
  187. return "Title";
  188. case 1:
  189. return "Year";
  190. case 2:
  191. return "Developer";
  192. default:
  193. return "Errore";
  194. }
  195. }
  196.  
  197. @Override
  198. public int getColumnCount() {
  199. return 3;
  200. }
  201.  
  202. @Override
  203. public int getRowCount() {
  204. return (list == null ? 0 : list.size());
  205. }
  206.  
  207. @Override
  208. public boolean isCellEditable(int i, int i1) {
  209. return false;
  210. }
  211.  
  212. public void add(MegaGame g) {
  213. list.add(g);
  214. }
  215.  
  216. public MegaGame get(int i) {
  217. return list.get(i);
  218. }
  219.  
  220. public void readAll() {
  221. list = MegaDAO.readAll();
  222. fireTableDataChanged();
  223. }
  224.  
  225. public void sortBy(String criterio) {
  226. list = MegaDAO.sortBy(criterio);
  227. fireTableDataChanged();
  228. }
  229.  
  230.  
  231.  
  232. public void save(MegaGame g) {
  233. MegaDAO.insert(g);
  234. }
  235.  
  236. public List<MegaGame> getList() {
  237. return list;
  238. }
  239.  
  240. }
  241.  
  242. _________________________________________________________________________
  243.  
  244. QUESTA E' LA CLASSE DEGLI ITEM CHE VANNO NELL'ARRAYLIST/NEL DB, MegaGames
  245.  
  246. package megamarathon2;
  247.  
  248. public class MegaGame {
  249. private String title;
  250. private String year;
  251. private String developer;
  252.  
  253.  
  254. //private String genre;
  255. //private String language;
  256. //private String clearTime;
  257. //private String clearDate;
  258. //private String info;
  259. //private String manual;
  260. //private String notes;
  261.  
  262. public String getTitle() {
  263. return title;
  264. }
  265.  
  266. public void setTitle(String title) {
  267. this.title = title;
  268. }
  269.  
  270. public String getYear() {
  271. return year;
  272. }
  273.  
  274. public void setYear(String year) {
  275. this.year = year;
  276. }
  277.  
  278. public String getDeveloper() {
  279. return developer;
  280. }
  281.  
  282. public void setDeveloper(String developer) {
  283. this.developer = developer;
  284. }
  285.  
  286.  
  287.  
  288.  
  289. }
  290.  
  291. _____________________________________________________________________
  292.  
  293. QUESTA E' LA CLASSE DEL DAO, MegaDao
  294. package megamarathon2;
  295. import java.sql.Connection;
  296. import java.sql.DriverManager;
  297. import java.sql.PreparedStatement;
  298. import java.sql.ResultSet;
  299. import java.sql.SQLException;
  300. import java.sql.Statement;
  301. import java.util.ArrayList;
  302. import java.util.List;
  303.  
  304. public class MegaDAO {
  305.  
  306. private static String url = "jdbc:mysql://localhost:3306/megamarathon?serverTimezone=Europe/Berlin";
  307. private static Connection conn = null;
  308. private static Statement stmt = null;
  309. private static ResultSet rs = null;
  310. private static String user = "root";
  311. private static String psw = "";
  312.  
  313. public static List<MegaGame> readAll() {
  314. PreparedStatement stmt = null;
  315. try {
  316. conn = DriverManager.getConnection(url, "root", "");
  317. String sql = "select * from gamelist";
  318. stmt = conn.prepareStatement(sql);
  319. rs = stmt.executeQuery();
  320. List<MegaGame> ris = new ArrayList<>();
  321. while (rs.next()) {
  322. MegaGame gam = new MegaGame();
  323.  
  324. gam.setTitle(rs.getString("title"));
  325. gam.setYear(rs.getString("year"));
  326. gam.setDeveloper(rs.getString("publisher/developer"));
  327.  
  328. ris.add(gam);
  329. }
  330. return ris;
  331.  
  332. } catch (SQLException ex) {
  333. ex.printStackTrace();
  334. System.out.println("Si è verificato un errore nell'accesso al DB");
  335. } finally {
  336. if (rs != null) {
  337. try {
  338. rs.close();
  339. } catch (SQLException ex) {
  340. // Eccezione ignorata volutamente perché non si può fare nulla
  341. System.out.println("Impossibile chiudere result set");
  342. }
  343. }
  344. if (stmt != null) {
  345. try {
  346. stmt.close();
  347. } catch (SQLException e) {
  348. // Eccezione ignorata volutamente perché non si può fare nulla
  349. System.out.println("Impossibile chiudere statement");
  350. }
  351. }
  352. if (conn != null) {
  353. try {
  354. conn.close();
  355. } catch (SQLException e) {
  356. // Eccezione ignorata volutamente perché non si può fare nulla
  357. System.out.println("Impossibile chiudere statement");
  358. }
  359. }
  360. }
  361.  
  362. return null;
  363. }
  364.  
  365. public static List<MegaGame> sortBy(String criterio) {
  366. PreparedStatement stmt = null;
  367. try {
  368. conn = DriverManager.getConnection(url, "root", "");
  369. String sql = "select * from gamelist order by " + criterio;
  370. stmt = conn.prepareStatement(sql);
  371.  
  372. rs = stmt.executeQuery();
  373. List<MegaGame> ris = new ArrayList<>();
  374. while (rs.next()) {
  375. MegaGame gam = new MegaGame();
  376.  
  377. gam.setTitle(rs.getString("title"));
  378. gam.setYear(rs.getString("year"));
  379. gam.setDeveloper(rs.getString("publisher/developer"));
  380.  
  381. ris.add(gam);
  382. }
  383.  
  384. return ris;
  385.  
  386. } catch (SQLException ex) {
  387. ex.printStackTrace();
  388. System.out.println("Si è verificato un errore nell'accesso al DB");
  389. } finally {
  390. if (rs != null) {
  391. try {
  392. rs.close();
  393. } catch (SQLException ex) {
  394. // Eccezione ignorata volutamente perché non si può fare nulla
  395. System.out.println("Impossibile chiudere result set");
  396. }
  397. }
  398. if (stmt != null) {
  399. try {
  400. stmt.close();
  401. } catch (SQLException e) {
  402. // Eccezione ignorata volutamente perché non si può fare nulla
  403. System.out.println("Impossibile chiudere statement");
  404. }
  405. }
  406. if (conn != null) {
  407. try {
  408. conn.close();
  409. } catch (SQLException e) {
  410. // Eccezione ignorata volutamente perché non si può fare nulla
  411. System.out.println("Impossibile chiudere statement");
  412. }
  413. }
  414. }
  415.  
  416. return null;
  417. }
  418.  
  419. public static ResultSet getRs() {
  420. return rs;
  421. }
  422.  
  423. public static void setRs(ResultSet rs) {
  424. MegaDAO.rs = rs;
  425. }
  426.  
  427. public static void insert(MegaGame g) {
  428.  
  429. }
  430. }
  431.  
  432. ______________________________________________________________
  433. QUESTA E' LA CLASSE DEL LISTENER PER L'HEADER, TableHeaderMouseListener
  434.  
  435. package megamarathon2;
  436.  
  437. import java.awt.Container;
  438. import java.awt.Point;
  439. import java.awt.event.MouseAdapter;
  440. import java.awt.event.MouseEvent;
  441. import javax.swing.JTable;
  442. import javax.swing.event.TableModelEvent;
  443.  
  444. public class TableHeaderMouseListener extends MouseAdapter {
  445.  
  446. private JTable table;
  447.  
  448. public TableHeaderMouseListener(JTable table) {
  449. this.table = table;
  450. }
  451.  
  452. @Override
  453. public void mouseClicked(MouseEvent event) {
  454.  
  455. Point point = event.getPoint();
  456. int column = table.columnAtPoint(point);
  457. System.out.println("clicked " + column);
  458. if (column == 1) {
  459. //qua ho provato di tutto e non so come farlo funzionare
  460. //pare si connetta al database in output ma non riordina un cazzo sulla tabella
  461. //forse non refresha ma non ci sono fireTableDataChanged() o validate() che tengano
  462. //credo sia colpa dell'associazione tra database e arraylist
  463. //e del fatto che non ho studiato le arraylist
  464. //comunque la logica fino a qua funziona, ed entra anche nell'if, la stampa "diocan" qui sotto lo comprova
  465. //pls help
  466.  
  467.  
  468. MegaFrame f = new MegaFrame();
  469. MegaDAO dao = new MegaDAO();
  470. MegaModel m = new MegaModel();
  471. m.sortBy("year");
  472.  
  473. System.out.println("diocan");
  474.  
  475. }
  476. }
  477. }
  478.  
  479. _________________________________________________________
  480.  
  481. P O R C O D D I O
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement