codice:
public class Docenteview {
private static final String root1 = "root";
private static final String mydb = "mydb1";
private static final int YES = 1234;
Connection conn = null;
Vector<String[]> rs =null;
PreparedStatement pst =null;
JFrame frame;
private static Connection db; // La connessione col Database
private static boolean connesso; // Flag che indica se la connessione � attiva o meno
private static DbConnection instance;
/**
* @wbp.nonvisual location=405,79
*/
private final Docente docente = new Docente();
private JTable table;
private void UpdateTable() {
try{
table = new JTable();
String query= "SELECT settimana.nomegiorno, ora.ora, prenotazione.idaula , insegnamento.nome FROM prenotazione, insegnamento_has_docente, settimana, ora, insegnamento WHERE insegnamento_has_docente.Docente_idDocente=1 AND insegnamento_has_docente.insegnamento_idinsegnamento= prenotazione.idinsegnamento AND prenotazione.idgiorno=settimana.idgiorno AND prenotazione.idora= ora.idora AND prenotazione.idinsegnamento= insegnamento.idinsegnamento ";
DbConnection stmt1 = new DbConnection();
Class.forName("com.mysql.jdbc.Driver");
db = DriverManager.getConnection("jdbc:mysql://127.0.0.1/" + mydb + "?user=" + root1 + "&password=" + YES);
connesso=true;
Statement stmt = db.createStatement(); // Creo lo Statement per l'esecuzione della query
ResultSet rs = stmt.executeQuery(query); // Ottengo il ResultSet dell'esecuzione della query
table.setModel(DbUtils.resultSetToTableModel(rs));
}catch(Exception e)
{JOptionPane.showMessageDialog(null, e);}
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Docenteview window = new Docenteview();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Docenteview() {
UpdateTable();
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 650, 495);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SpringLayout springLayout = new SpringLayout();
springLayout.putConstraint(SpringLayout.NORTH, table, 77, SpringLayout.NORTH, frame.getContentPane());
springLayout.putConstraint(SpringLayout.SOUTH, table, -29, SpringLayout.SOUTH, frame.getContentPane());
frame.getContentPane().setLayout(springLayout);
JButton btnConferma = new JButton("Aggiorna");
springLayout.putConstraint(SpringLayout.WEST, btnConferma, 267, SpringLayout.WEST, frame.getContentPane());
springLayout.putConstraint(SpringLayout.SOUTH, btnConferma, -16, SpringLayout.NORTH, table);
btnConferma.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent n) {
UpdateTable();// QUI DOVREBBE AGGIORNARE LA TABELLA
}
});
frame.getContentPane().add(btnConferma);
JButton btnEsci = new JButton("Esci");
springLayout.putConstraint(SpringLayout.NORTH, btnEsci, 38, SpringLayout.NORTH, frame.getContentPane());
btnEsci.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
springLayout.putConstraint(SpringLayout.EAST, btnEsci, -30, SpringLayout.EAST, frame.getContentPane());
frame.getContentPane().add(btnEsci);
springLayout.putConstraint(SpringLayout.WEST, table, 40, SpringLayout.WEST, frame.getContentPane());
springLayout.putConstraint(SpringLayout.EAST, table, 472, SpringLayout.WEST, frame.getContentPane());
frame.getContentPane().add(table);
initDataBindings();
}
protected void initDataBindings() {
}
}