Skip to content

Instantly share code, notes, and snippets.

@thorskjold

thorskjold/JFrame.class

Last active Mar 11, 2021
Embed
What would you like to do?
package DDV;
public class NewJFrame extends javax.swing.JFrame {
public NewJFrame() {
initComponents();
}
@SuppressWarnings("unchecked")
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jTextField3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jTextField4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jTextField5ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JComboBox<String> jComboBox1; private javax.swing.JComboBox<String> jComboBox2; private
javax.swing.JComboBox<String> jComboBox3; private javax.swing.JComboBox<String> jComboBox4; private
javax.swing.JComboBox<String> jComboBox5; private javax.swing.JComboBox<String> jComboBox6; private
javax.swing.JComboBox<String> jComboBox7; private javax.swing.JComboBox<String> jComboBox8; private javax.swing.JPanel
jPanel1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private javax.swing.JTextField jTextField4;
private javax.swing.JTextField jTextField5;
private javax.swing.JTextField jTextField6;
// End of variables declaration
}
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setVisible(true);
}
JFrame
private void buttonOpretBorgerActionPerformed(java.awt.event.ActionEvent evt) {
Borger b = new Borger(); b.setCPR(textCPR.getText()); b.setNavn(textNavn.getText()); model.addElement(b);
}
Borger klasse
public class Borger {
private String CPR; private String Navn; public Borger() { }
public Borger(String CPR, String Navn) {
this.CPR = CPR;
this.Navn = Navn;
}
public String getCPR() {
return CPR;
}
public void setCPR(String CPR) {
this.CPR = CPR;
}
public String getNavn() {
return Navn;
}
public void setNavn(String Navn) {
this.Navn = Navn;
}
@Override
public String toString() {
return "Borger{" + "CPR=" + CPR + ", Navn=" + Navn + '}';
}
}
private void buttonOpretBorgerActionPerformed(java.awt.event.ActionEvent evt) {
Connection c = null;
Statement stmt = null;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:database.db");
c.setAutoCommit(true);
stmt = c.createStatement();
String stmtText = "INSERT INTO Borger (CPR, Navn) VALUES ('"+ textCPR.getText() + "', '" + textNavn.getText() +"');";
stmt.executeQuery(stmtText);
stmt.close();
c.close();
Borger b = new Borger();
b.setCPR(textCPR.getText());
b.setNavn(textNavn.getText());
model.addElement(b);
} catch (ClassNotFoundException e) {
} catch (SQLException e) {
} catch (Exception e) { }
}
private void buttonHentDatabaseActionPerformed(java.awt.event.ActionEvent evt) {
Connection c = null;
Statement stmt = null;
model.clear();
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:database.db");
c.setAutoCommit(true);
stmt = c.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM Borger;");
while (rs.next()) {
Borger b = new Borger();
b.setCPR(rs.getString("CPR"));
b.setNavn(rs.getString("Navn"));
model.addElement(b);
}
rs.close();
stmt.close();
c.close();
} catch (ClassNotFoundException e) {
} catch (SQLException e) {
} catch (Exception e) {
}
}
private void buttonOpdaterBorgerActionPerformed(java.awt.event.ActionEvent evt) {
}
Connection c = null;
Statement stmt = null;
try {
int indeks = listBorger.getSelectedIndex();
Borger valgtElement = (Borger) model.getElementAt(indeks);
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:database.db");
c.setAutoCommit(true);
stmt = c.createStatement();
String stmtText = "UPDATE Borger "
+ "SET CPR = '" + textCPR.getText() + "', Navn = '" + textNavn.getText() + "' "
+" WHERE CPR = '"+ valgtElement.getCPR() + "' AND Navn = '" + valgtElement.getNavn() + "';";
stmt.executeQuery(stmtText);
stmt.close();
c.close();
Borger b = new Borger();
b.setCPR(textCPR.getText());
b.setNavn(textNavn.getText());
model.removeElementAt(indeks);
model.addElement(b);
} catch (ClassNotFoundException e) {
} catch (SQLException e) {
} catch (Exception e) {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment