Yea, you didn't close the statements. Also, I suggest you to cache them all and only reload it whenever a news has been added. If you didn't do so and your server has huge amount of players, and if players spam click that NPC, you'll have huge loads on your database.
EDIT* Here, Cached in a list where you load it upon startup. It will only reload it when you add or delete news. Not tested yet, but it should work.
MapleNews.java
PHP Code:
package server;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import util.DatabaseConnection;
/**
*
* @author AuroX
*/
public class MapleNews {
private static MapleNews instance = new MapleNews();
private List<MapleNewsInfo> news = new LinkedList<MapleNewsInfo>();
public static MapleNews getInstance() {
return instance;
}
public List<MapleNewsInfo> getNews() {
if (news.isEmpty()) {
reload();
}
return news;
}
public void reload() {
news.clear();
try {
Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("SELECT * FROM custom_news ORDER BY `id` DESC LIMIT 5");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
final MapleNewsInfo rank = new MapleNewsInfo(rs.getString("title"), rs.getString("date"), rs.getString("message"));
news.add(rank);
}
ps.close();
rs.close();
} catch (SQLException e) {
System.err.println("[MapleNews] Error retrieving data..." + e);
}
}
public static class MapleNewsInfo {
private String title, date, message;
public MapleNewsInfo(String title, String date, String message) {
this.title = title;
this.date = date;
this.message = message;
}
public String getTitle() {
return title;
}
public String getDate() {
return date;
}
public String getMessage() {
return message;
}
}
}
NPC function
PHP Code:
public String getCustomNews() {
StringBuilder ret = new StringBuilder();
final List<MapleNewsInfo> news = MapleNews.getInstance().getNews();
for (MapleNewsInfo info : news) {
ret.append("\r\n#e");
ret.append(info.getTitle());
ret.append(" - (");
ret.append(info.getDate());
ret.append(")#n\r\n");
ret.append(info.getMessage());
ret.append("\r\n");
}
return ret.toString();
}
Command
PHP Code:
} else if (splitted[0].equals("!news")) { // !news <title> <message>
if (splitted.length < 3 || splitted[1] == null || splitted[1].equals("") || splitted[2] == null || splitted[2].equals("")) {
c.getPlayer().dropMessage(6, "!news <title> <message>");
return;
}
try {
Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("INSERT INTO `custom_news` (title, message, date) VALUES (?, ?, ?)");
ps.setString(1, splitted[1]);
ps.setString(2, StringUtil.joinStringFrom(splitted, 2));
ps.setString(3, KoreanDateUtil.getDate("dd/MM/yy"));
ps.executeUpdate();
ps.close();
MapleNews.getInstance().reload();
} catch (SQLException e) {
c.getPlayer().dropMessage(6, "[Error] Unable to save news!");
System.out.println("[MapleNews] Unable to save news..." + e);
}
} else if (splitted[0].equals("!deletenews")) { // !deletenews <id>
if (splitted.length < 2 || splitted[1] == null || splitted[1].equals("")) {
c.getPlayer().dropMessage(6, "!deletenews <id>");
return;
}
final int newsID = Integer.parseInt(splitted[1]);
try {
Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("DELETE FROM `custom_news` WHERE `newsid` = ?");
ps.setInt(1, newsID);
ps.execute();
ps.close();
MapleNews.getInstance().reload();
} catch (SQLException e) {
c.getPlayer().dropMessage(6, "[Error] Unable to delete news!");
System.out.println("[MapleNews] Unable to delete news..." + e);
}
ChannelServer add this where you load your data
PHP Code:
MapleNews.getInstance().getNews();