Reputation: 1329
I'm having a bit of problem when trying to add things to my database.
If I don't use an if statement such as this, it works:
public void onMessage(String channel, String sender, String login, String hostname, String message) {
String[] splitMessage = message.split(" ", 3);
try {
conn = TwitchBotMain.getConnection();
String query = "insert ignore into commands(name, output) values(?,?)";
pstmt = conn.prepareStatement(query);
pstmt.setString(1, splitMessage[1]);
pstmt.setString(2, splitMessage[2]);
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
However, if I add this if statement to the mix, it doesn't log anything to the database:
public void onMessage(String channel, String sender, String login, String hostname, String message) {
String[] splitMessage = message.split(" ", 3);
if (splitMessage[0] == "!add") {
try {
conn = TwitchBotMain.getConnection();
String query = "insert ignore into commands(name, output) values(?,?)";
pstmt = conn.prepareStatement(query);
pstmt.setString(1, splitMessage[1]);
pstmt.setString(2, splitMessage[2]);
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
Eclipse isn't showing any Java errors.
Upvotes: 2
Views: 37
Reputation: 6138
Always use equals()
instead of ==
when compare string:
if (splitMessage[0].equals("!add"))
Upvotes: 4
Reputation: 12523
try:
if (splitMessage[0].equals("!add")) {
with ==
you compare the reference not the values of your strings.
Upvotes: 2