Strings vergleichen

Status
Nicht offen für weitere Antworten.

Mundschuk

Mitglied
Hallo,

ich bin's schon wieder 🙁
Ich bin mittlerweile fast fertig mit dem Projekt, ich habe
aber noch ein letztes Problem, wohinter meiner Meinung nach
ein Logikehler meinerseits steckt.

Ich möchte prüfen, ob es in einer Datenbank (SQL-Server)
bereits einen Eintrag gibt, der den Namen hat, der bereits
in einer Tabelle gespeichert wird. Speziell geht es
darum, dass jmd nicht zwei Server speichern kann mit
dem gleichen Namen.

Code:
[SIZE=2][LEFT]btAdd.addActionListener([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ActionListener() {
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] actionPerformed(ActionEvent e) {
String pSQLIdentity = [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"INSERT INTO dbo.ServerIdentification (ServerName, InternalIP, ExternalIP, SNM, Gateway, FirstPort, SecPort, Cage, CPU, RAM, HDD, Purpose, OperatingSystem, InstalledPrograms, InstalledWINFeatures, Roles, Virtualized, Host) VALUES ('"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + tfServerName.getText() + [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"', '"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + tfInternalIP.getText() + [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"', '"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + tfExternalIP.getText() + [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"', '"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + tfSNM.getText() + [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"', '"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + tfGateway.getText() + [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"', '"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + tfFirstPort.getText() + [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"', '"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + tfSecPort.getText() + [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"', '"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + tfCage.getText() + [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"', '"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + tfCPU.getText() + [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"', '"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + tfRAM.getText() + [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"', '"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + tfHDD.getText() + [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"', '"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + tfPurpose.getText() + [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"', '"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + serverMenu.getSelectedItem() + [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"', '"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + taInstalled.getText() + [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"', '"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + taInstFeats.getText() + [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"', '"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + tfRole.getText() + [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"', '"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + String.[I]valueOf[/I](cbVirtYes.getState()) + [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"', '"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + tfHost.getText() + [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"')"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
String pSQLCheck = [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"SELECT * FROM dbo.ServerIdentification WHERE ServerName = '"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + tfServerName.getText() + [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"'"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
DBConnection dbCon = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] DBConnection();

dbCon.DBOpen();
ResultSet rs = dbCon.DBExecute(pSQLCheck);

[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]try[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] {
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]while[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (rs.next()) {
System.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]out[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].println(rs.getString([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"ServerName"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]));
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](rs.getString([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"ServerName"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]) == tfServerName.getText()) {
JOptionPane.[I]showMessageDialog[/I](addServer, [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Der Server ist bereits vorhanden"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Fehler"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], JOptionPane.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]ERROR_MESSAGE[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);
}
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]else[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] {
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]try[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] {
dbCon.DBExecute(pSQLIdentity);
JOptionPane.[I]showMessageDialog[/I](addServer, [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Die Serverdaten wurden erfasst"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Bestätigung"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], JOptionPane.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]INFORMATION_MESSAGE[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);
}
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]catch[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](Exception ex) {
JOptionPane.[I]showMessageDialog[/I](addServer, [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Bei der Speicherung ist ein Fehler aufgetreten"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Fehler"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], JOptionPane.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]ERROR_MESSAGE[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);
ex.printStackTrace();
}
}
}
}
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]catch[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](SQLException ex) {
ex.printStackTrace();
}
dbCon.DBClose();
}[/LEFT]
});

sry, blicke das mit dem highlight nich^^
[/SIZE]
 
Zuletzt bearbeitet:
Ich werd mir heute Abend doch mal wieder das Java-ist-auch-eine-Insel Buch zur Hand nehmen und dann --> :rtfm:

Schau dir das String-Kapitel genauer an. Es wäre nur halb wahr wenn du jetzt denkst dass man Strings nur per equals() vergleichen kann. (Obwohl es stimmt, dass man es nur so tun sollte. Aber es geht u.U auch mit ==)
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben