Advertisement
Guest User

Untitled

a guest
May 5th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.94 KB | None | 0 0
  1. import java.io.InputStreamReader;
  2. import java.sql.DriverManager;
  3. import java.util.LinkedList;
  4. import java.io.BufferedReader;
  5. import java.io.FileReader;
  6. import java.sql.Connection;
  7.  
  8. public class Diagnostico {
  9.  
  10. private final String DATAFILE = "data/disease_data.data";
  11. private Connection conn = null;
  12.  
  13. private void showMenu() {
  14.  
  15. int option = -1;
  16. do {
  17. System.out.println("Bienvenido a sistema de diagnóstico\n");
  18. System.out.println("Selecciona una opción:\n");
  19. System.out.println("\t1. Creación de base de datos y carga de datos.");
  20. System.out.println("\t2. Realizar diagnóstico.");
  21. System.out.println("\t3. Listar síntomas de una enfermedad.");
  22. System.out.println("\t4. Listar enfermedades y sus códigos asociados.");
  23. System.out.println("\t5. Listar síntomas existentes en la BD y su tipo semántico.");
  24. System.out.println("\t6. Mostrar estadísticas de la base de datos.");
  25. System.out.println("\t7. Salir.");
  26. try {
  27. option = readInt();
  28. switch (option) {
  29. case 1:
  30. crearBD();
  31. break;
  32. case 2:
  33. realizarDiagnostico();
  34. break;
  35. case 3:
  36. listarSintomasEnfermedad();
  37. break;
  38. case 4:
  39. listarEnfermedadesYCodigosAsociados();
  40. break;
  41. case 5:
  42. listarSintomasYTiposSemanticos();
  43. break;
  44. case 6:
  45. mostrarEstadisticasBD();
  46. break;
  47. case 7:
  48. exit();
  49. break;
  50. }
  51. } catch (Exception e) {
  52. System.err.println("Opción introducida no válida!");
  53. }
  54. } while (option != 7);
  55. exit();
  56. }
  57.  
  58. private void exit() {
  59. if (conn != null){
  60. try{
  61. if(!conn.isClosed())
  62. conn.close();
  63. }
  64. catch (Exception e){
  65. System.out.println("No se pudo desconectar de la base de datos");
  66. }
  67. }
  68. System.out.println("Saliendo.. ¡hasta otra!");
  69. System.exit(0);
  70. }
  71.  
  72. private void conectar() throws Exception {
  73. String drv ="com.mysql.jdbc.Driver";
  74. Class.forName(drv).newInstance();
  75.  
  76. String serverAddress = "localhost:3306";
  77. String db = "diagnostico";
  78. String user = "bddx";
  79. String pwd = "bddx_pwd";
  80. String url = "jdbc:mysql://" + serverAddress + "/" + db;
  81. conn = DriverManager.getConnection(url, user, pwd);
  82. if (!conn.isClosed()){
  83. System.out.println("¡Conectado a la base de datos!");
  84. }
  85. }
  86.  
  87. private void crearBD() {
  88. try {
  89. if (conn == null || conn.isClosed())
  90. this.conectar();
  91. }
  92. catch (Exception e) {
  93. System.out.println(e);
  94. }
  95. /* Lo que tenga que hacer */
  96.  
  97. }
  98.  
  99. private void realizarDiagnostico() {
  100. try {
  101. if (conn == null || conn.isClosed())
  102. this.conectar();
  103. }
  104. catch (Exception e) {
  105. System.out.println(e);
  106. }
  107. /* Lo que tenga que hacer */
  108.  
  109. }
  110.  
  111. private void listarSintomasEnfermedad() {
  112. try {
  113. if (conn == null || conn.isClosed())
  114. this.conectar();
  115. }
  116. catch (Exception e) {
  117. System.out.println(e);
  118. }
  119. /* Lo que tenga que hacer */
  120.  
  121. }
  122.  
  123. private void listarEnfermedadesYCodigosAsociados() {
  124. try {
  125. if (conn == null || conn.isClosed())
  126. this.conectar();
  127. }
  128. catch (Exception e) {
  129. System.out.println(e);
  130. }
  131. /* Lo que tenga que hacer */
  132.  
  133. }
  134.  
  135. private void listarSintomasYTiposSemanticos() {
  136. try {
  137. if (conn == null || conn.isClosed())
  138. this.conectar();
  139. }
  140. catch (Exception e) {
  141. System.out.println(e);
  142. }
  143. /* Lo que tenga que hacer */
  144.  
  145. }
  146.  
  147. private void mostrarEstadisticasBD() {
  148. try {
  149. if (conn == null || conn.isClosed())
  150. this.conectar();
  151. }
  152. catch (Exception e) {
  153. System.out.println(e);
  154. }
  155. /* Lo que tenga que hacer */
  156.  
  157. }
  158.  
  159. /**
  160. * Método para leer números enteros de teclado.
  161. *
  162. * @return Devuelve el número leído.
  163. * @throws Exception
  164. * Puede lanzar excepción.
  165. */
  166. private int readInt() throws Exception {
  167. try {
  168. System.out.print("> ");
  169. return Integer.parseInt(new BufferedReader(new InputStreamReader(System.in)).readLine());
  170. } catch (Exception e) {
  171. throw new Exception("Not number");
  172. }
  173. }
  174.  
  175. /**
  176. * Método para leer cadenas de teclado.
  177. *
  178. * @return Devuelve la cadena leída.
  179. * @throws Exception
  180. * Puede lanzar excepción.
  181. */
  182. private String readString() throws Exception {
  183. try {
  184. System.out.print("> ");
  185. return new BufferedReader(new InputStreamReader(System.in)).readLine();
  186. } catch (Exception e) {
  187. throw new Exception("Error reading line");
  188. }
  189. }
  190.  
  191. /**
  192. * Método para leer el fichero que contiene los datos.
  193. *
  194. * @return Devuelve una lista de String con el contenido.
  195. * @throws Exception
  196. * Puede lanzar excepción.
  197. */
  198. private LinkedList<String> readData() throws Exception {
  199. LinkedList<String> data = new LinkedList<String>();
  200. BufferedReader bL = new BufferedReader(new FileReader(DATAFILE));
  201. while (bL.ready()) {
  202. data.add(bL.readLine());
  203. }
  204. bL.close();
  205. return data;
  206. }
  207.  
  208. public static void main(String args[]) throws Exception {
  209.  
  210. new Diagnostico().showMenu();
  211. }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement