Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //QUESTO E' UN BEL MAIN VUOTO
- package megamarathon2;
- public class MegaMarathon2 {
- public static void main(String[] args) {
- }
- }
- __________________________________________________________________________________________
- //QUESTA è LA CLASSE DEL FRAME, MegaFrame
- package megamarathon2;
- import javax.swing.JTable;
- import javax.swing.table.JTableHeader;
- import net.proteanit.sql.DbUtils;
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author lemon
- */
- public class MegaFrame extends javax.swing.JFrame {
- MegaModel model;
- MegaDAO dao = new MegaDAO();
- /**
- * Creates new form MegaFrame
- */
- public MegaFrame() {
- initComponents();
- model = new MegaModel();
- model.readAll();
- jTable1.setModel(model);
- JTableHeader header = jTable1.getTableHeader();
- header.addMouseListener(new TableHeaderMouseListener(jTable1));
- }
- /**
- * This method is called from within the constructor to initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is always
- * regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- jScrollPane1 = new javax.swing.JScrollPane();
- jTable1 = new javax.swing.JTable();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- jTable1.setModel(new javax.swing.table.DefaultTableModel(
- new Object [][] {
- {null, null, null, null},
- {null, null, null, null},
- {null, null, null, null},
- {null, null, null, null}
- },
- new String [] {
- "Title 1", "Title 2", "Title 3", "Title 4"
- }
- ));
- jScrollPane1.setViewportView(jTable1);
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(170, 170, 170)
- .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addContainerGap(241, Short.MAX_VALUE))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(123, 123, 123)
- .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addContainerGap(151, Short.MAX_VALUE))
- );
- pack();
- }// </editor-fold>
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- /* Set the Nimbus look and feel */
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
- /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- */
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- if ("Nimbus".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(MegaFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(MegaFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(MegaFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(MegaFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new MegaFrame().setVisible(true);
- }
- });
- }
- public JTable getjTable1() {
- return jTable1;
- }
- public void setjTable1(JTable jTable1) {
- this.jTable1 = jTable1;
- }
- public MegaModel getModel() {
- return model;
- }
- public void setModel(MegaModel model) {
- this.model = model;
- }
- // Variables declaration - do not modify
- private javax.swing.JScrollPane jScrollPane1;
- private javax.swing.JTable jTable1;
- // End of variables declaration
- }
- __________________________________
- //QUESTA è LA CLASSE DEL MODELLO, MegaModel
- package megamarathon2;
- import java.util.List;
- import java.util.ArrayList;
- import javax.swing.table.DefaultTableModel;
- public class MegaModel extends DefaultTableModel {
- private List<MegaGame> list = new ArrayList<>();
- @Override
- public Object getValueAt(int row, int column) {
- MegaGame gam = list.get(row);
- switch (column) {
- case 0:
- return gam.getTitle();
- case 1:
- return gam.getYear();
- case 2:
- return gam.getDeveloper();
- default:
- return "Errore";
- }
- }
- @Override
- public String getColumnName(int column) {
- switch (column) {
- case 0:
- return "Title";
- case 1:
- return "Year";
- case 2:
- return "Developer";
- default:
- return "Errore";
- }
- }
- @Override
- public int getColumnCount() {
- return 3;
- }
- @Override
- public int getRowCount() {
- return (list == null ? 0 : list.size());
- }
- @Override
- public boolean isCellEditable(int i, int i1) {
- return false;
- }
- public void add(MegaGame g) {
- list.add(g);
- }
- public MegaGame get(int i) {
- return list.get(i);
- }
- public void readAll() {
- list = MegaDAO.readAll();
- fireTableDataChanged();
- }
- public void sortBy(String criterio) {
- list = MegaDAO.sortBy(criterio);
- fireTableDataChanged();
- }
- public void save(MegaGame g) {
- MegaDAO.insert(g);
- }
- public List<MegaGame> getList() {
- return list;
- }
- }
- _________________________________________________________________________
- QUESTA E' LA CLASSE DEGLI ITEM CHE VANNO NELL'ARRAYLIST/NEL DB, MegaGames
- package megamarathon2;
- public class MegaGame {
- private String title;
- private String year;
- private String developer;
- //private String genre;
- //private String language;
- //private String clearTime;
- //private String clearDate;
- //private String info;
- //private String manual;
- //private String notes;
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public String getYear() {
- return year;
- }
- public void setYear(String year) {
- this.year = year;
- }
- public String getDeveloper() {
- return developer;
- }
- public void setDeveloper(String developer) {
- this.developer = developer;
- }
- }
- _____________________________________________________________________
- QUESTA E' LA CLASSE DEL DAO, MegaDao
- package megamarathon2;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.List;
- public class MegaDAO {
- private static String url = "jdbc:mysql://localhost:3306/megamarathon?serverTimezone=Europe/Berlin";
- private static Connection conn = null;
- private static Statement stmt = null;
- private static ResultSet rs = null;
- private static String user = "root";
- private static String psw = "";
- public static List<MegaGame> readAll() {
- PreparedStatement stmt = null;
- try {
- conn = DriverManager.getConnection(url, "root", "");
- String sql = "select * from gamelist";
- stmt = conn.prepareStatement(sql);
- rs = stmt.executeQuery();
- List<MegaGame> ris = new ArrayList<>();
- while (rs.next()) {
- MegaGame gam = new MegaGame();
- gam.setTitle(rs.getString("title"));
- gam.setYear(rs.getString("year"));
- gam.setDeveloper(rs.getString("publisher/developer"));
- ris.add(gam);
- }
- return ris;
- } catch (SQLException ex) {
- ex.printStackTrace();
- System.out.println("Si è verificato un errore nell'accesso al DB");
- } finally {
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException ex) {
- // Eccezione ignorata volutamente perché non si può fare nulla
- System.out.println("Impossibile chiudere result set");
- }
- }
- if (stmt != null) {
- try {
- stmt.close();
- } catch (SQLException e) {
- // Eccezione ignorata volutamente perché non si può fare nulla
- System.out.println("Impossibile chiudere statement");
- }
- }
- if (conn != null) {
- try {
- conn.close();
- } catch (SQLException e) {
- // Eccezione ignorata volutamente perché non si può fare nulla
- System.out.println("Impossibile chiudere statement");
- }
- }
- }
- return null;
- }
- public static List<MegaGame> sortBy(String criterio) {
- PreparedStatement stmt = null;
- try {
- conn = DriverManager.getConnection(url, "root", "");
- String sql = "select * from gamelist order by " + criterio;
- stmt = conn.prepareStatement(sql);
- rs = stmt.executeQuery();
- List<MegaGame> ris = new ArrayList<>();
- while (rs.next()) {
- MegaGame gam = new MegaGame();
- gam.setTitle(rs.getString("title"));
- gam.setYear(rs.getString("year"));
- gam.setDeveloper(rs.getString("publisher/developer"));
- ris.add(gam);
- }
- return ris;
- } catch (SQLException ex) {
- ex.printStackTrace();
- System.out.println("Si è verificato un errore nell'accesso al DB");
- } finally {
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException ex) {
- // Eccezione ignorata volutamente perché non si può fare nulla
- System.out.println("Impossibile chiudere result set");
- }
- }
- if (stmt != null) {
- try {
- stmt.close();
- } catch (SQLException e) {
- // Eccezione ignorata volutamente perché non si può fare nulla
- System.out.println("Impossibile chiudere statement");
- }
- }
- if (conn != null) {
- try {
- conn.close();
- } catch (SQLException e) {
- // Eccezione ignorata volutamente perché non si può fare nulla
- System.out.println("Impossibile chiudere statement");
- }
- }
- }
- return null;
- }
- public static ResultSet getRs() {
- return rs;
- }
- public static void setRs(ResultSet rs) {
- MegaDAO.rs = rs;
- }
- public static void insert(MegaGame g) {
- }
- }
- ______________________________________________________________
- QUESTA E' LA CLASSE DEL LISTENER PER L'HEADER, TableHeaderMouseListener
- package megamarathon2;
- import java.awt.Container;
- import java.awt.Point;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import javax.swing.JTable;
- import javax.swing.event.TableModelEvent;
- public class TableHeaderMouseListener extends MouseAdapter {
- private JTable table;
- public TableHeaderMouseListener(JTable table) {
- this.table = table;
- }
- @Override
- public void mouseClicked(MouseEvent event) {
- Point point = event.getPoint();
- int column = table.columnAtPoint(point);
- System.out.println("clicked " + column);
- if (column == 1) {
- //qua ho provato di tutto e non so come farlo funzionare
- //pare si connetta al database in output ma non riordina un cazzo sulla tabella
- //forse non refresha ma non ci sono fireTableDataChanged() o validate() che tengano
- //credo sia colpa dell'associazione tra database e arraylist
- //e del fatto che non ho studiato le arraylist
- //comunque la logica fino a qua funziona, ed entra anche nell'if, la stampa "diocan" qui sotto lo comprova
- //pls help
- MegaFrame f = new MegaFrame();
- MegaDAO dao = new MegaDAO();
- MegaModel m = new MegaModel();
- m.sortBy("year");
- System.out.println("diocan");
- }
- }
- }
- _________________________________________________________
- P O R C O D D I O
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement