dastool
Erfahrenes Mitglied
Moin,
ich versuch schon seit zwei Tagen einen Eintrag der in einem JTable gemacht wurde an meine MySQL datenbank zu senden.
Leider wird bei einer Änderung in der Tabelle nicht das geänderte ausgelesen, sondern der Inhalt der zuvor in der jeweiligen Zelle stand.
Der bisherige Code ist:
//Edit
Hat sich nun alles erledigt.
Ich hab den TableModelListener rausgeworfen und bekomme nun den neuen Inhalt der jeweiligen Zelle via ListSelectionListener.
ich versuch schon seit zwei Tagen einen Eintrag der in einem JTable gemacht wurde an meine MySQL datenbank zu senden.
Leider wird bei einer Änderung in der Tabelle nicht das geänderte ausgelesen, sondern der Inhalt der zuvor in der jeweiligen Zelle stand.
Der bisherige Code ist:
Code:
/*
* Main.java
*
* Created on 6. September 2007, 23:23
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package administratortsystem;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
import java.util.Vector;
/**
*
* @author tool
*/
public class Main extends JFrame implements ActionListener, TableModelListener{
private JButton bSuchen, bAendern, bDel;
private JComboBox jbSuchen;
private JLabel jLabel3;
private JScrollPane jScrollPane1;
private JSeparator jSeparator1;
private JTable table;
private JTextField jtfName, jtSuchen;
private DefaultTableModel defTM;
private int h,j;
private Object obj;
public final static Vector COLUMN_IDENTIFIERS = new Vector() {
{
add("IDTicket");
add("Person");
add("Abteilung");
add("Telefon");
add("Email");
add("Hardware");
add("Software");
add("SWProblem");
add("Dringlichkeit");
add("Status");
}
};
public Main() {
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setTitle("Administrator");
jLabel3 = new JLabel();
table = new JTable();
jtfName = new JTextField();
jtSuchen = new JTextField();
jtfName.setColumns(10);
jtSuchen.setColumns(10);
jSeparator1 = new JSeparator();
jbSuchen = new JComboBox();
bSuchen = new JButton();
bAendern = new JButton();
bDel = new JButton();
jScrollPane1 = new JScrollPane();
jLabel3.setText("Suchoption");
jtfName.setText("root");
jtSuchen.setText("1");
bSuchen.setText("Suchen");
bAendern.setText("Eintr\u00e4ge \u00e4ndern");
bDel.setText("Eintrag l\u00f6schen");
defTM = new DefaultTableModel(1,0);
defTM.setColumnIdentifiers(COLUMN_IDENTIFIERS);
table = new JTable(defTM);
jbSuchen.setModel(new DefaultComboBoxModel(new String[] { "ID", "Name", "Abteilung", "Status", "Software", "Hardware", "Dringlichkeit", "E-Mail", "Telefon", "SW-Problem" }));
bSuchen.setActionCommand("Suchen");
bAendern.setActionCommand("Ändern");
bDel.setActionCommand("Löschen");
bSuchen.addActionListener(this);
bAendern.addActionListener(this);
bDel.addActionListener(this);
defTM.addTableModelListener(this);
//"ID", "Name", "Abteilung", "Telefon", "Email", "Hardware", "Software", "SW-Problem" , "Dringlichkeit", "Status", "Anliegen"
jScrollPane1.setViewportView(table);
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
.addComponent(jScrollPane1, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 900, Short.MAX_VALUE)
.addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(jLabel3)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jbSuchen, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(6, 6, 6)
.addComponent(jtSuchen, GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(bSuchen)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(bAendern)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(bDel)
.addGap(500, 500, 500))
.addComponent(jSeparator1, GroupLayout.DEFAULT_SIZE, 900, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(jbSuchen, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(jtSuchen, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(bSuchen)
.addComponent(bAendern)
.addComponent(bDel))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jSeparator1, GroupLayout.PREFERRED_SIZE, 10, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, GroupLayout.DEFAULT_SIZE, 600, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new Main().setVisible(true);
}
});
}
public void actionPerformed(ActionEvent e){
String jb = selectCB(jbSuchen);
String suchText = jtSuchen.getText();
String sql = "";
String user = jtfName.getText().toString();
String pw = "";
int i = 0;
if((jbSuchen.getSelectedItem().toString() == "ID")||(jbSuchen.getSelectedItem().toString() == "Hardware")||(jbSuchen.getSelectedItem().toString() == "Software")||(jbSuchen.getSelectedItem().toString() == "Dringlichkeit")|| (jbSuchen.getSelectedItem().toString() == "Status")){
i = Integer.parseInt(jtSuchen.getText());
suchText = Integer.toString(i);
}else{
suchText = "\u0027"+suchText+"\u0027";
}
if ("Suchen".equals(e.getActionCommand())){
sql = "select * from ticket where"+jb+"="+suchText+" order by dringlichkeit;";
try {
String driver = "com.mysql.jdbc.Driver";
String url = "";
try {
url = lesen();
} catch( FileNotFoundException eof ) {
String ticketnr = "Datei nicht gefunden, \n" +
"bitte Informieren Sie ihren Administrator";
JDialog d1 = new JDialog();
JLabel nummer = new JLabel();
nummer.setText(ticketnr);
d1.add(nummer);
}
Class.forName(driver);
Connection con;
con = DriverManager.getConnection(url, user, pw);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()){
defTM.addRow( new Object[]{rs.getString("IDTicket"),rs.getString("Person"),rs.getString("Abteilung"),rs.getString("Telefon"),rs.getString("Email"),rs.getString("Hardware"),rs.getString("Software"),rs.getString("SWProblem"),rs.getString("Dringlichkeit"),rs.getString("Status")});
}
rs.close();
stmt.close();
con.close();
} catch(Exception ex) {
ex.printStackTrace();
}
}
if (e.getActionCommand()=="Ändern") {
}
if (e.getActionCommand()=="Löschen"){
}
}
public void tableChanged(TableModelEvent e){
}
private String tableChange(){
String s = null;
h = table.getSelectedRow();
j = table.getSelectedColumn();
obj = defTM.getValueAt(h,j);
s = obj.toString();
System.out.println(s);
return s;
}
private String tauglich(String untauglich){
String neu = "`";
String neu2 = neu + untauglich + neu;
return neu2;
}
private String selectCB(JComboBox jb1){
String jb2 = jb1.getSelectedItem().toString();
if (jb2 == "ID"){
jb2 = "IDTicket";
}
if (jb2 == "Name"){
jb2 = "Person";
}
if (jb2 == "SW-Problem"){
jb2 = "swproblem";
}
jb2 = tauglich(jb2);
return jb2;
}
private static String lesen()
throws FileNotFoundException, IOException {
String line, inputstr = "";
try{
BufferedReader in = new BufferedReader(new FileReader("server.txt"));
line = in.readLine();
inputstr = line;
in.close();
} catch( FileNotFoundException e ) {
String ticketnr = "Datei nicht gefunden, bitte Informieren Sie ihren Administrator";
JFrame frame = new JFrame();
frame.setTitle("Ticketnummer");
frame.setSize(200, 200);
frame.setLocation(350, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JLabel nummer = new JLabel();
nummer.setText(ticketnr);
frame.add(nummer);
}
return inputstr;
}
}
//Edit
Hat sich nun alles erledigt.
Ich hab den TableModelListener rausgeworfen und bekomme nun den neuen Inhalt der jeweiligen Zelle via ListSelectionListener.
Zuletzt bearbeitet: