import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.DefaultListModel;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/*
* 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 Vako
*/
public class Meine_Bücher extends javax.swing.JFrame {
//creat a new DefaultListModel for each jList
DefaultListModel ListModel1;
DefaultListModel ListModel2;
DefaultListModel ListModel3;
private FileWriter file1;
private FileWriter file2;
private FileWriter file3;
private BufferedWriter writer1;
private BufferedWriter writer2;
private BufferedWriter writer3;
private FileReader fileReader1;
private FileReader fileReader2;
private FileReader fileReader3;
Connection con = null;
PreparedStatement pst = null;
ResultSet st = null;
/**
* Creates new form Meine_Bücher
*/
public Meine_Bücher() throws IOException {
initComponents();
//creat a new DefaultListModel for each jList
ListModel1 = new DefaultListModel();
ListModel2 = new DefaultListModel();
ListModel3 = new DefaultListModel();
//setting each Model to the jList
jList1.setModel(ListModel1);
jList2.setModel(ListModel2);
jList3.setModel(ListModel3);
}
//The methods will be run in the method btnEInfügenActionPerformed
public void einfügen1() {
ListModel1.addElement(txtFeld.getText());
txtFeld.setText("");
}
public void einfügen2(){
ListModel2.addElement(txtFeld.getText());
txtFeld.setText("");
}
public void einfügen3(){
ListModel3.addElement(txtFeld.getText());
txtFeld.setText("");
}
/**
* 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() {
buttonGroup1 = new javax.swing.ButtonGroup();
jScrollPane1 = new javax.swing.JScrollPane();
jList1 = new javax.swing.JList<>();
jScrollPane2 = new javax.swing.JScrollPane();
jList2 = new javax.swing.JList<>();
jScrollPane3 = new javax.swing.JScrollPane();
jList3 = new javax.swing.JList<>();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
rbSach = new javax.swing.JRadioButton();
rbRom = new javax.swing.JRadioButton();
rbFach = new javax.swing.JRadioButton();
txtFeld = new javax.swing.JTextField();
btnEinfügen = new javax.swing.JButton();
btnEntfernen = new javax.swing.JButton();
btnAktualisieren = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jScrollPane1.setViewportView(jList1);
jScrollPane2.setViewportView(jList2);
jScrollPane3.setViewportView(jList3);
jLabel1.setText("Sachbücher");
jLabel2.setText("Fachbücher");
jLabel3.setText("Romane");
buttonGroup1.add(rbSach);
rbSach.setText("Sachbücher");
rbSach.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
rbSachActionPerformed(evt);
}
});
buttonGroup1.add(rbRom);
rbRom.setText("Romane");
rbRom.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
rbRomActionPerformed(evt);
}
});
buttonGroup1.add(rbFach);
rbFach.setText("Fachbücher");
rbFach.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
rbFachActionPerformed(evt);
}
});
txtFeld.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtFeldActionPerformed(evt);
}
});
btnEinfügen.setText("Einfügen");
btnEinfügen.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnEinfügenActionPerformed(evt);
}
});
btnEntfernen.setText("Entfernen");
btnEntfernen.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnEntfernenActionPerformed(evt);
}
});
btnAktualisieren.setText("aktualisieren");
btnAktualisieren.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAktualisierenActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(rbSach)
.addGap(18, 18, 18)
.addComponent(rbFach)
.addGap(18, 18, 18)
.addComponent(rbRom, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(txtFeld)
.addComponent(btnEinfügen, javax.swing.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE)))
.addComponent(btnEntfernen, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnAktualisieren, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(3, 3, 3)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane3)
.addComponent(jScrollPane2)
.addComponent(jScrollPane1)))
.addGroup(layout.createSequentialGroup()
.addGap(122, 122, 122)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(rbSach)
.addComponent(rbRom)
.addComponent(rbFach))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(txtFeld, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnEinfügen, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnEntfernen, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnAktualisieren, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 19, Short.MAX_VALUE)))
.addContainerGap())
);
pack();
}// </editor-fold>
private void btnEinfügenActionPerformed(java.awt.event.ActionEvent evt) {
//check the radio button and add the Text from TextField to the List which was selected
if (rbSach.isSelected() == true) {
einfügen1();
}else if (rbFach.isSelected() == true){
einfügen2();
}else if (rbRom.isSelected() == true){
einfügen3();
}
try {
String sql;
sql = "INSERT INTO `bücherliste`(`Sachbücher`, `Fachbücher`, `Romane`) VALUES (?,?,?)";
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3307/bücherliste", "root", "");
pst = con.prepareStatement(sql);
if (rbSach.isSelected() == true){
pst.setString(1,txtFeld.getText());
}else if (rbFach.isSelected() == true){
pst.setString(2,txtFeld.getText());
}else if (rbRom.isSelected() == true){
pst.setString(3,txtFeld.getText());
}
pst.executeUpdate();
} catch (SQLException ex) {
Logger.getLogger(Meine_Bücher.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Meine_Bücher.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void txtFeldActionPerformed(java.awt.event.ActionEvent evt) {
}
private void rbSachActionPerformed(java.awt.event.ActionEvent evt) {
}
private void rbFachActionPerformed(java.awt.event.ActionEvent evt) {
}
private void rbRomActionPerformed(java.awt.event.ActionEvent evt) {
}
private void btnEntfernenActionPerformed(java.awt.event.ActionEvent evt) {
//removing the Element which was added from the List.
int selectedIndex1 = jList1.getSelectedIndex();
if (selectedIndex1 != -1) {
ListModel1.remove(selectedIndex1);
}
int selectedIndex2 = jList2.getSelectedIndex();
if (selectedIndex2 != -1) {
ListModel2.remove(selectedIndex2);
}
int selectedIndex3 = jList3.getSelectedIndex();
if (selectedIndex3 != -1) {
ListModel3.remove(selectedIndex3);
}
}
private void btnAktualisierenActionPerformed(java.awt.event.ActionEvent evt) {
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) throws IOException {
/* 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(Meine_Bücher.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Meine_Bücher.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Meine_Bücher.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Meine_Bücher.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() {
try {
new Meine_Bücher().setVisible(true);
} catch (IOException ex) {
Logger.getLogger(Meine_Bücher.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnAktualisieren;
private javax.swing.JButton btnEinfügen;
private javax.swing.JButton btnEntfernen;
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JList<String> jList1;
private javax.swing.JList<String> jList2;
private javax.swing.JList<String> jList3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JRadioButton rbFach;
private javax.swing.JRadioButton rbRom;
private javax.swing.JRadioButton rbSach;
public javax.swing.JTextField txtFeld;
// End of variables declaration
}
Das ist der gesamte Code von Netbeans. Leider kann ich das Projekt selbst nicht hochladen. Ich möchte einmal wissen was ich genau machen muss, um meine Strings, die ich in JList einspeichere, in eine Datenbank speichern zu können. Wie sie sehen können habe ich schon paar Sachen mit MySQL u.s.w ausprobiert aber halt nicht erfolgreich. Ich habe die Zeilen doch drinne gelassen, damit sie sehen, vielleicht habe ich doch einiges richtig. Vielen Dank voraus.
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.DefaultListModel;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/*
* 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 Vako
*/
public class Meine_Bücher extends javax.swing.JFrame {
//creat a new DefaultListModel for each jList
DefaultListModel ListModel1;
DefaultListModel ListModel2;
DefaultListModel ListModel3;
private FileWriter file1;
private FileWriter file2;
private FileWriter file3;
private BufferedWriter writer1;
private BufferedWriter writer2;
private BufferedWriter writer3;
private FileReader fileReader1;
private FileReader fileReader2;
private FileReader fileReader3;
Connection con = null;
PreparedStatement pst = null;
ResultSet st = null;
/**
* Creates new form Meine_Bücher
*/
public Meine_Bücher() throws IOException {
initComponents();
//creat a new DefaultListModel for each jList
ListModel1 = new DefaultListModel();
ListModel2 = new DefaultListModel();
ListModel3 = new DefaultListModel();
//setting each Model to the jList
jList1.setModel(ListModel1);
jList2.setModel(ListModel2);
jList3.setModel(ListModel3);
}
//The methods will be run in the method btnEInfügenActionPerformed
public void einfügen1() {
ListModel1.addElement(txtFeld.getText());
txtFeld.setText("");
}
public void einfügen2(){
ListModel2.addElement(txtFeld.getText());
txtFeld.setText("");
}
public void einfügen3(){
ListModel3.addElement(txtFeld.getText());
txtFeld.setText("");
}
/**
* 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() {
buttonGroup1 = new javax.swing.ButtonGroup();
jScrollPane1 = new javax.swing.JScrollPane();
jList1 = new javax.swing.JList<>();
jScrollPane2 = new javax.swing.JScrollPane();
jList2 = new javax.swing.JList<>();
jScrollPane3 = new javax.swing.JScrollPane();
jList3 = new javax.swing.JList<>();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
rbSach = new javax.swing.JRadioButton();
rbRom = new javax.swing.JRadioButton();
rbFach = new javax.swing.JRadioButton();
txtFeld = new javax.swing.JTextField();
btnEinfügen = new javax.swing.JButton();
btnEntfernen = new javax.swing.JButton();
btnAktualisieren = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jScrollPane1.setViewportView(jList1);
jScrollPane2.setViewportView(jList2);
jScrollPane3.setViewportView(jList3);
jLabel1.setText("Sachbücher");
jLabel2.setText("Fachbücher");
jLabel3.setText("Romane");
buttonGroup1.add(rbSach);
rbSach.setText("Sachbücher");
rbSach.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
rbSachActionPerformed(evt);
}
});
buttonGroup1.add(rbRom);
rbRom.setText("Romane");
rbRom.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
rbRomActionPerformed(evt);
}
});
buttonGroup1.add(rbFach);
rbFach.setText("Fachbücher");
rbFach.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
rbFachActionPerformed(evt);
}
});
txtFeld.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtFeldActionPerformed(evt);
}
});
btnEinfügen.setText("Einfügen");
btnEinfügen.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnEinfügenActionPerformed(evt);
}
});
btnEntfernen.setText("Entfernen");
btnEntfernen.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnEntfernenActionPerformed(evt);
}
});
btnAktualisieren.setText("aktualisieren");
btnAktualisieren.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAktualisierenActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(rbSach)
.addGap(18, 18, 18)
.addComponent(rbFach)
.addGap(18, 18, 18)
.addComponent(rbRom, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(txtFeld)
.addComponent(btnEinfügen, javax.swing.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE)))
.addComponent(btnEntfernen, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnAktualisieren, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(3, 3, 3)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane3)
.addComponent(jScrollPane2)
.addComponent(jScrollPane1)))
.addGroup(layout.createSequentialGroup()
.addGap(122, 122, 122)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(rbSach)
.addComponent(rbRom)
.addComponent(rbFach))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(txtFeld, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnEinfügen, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnEntfernen, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnAktualisieren, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 19, Short.MAX_VALUE)))
.addContainerGap())
);
pack();
}// </editor-fold>
private void btnEinfügenActionPerformed(java.awt.event.ActionEvent evt) {
//check the radio button and add the Text from TextField to the List which was selected
if (rbSach.isSelected() == true) {
einfügen1();
}else if (rbFach.isSelected() == true){
einfügen2();
}else if (rbRom.isSelected() == true){
einfügen3();
}
try {
String sql;
sql = "INSERT INTO `bücherliste`(`Sachbücher`, `Fachbücher`, `Romane`) VALUES (?,?,?)";
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3307/bücherliste", "root", "");
pst = con.prepareStatement(sql);
if (rbSach.isSelected() == true){
pst.setString(1,txtFeld.getText());
}else if (rbFach.isSelected() == true){
pst.setString(2,txtFeld.getText());
}else if (rbRom.isSelected() == true){
pst.setString(3,txtFeld.getText());
}
pst.executeUpdate();
} catch (SQLException ex) {
Logger.getLogger(Meine_Bücher.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Meine_Bücher.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void txtFeldActionPerformed(java.awt.event.ActionEvent evt) {
}
private void rbSachActionPerformed(java.awt.event.ActionEvent evt) {
}
private void rbFachActionPerformed(java.awt.event.ActionEvent evt) {
}
private void rbRomActionPerformed(java.awt.event.ActionEvent evt) {
}
private void btnEntfernenActionPerformed(java.awt.event.ActionEvent evt) {
//removing the Element which was added from the List.
int selectedIndex1 = jList1.getSelectedIndex();
if (selectedIndex1 != -1) {
ListModel1.remove(selectedIndex1);
}
int selectedIndex2 = jList2.getSelectedIndex();
if (selectedIndex2 != -1) {
ListModel2.remove(selectedIndex2);
}
int selectedIndex3 = jList3.getSelectedIndex();
if (selectedIndex3 != -1) {
ListModel3.remove(selectedIndex3);
}
}
private void btnAktualisierenActionPerformed(java.awt.event.ActionEvent evt) {
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) throws IOException {
/* 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(Meine_Bücher.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Meine_Bücher.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Meine_Bücher.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Meine_Bücher.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() {
try {
new Meine_Bücher().setVisible(true);
} catch (IOException ex) {
Logger.getLogger(Meine_Bücher.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnAktualisieren;
private javax.swing.JButton btnEinfügen;
private javax.swing.JButton btnEntfernen;
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JList<String> jList1;
private javax.swing.JList<String> jList2;
private javax.swing.JList<String> jList3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JRadioButton rbFach;
private javax.swing.JRadioButton rbRom;
private javax.swing.JRadioButton rbSach;
public javax.swing.JTextField txtFeld;
// End of variables declaration
}
Das ist der gesamte Code von Netbeans. Leider kann ich das Projekt selbst nicht hochladen. Ich möchte einmal wissen was ich genau machen muss, um meine Strings, die ich in JList einspeichere, in eine Datenbank speichern zu können. Wie sie sehen können habe ich schon paar Sachen mit MySQL u.s.w ausprobiert aber halt nicht erfolgreich. Ich habe die Zeilen doch drinne gelassen, damit sie sehen, vielleicht habe ich doch einiges richtig. Vielen Dank voraus.