mi sono impantanato
ho creato un nuovo progetto per provare l'agendo
classe principale:
codice:
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.Calendar;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.border.Border;
import javax.swing.table.DefaultTableModel;
public class ProvaTabella {
CreazioneDb data = new CreazioneDb();
public ProvaTabella(){
JFrame finestra = new JFrame();
DefaultTableModel model = new DefaultTableModel();
JTable tabella = new JTable (model);
model.addColumn(" / ");
model.addColumn("lavorante1");
model.addColumn("lavorante2");
model.addColumn("lavorante3");
model.addColumn("lavorante4");
model.addColumn("lavorante5");
Object[] ora1 = {"8:00"};
Object[] ora2 = {"8:15"};
Object[] ora3 = {"8:30"};
Object[] ora4 = {"8:45"};
Object[] ora5 = {"9:00"};
Object[] ora6 = {"9:15"};
Object[] ora7 = {"9:30"};
Object[] ora8 = {"9:45"};
Object[] ora9 = {"10:00"};
Object[] ora10 = {"10:15"};
Object[] ora11 = {"10:30"};
Object[] ora12 = {"10:45"};
Object[] ora13 = {"11:00"};
Object[] ora14 = {"11:15"};
Object[] ora15 = {"11:30"};
Object[] ora16 = {"11:45"};
Object[] ora17 = {"12:00"};
Object[] ora18 = {"12:15"};
Object[] ora19 = {"12:30"};
Object[] ora20 = {"12:45"};
Object[] ora21 = {"13:00"};
Object[] ora22 = {"13:15"};
Object[] ora23 = {"13:30"};
Object[] ora24 = {"13:45"};
Object[] ora25 = {"14:00"};
Object[] ora26 = {"14:15"};
Object[] ora27 = {"14:30"};
Object[] ora28 = {"14:45"};
Object[] ora29 = {"15:00"};
Object[] ora30 = {"15:15"};
Object[] ora31 = {"15:30"};
Object[] ora32 = {"15:45"};
Object[] ora33 = {"16:00"};
Object[] ora34 = {"16:15"};
Object[] ora35 = {"16:30"};
Object[] ora36 = {"16:45"};
Object[] ora37 = {"17:00"};
Object[] ora38 = {"17:15"};
Object[] ora39 = {"17:30"};
Object[] ora40 = {"17:45"};
Object[] ora41 = {"18:00"};
Object[] ora42 = {"18:15"};
Object[] ora43 = {"18:30"};
Object[] ora44 = {"18:45"};
Object[] ora45 = {"19:15"};
Object[] ora46 = {"19:30"};
Object[] ora47 = {"19:45"};
Object[] ora48 = {"20:00"};
model.addRow(ora1);
model.addRow(ora2);
model.addRow(ora3);
model.addRow(ora4);
model.addRow(ora5);
model.addRow(ora6);
model.addRow(ora7);
model.addRow(ora8);
model.addRow(ora9);
model.addRow(ora10);
model.addRow(ora11);
model.addRow(ora12);
model.addRow(ora13);
model.addRow(ora14);
model.addRow(ora15);
model.addRow(ora16);
model.addRow(ora17);
model.addRow(ora18);
model.addRow(ora19);
model.addRow(ora20);
model.addRow(ora21);
model.addRow(ora22);
model.addRow(ora23);
model.addRow(ora24);
model.addRow(ora25);
model.addRow(ora26);
model.addRow(ora27);
model.addRow(ora28);
model.addRow(ora29);
model.addRow(ora30);
model.addRow(ora31);
model.addRow(ora32);
model.addRow(ora33);
model.addRow(ora34);
model.addRow(ora35);
model.addRow(ora36);
model.addRow(ora37);
model.addRow(ora38);
model.addRow(ora39);
model.addRow(ora40);
model.addRow(ora41);
model.addRow(ora42);
model.addRow(ora43);
model.addRow(ora44);
model.addRow(ora45);
model.addRow(ora46);
model.addRow(ora47);
model.addRow(ora48);
finestra.add(tabella);
finestra.setBounds(50, 50, 500, 500);
finestra.setVisible(true);
model.fireTableRowsUpdated(0, model.getRowCount());
Vector data = model.getDataVector();
System.out.println(data);
// try {
//
// Connection conn = DriverManager.getConnection("jdbc:sqlite:gestione.db");
//
// PreparedStatement prep = conn.prepareStatement("INSERT INTO agenda(data) VALUES(?)");
//
// prep.setString(1, data);
//
// prep.execute();
//
// } catch (SQLException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }
// }
}
public static void main(String[] args) {
ProvaTabella a = new ProvaTabella();
}
}
tablemodel:
codice:
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
//sente tutto quello ke si fa sulla cella e capisce quale azione è stata svolta
public class MyTableModel implements TableModelListener{
JTable table;
MyTableModel(JTable table) {
this.table = table;
table.setRowSelectionAllowed(false);
table.setColumnSelectionAllowed(false);
table.setCellSelectionEnabled(false);
}
public void tableChanged(TableModelEvent e) {
int firstRow = e.getFirstRow();
int lastRow = e.getLastRow();
int index = e.getColumn();
switch (e.getType()) {
case TableModelEvent.INSERT:
for (int i = firstRow; i <= lastRow; i++) {
System.out.println("inserito");
}
break;
case TableModelEvent.UPDATE:
if (firstRow == TableModelEvent.HEADER_ROW) {
if (index == TableModelEvent.ALL_COLUMNS) {
System.out.println("A column was added");
} else {
System.out.println("aggiornata");
System.out.println(index + "in header changed");
}
}
else {
for (int i = firstRow; i <= lastRow; i++) {
if (index == TableModelEvent.ALL_COLUMNS) {
System.out.println("All columns have changed");
break;
} else {
System.out.println(index);
System.out.println(table.getSelectedRow() + table.getSelectedColumn());
}
}
}
break;
case TableModelEvent.DELETE:
for (int i = firstRow; i <= lastRow; i++) {
System.out.println("delete");
}
break;
}
}
}
e la parte che si occupa di creare il db:
codice:
import java.sql.*;
// classe per la creazione del db
public class CreazioneDb {
public CreazioneDb(){
Statement stmt;
PreparedStatement pstmt;
ResultSet rs;
try {
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:agenda.db");
stmt = conn.createStatement();
stmt.executeUpdate("CREATE TABLE IF NOT EXISTS agenda " +
"(id INTEGER primary key autoincrement, " +
"data TEXT NOT NULL, " +
"ora TEXT NOT NULL, " +
"colonna TEXT NOT NULL, " +
"appuntamento TEXT )");
// stmt.close(); // rilascio le risorse
// pstmt.close(); // rilascio le risorse
// conn.close(); // termino la connessione
}
catch(ClassNotFoundException e)
{
System.out.println(e);
}
catch(SQLException e)
{
System.out.println(e);
}
}
}
con Vector data = model.getDataVector(); recupero i dati di tutta la tabella,ma ovviamente non posso inserirli così all'interno del db
avrei bisogno di sapere se si possono salvare i dati di un vettore così grande(tenendo conto che questo è soltanto un giorno
) o se devo trasformarlo oppure non va proprio bene fare così