I want to split up an input by the user and insert only the second element and then every element after that. Don't worry about any of the database code, it all works fine.
Basically the user is going to input !add name output If it sees !add, its going to log the name and output to the database. The name is one word but the output can be a string of many words. The problem is when I split the string the output becomes too much to manage and I don't know how to insert it into the database.
This is what I have so far
public void onMessage(message) { String[] splitMessage = message.split(" "); if (splitMessage[0] == "!add") { try { conn = connectionMain.getConnection(); String query = "insert ignore into commands(name, output) values(?,?)"; pstmt = conn.prepareStatement(query); pstmt.setString(1, splitMessage[1]); pstmt.setString(2, /*Question*/); pstmt.executeUpdate(); /* Catch/Finally Statements Below */ } } }
message.split(" ",3)instead ofmessage.split(" ")and then usesplitMessage[2]to set your second parameter. See docs.oracle.com/javase/8/docs/api/java/lang/… but take care that the resulting array is only guaranteed to have at most 3 elements (ie: it could have less)