Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- === Main Class ===
- public class CentralServer {
- protected static final Map<Integer, StudentDetails> nameCache = new HashMap<Integer, StudentDetails>();
- public static int name = "";
- public static void main(String[] args) {
- System.out.println("Loading...");
- loadStudentDetails();
- System.out.println("Please enter the student's ID");
- boolean retry = false;
- while (!retry) {
- try {
- name = System.console().readLine();
- final StudentDet newName;
- try {
- newName = nameCache.get(name);
- retry = true;
- } catch (Exception ex) {
- retry = false;
- System.out.println("Invalid input. Please re-enter.");
- }
- } catch (Exception e) {
- System.out.println("Invalid input. Please re-enter.");
- }
- }
- final StringBuilder sb = new StringBuilder("Student Details...");
- sb.append("Name...").append(newName.getName());
- sb.append("Age...").append(newName.getAge());
- System.out.println(sb.toString());
- }
- public static void loadStudentDetails() {
- Connection con = DatabaseConnection.getConnection();
- try {
- PreparedStatement ps = con.prepareStatement("SELECT * FROM `students`");
- ResultSet rs = ps.executeQuery();
- while (rs.next()) {
- StudentDetails info = new StudentDetails();
- int studentID = rs.getInt("id");
- info.name = rs.getInt("name");
- info.age = rs.getByte("age"));
- nameCache.put(studentID, info);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public static class StudentDetails {
- private String name;
- private byte age;
- private NpcInfo() {
- }
- public String getName() {
- return name;
- }
- public byte getAge() {
- return age;
- }
- }
- }
- == Database class ===
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.util.Collection;
- import java.util.LinkedList;
- public class DatabaseConnection {
- // Alter this.
- private static String dbDriver = "com.mysql.jdbc.Driver",
- dbUrl = "jdbc:mysql://localhost:3306/database_name?autoReconnect=true",
- dbUser = "root",
- dbPass = "";
- private static final ThreadLocal<Connection> connections = new ThreadLocalConnection();
- public static final Connection getConnection() {
- return connections.get();
- }
- public static final void closeAll() throws SQLException {
- for (final Connection con : ThreadLocalConnection.allConnections) {
- con.close();
- }
- }
- private static final class ThreadLocalConnection extends ThreadLocal<Connection> {
- public static final Collection<Connection> allConnections = new LinkedList<Connection>();
- @Override
- protected final Connection initialValue() {
- try {
- Class.forName(dbDriver);
- } catch (final ClassNotFoundException e) {
- System.err.println("ERROR" + e);
- }
- try {
- final Connection con = DriverManager.getConnection(dbUrl, dbUser, dbPass);
- allConnections.add(con);
- return con;
- } catch (SQLException e) {
- System.err.println("ERROR" + e);
- return null;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement