Hi Leute, im rahmen eines Projektes habe ich eine Oberfläche gebaut mit welcher ich nun aus den ausgewählten JComboBoxen ein query erstellen möchte, dass funktioniert auch soweit. Allerdings funktioniert das nur für alle Auswahlkriterien. Ich würde gerne, dass er bei der ersten Combobox, nur einen ganzen Table auswählt (SELECT * FROM ...), beim der zweiten ComboBox, aus dieser dann eine Bedingung dazuholt (SELECT * FROM ... WHERE ...) und sollte mann auch über die dritte ComboBox filtern dann (SELECT * FROM ... WHERE ... AND ...) abgefragt werden.
ich würde mich freuen wenn mir jemand Helfen könnte.
Hier ist Code: für die Oberfläche diese wird in der Mainmethode nur aufgerufen und sichtbar gemacht.
ich würde mich freuen wenn mir jemand Helfen könnte.
Hier ist Code: für die Oberfläche diese wird in der Mainmethode nur aufgerufen und sichtbar gemacht.
Java:
package selction;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import javax.swing.table.DefaultTableModel;
/**
*
*
*/
public class MainFrame extends javax.swing.JFrame {
/**
* Creates new form MainFrame
*/
public MainFrame() {
initComponents();
}
/**
* 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.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
überschrift1 = new javax.swing.JLabel();
überschrift2 = new javax.swing.JLabel();
LpSelectLabel = new javax.swing.JLabel();
ifcComboBox = new javax.swing.JComboBox<>();
ifcSelectLabel = new javax.swing.JLabel();
LpComboBox = new javax.swing.JComboBox<>();
AttributSelectLabel = new javax.swing.JLabel();
AttributComboBox = new javax.swing.JComboBox<>();
SuchButton = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
tblData = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
überschrift1.setFont(new java.awt.Font("Segoe UI", 0, 24)); // NOI18N
überschrift1.setText("Schrobsdorff Bau AG");
überschrift1.setPreferredSize(new java.awt.Dimension(40, 15));
überschrift2.setFont(new java.awt.Font("Segoe UI", 0, 18)); // NOI18N
überschrift2.setText("Bauteilkatalog");
überschrift2.setPreferredSize(new java.awt.Dimension(40, 15));
LpSelectLabel.setFont(new java.awt.Font("Segoe UI", 0, 16)); // NOI18N
LpSelectLabel.setText("Leistungsphase wählen:");
ifcComboBox.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Wand", "Decke", "Stütze", "Träger" }));
ifcComboBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
}
});
ifcSelectLabel.setFont(new java.awt.Font("Segoe UI", 0, 16)); // NOI18N
ifcSelectLabel.setText("ifcType wählen:");
LpComboBox.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Lp3", "Lp4", "Lp5",}));
LpComboBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
}
});
AttributSelectLabel.setFont(new java.awt.Font("Segoe UI", 0, 16)); // NOI18N
AttributSelectLabel.setText("Attribut wählen:");
AttributComboBox.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Typ","Festigkeit","Oberflächenqualität"}));
SuchButton.setText("Suchen");
SuchButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evnt) {
try {
SuchButtonActionPerformed(evnt);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
tblData.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(tblData);
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(59, 59, 59)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(AttributSelectLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(ifcComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(überschrift2, javax.swing.GroupLayout.DEFAULT_SIZE, 264, Short.MAX_VALUE)
.addComponent(überschrift1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(LpSelectLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(ifcSelectLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(LpComboBox, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(AttributComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(SuchButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 1260, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(34, 34, 34)
.addComponent(überschrift1, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1)
.addGroup(layout.createSequentialGroup()
.addComponent(überschrift2, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(45, 45, 45)
.addComponent(ifcSelectLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(ifcComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(LpSelectLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(LpComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(AttributSelectLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(AttributComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(163, 163, 163)
.addComponent(SuchButton, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(20, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void SuchButtonActionPerformed(java.awt.event.ActionEvent evt) throws SQLException {
String selectedIfcType = (String)ifcComboBox.getSelectedItem();
String selectedLp = (String)LpComboBox.getSelectedItem();
String selectedAttribut = (String)AttributComboBox.getSelectedItem();
String url = "jdbc:mysql://localhost:3306/testtable?";
String user = "Vinzenz";
String password = "11ccffe1b5";
if ( selectedIfcType != null && selectedLp == null && selectedAttribut == null ) {
System.out.println("SELECT * FROM " + selectedIfcType);
try(Connection conn = DriverManager.getConnection(url, user, password)){
String query = "SELECT * FROM " + selectedIfcType;
System.out.println(query);
@SuppressWarnings("rawtypes")
java.sql.Statement st = conn.createStatement();
ResultSet rs = ((java.sql.Statement) st).executeQuery(query);
ResultSetMetaData rsmd = (ResultSetMetaData) rs.getMetaData();
DefaultTableModel model = (DefaultTableModel) tblData.getModel();
int columns = rsmd.getColumnCount();
String[] colName = new String[columns];
for (int i = 0; i < columns; i++)
colName[i] = rsmd.getColumnName(i+1);
model.setColumnIdentifiers(colName);
String id, Leistungsphase, Attribut, Werte;
while (rs.next()) {
id=rs.getString(1);
Leistungsphase=rs.getString(2);
Attribut=rs.getString(3);
Werte=rs.getString(4);
String [] row = {id, Leistungsphase, Attribut, Werte};
model.addRow(row);
}
((java.sql.Statement) st).close();
conn.close();
}
}if ( selectedIfcType != null && selectedLp != null && selectedAttribut == null ) {
System.out.println("SELECT * FROM " + selectedIfcType + " WHERE Leistungsphase = " + selectedLp);
try(Connection conn = DriverManager.getConnection(url, user, password)){
String query = "SELECT * FROM " + selectedIfcType + " WHERE Leistungsphase = " + selectedLp;
System.out.println(query);
@SuppressWarnings("rawtypes")
java.sql.Statement st = conn.createStatement();
ResultSet rs = ((java.sql.Statement) st).executeQuery(query);
ResultSetMetaData rsmd = (ResultSetMetaData) rs.getMetaData();
DefaultTableModel model = (DefaultTableModel) tblData.getModel();
int columns = rsmd.getColumnCount();
String[] colName = new String[columns];
for (int i = 0; i < columns; i++)
colName[i] = rsmd.getColumnName(i+1);
model.setColumnIdentifiers(colName);
String id, Leistungsphase, Attribut, Werte;
while (rs.next()) {
id=rs.getString(1);
Leistungsphase=rs.getString(2);
Attribut=rs.getString(3);
Werte=rs.getString(4);
String [] row = {id, Leistungsphase, Attribut, Werte};
model.addRow(row);
}
((java.sql.Statement) st).close();
conn.close();
}
}if ( selectedIfcType != null && selectedLp != null && selectedAttribut != null ) {
System.out.println("SELECT * FROM " + selectedIfcType + " WHERE Leistungsphase = " + selectedLp + " AND Attribut " + selectedAttribut);
try(Connection conn = DriverManager.getConnection(url, user, password)){
String query = "SELECT * FROM " + selectedIfcType + " WHERE Leistungsphase = " + selectedLp + " AND Attribut " + selectedAttribut;
System.out.println(query);
@SuppressWarnings("rawtypes")
java.sql.Statement st = conn.createStatement();
ResultSet rs = ((java.sql.Statement) st).executeQuery(query);
ResultSetMetaData rsmd = (ResultSetMetaData) rs.getMetaData();
DefaultTableModel model = (DefaultTableModel) tblData.getModel();
int columns = rsmd.getColumnCount();
String[] colName = new String[columns];
for (int i = 0; i < columns; i++)
colName[i] = rsmd.getColumnName(i+1);
model.setColumnIdentifiers(colName);
String id, Leistungsphase, Attribut, Werte;
while (rs.next()) {
id=rs.getString(1);
Leistungsphase=rs.getString(2);
Attribut=rs.getString(3);
Werte=rs.getString(4);
String [] row = {id, Leistungsphase, Attribut, Werte};
model.addRow(row);
}
((java.sql.Statement) st).close();
conn.close();
}
}
}
/**
* @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(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MainFrame.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 MainFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
public javax.swing.JComboBox<String> AttributComboBox;
private javax.swing.JLabel AttributSelectLabel;
public javax.swing.JComboBox<String> LpComboBox;
private javax.swing.JLabel LpSelectLabel;
private javax.swing.JButton SuchButton;
public javax.swing.JComboBox<String> ifcComboBox;
private javax.swing.JLabel ifcSelectLabel;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable tblData;
private javax.swing.JLabel überschrift1;
private javax.swing.JLabel überschrift2;
// End of variables declaration
}