Advertisement
Guest User

Untitled

a guest
Sep 14th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.54 KB | None | 0 0
  1.  === Main Class ===
  2.  
  3.  
  4.  
  5. public class CentralServer {
  6.  
  7.     protected static final Map<Integer, StudentDetails> nameCache = new HashMap<Integer, StudentDetails>();
  8.     public static int name = "";
  9.  
  10.     public static void main(String[] args) {
  11.         System.out.println("Loading...");
  12.         loadStudentDetails();
  13.         System.out.println("Please enter the student's ID");
  14.         boolean retry = false;
  15.         while (!retry) {
  16.             try {
  17.                 name = System.console().readLine();
  18.                 final StudentDet newName;
  19.                 try {
  20.                     newName = nameCache.get(name);
  21.                     retry = true;
  22.                 } catch (Exception ex) {
  23.                     retry = false;
  24.                     System.out.println("Invalid input. Please re-enter.");
  25.                 }
  26.             } catch (Exception e) {
  27.                 System.out.println("Invalid input. Please re-enter.");
  28.             }
  29.         }
  30.        
  31.         final StringBuilder sb = new StringBuilder("Student Details...");
  32.         sb.append("Name...").append(newName.getName());
  33.         sb.append("Age...").append(newName.getAge());
  34.        
  35.         System.out.println(sb.toString());
  36.     }
  37.  
  38.     public static void loadStudentDetails() {
  39.         Connection con = DatabaseConnection.getConnection();
  40.         try {
  41.             PreparedStatement ps = con.prepareStatement("SELECT * FROM `students`");
  42.             ResultSet rs = ps.executeQuery();
  43.             while (rs.next()) {
  44.                 StudentDetails info = new StudentDetails();
  45.                 int studentID = rs.getInt("id");
  46.                 info.name = rs.getInt("name");
  47.                 info.age = rs.getByte("age"));
  48.                 nameCache.put(studentID, info);
  49.             }
  50.         } catch (SQLException e) {
  51.             e.printStackTrace();
  52.         }
  53.     }
  54.  
  55.     public static class StudentDetails {
  56.         private String name;
  57.         private byte age;
  58.  
  59.         private NpcInfo() {
  60.         }
  61.  
  62.         public String getName() {
  63.             return name;
  64.         }
  65.  
  66.         public byte getAge() {
  67.             return age;
  68.         }
  69.     }
  70. }
  71.  
  72.  
  73.  
  74. == Database class ===
  75.  
  76. import java.sql.Connection;
  77. import java.sql.DriverManager;
  78. import java.sql.SQLException;
  79. import java.util.Collection;
  80. import java.util.LinkedList;
  81.  
  82. public class DatabaseConnection {
  83.  
  84.     // Alter this.
  85.     private static String dbDriver = "com.mysql.jdbc.Driver",
  86.             dbUrl = "jdbc:mysql://localhost:3306/database_name?autoReconnect=true",
  87.             dbUser = "root",
  88.             dbPass = "";
  89.    
  90.     private static final ThreadLocal<Connection> connections = new ThreadLocalConnection();
  91.  
  92.     public static final Connection getConnection() {
  93.         return connections.get();
  94.     }
  95.  
  96.     public static final void closeAll() throws SQLException {
  97.         for (final Connection con : ThreadLocalConnection.allConnections) {
  98.             con.close();
  99.         }
  100.     }
  101.  
  102.     private static final class ThreadLocalConnection extends ThreadLocal<Connection> {
  103.  
  104.         public static final Collection<Connection> allConnections = new LinkedList<Connection>();
  105.  
  106.         @Override
  107.         protected final Connection initialValue() {
  108.             try {
  109.                 Class.forName(dbDriver);
  110.             } catch (final ClassNotFoundException e) {
  111.                 System.err.println("ERROR" + e);
  112.             }
  113.             try {
  114.                 final Connection con = DriverManager.getConnection(dbUrl, dbUser, dbPass);
  115.                 allConnections.add(con);
  116.                 return con;
  117.             } catch (SQLException e) {
  118.                 System.err.println("ERROR" + e);
  119.                 return null;
  120.             }
  121.         }
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement