Advertisement
Guest User

Untitled

a guest
Jan 10th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.26 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package classes;
  7.  
  8. import beans.Persona;
  9. import java.io.BufferedWriter;
  10. import java.io.File;
  11. import java.io.FileInputStream;
  12. import java.io.FileOutputStream;
  13. import java.io.FileWriter;
  14. import java.io.IOException;
  15. import java.io.InputStream;
  16. import java.io.OutputStream;
  17. import java.sql.Connection;
  18. import java.sql.DriverManager;
  19. import java.sql.PreparedStatement;
  20. import java.sql.ResultSet;
  21. import java.sql.SQLException;
  22. import java.util.ArrayList;
  23. import java.util.Date;
  24. import java.util.List;
  25. import java.util.Properties;
  26. import java.util.logging.Level;
  27. import java.util.logging.Logger;
  28. import javax.servlet.http.HttpSession;
  29.  
  30. /**
  31. *
  32. * @author Luser
  33. */
  34. public class Validate {
  35.  
  36. private static Validate instance;
  37.  
  38. private static Connection connection;
  39.  
  40. private Validate() {
  41. try {
  42. //loading drivers for mysql
  43. // Class.forName("com.mysql.jdbc.Driver");
  44. Class.forName("org.mariadb.jdbc.Driver");
  45. //creating connection with the database
  46. connection = DriverManager.getConnection("jdbc:mysql://localhost:3307/dbchaol", "dbChaol", "uChaolX");
  47. } catch (ClassNotFoundException ex) {
  48. ex.printStackTrace();
  49. } catch (SQLException ex) {
  50. ex.printStackTrace();
  51. }
  52. }
  53.  
  54. public static Validate getInstance() {
  55. if (instance == null) {
  56. instance = new Validate();
  57. }
  58. return instance;
  59. }
  60.  
  61. // public boolean checkUser(String email, String pass) {
  62. public int checkUser(String email, String pass) {
  63. int st = -100;
  64. ResultSet rs = null;
  65. PreparedStatement ps = null;
  66. try {
  67. ps = connection.prepareStatement("select * from tmlogin where cmloginuser=? and cmloginpass=?");
  68. ps.setString(1, email);
  69. ps.setString(2, pass);
  70. rs = ps.executeQuery();
  71.  
  72. if (rs.next()) {
  73. st = rs.getInt("imloginid");
  74. }
  75.  
  76. } catch (Exception e) {
  77. e.printStackTrace();
  78. if (rs != null) {
  79. try {
  80. rs.close();
  81. } catch (SQLException ex) {
  82. Logger.getLogger(Validate.class.getName()).log(Level.SEVERE, null, ex);
  83. }
  84. }
  85. if (ps != null) {
  86. try {
  87. ps.close();
  88. } catch (SQLException ex) {
  89. Logger.getLogger(Validate.class.getName()).log(Level.SEVERE, null, ex);
  90. }
  91. }
  92.  
  93. }
  94. return st;
  95. }
  96.  
  97. public int checkUserClient(String email, String pass) {
  98. int st = -100;
  99. ResultSet rs = null;
  100. PreparedStatement ps = null;
  101. try {
  102. // PreparedStatement ps = connection.prepareStatement("select * from tcatcli where ccatcliemail=? and ccatclipass=?");
  103. ps = connection.prepareStatement("select * from tcatcli where ccatclicodigo=? and ccatclipass=?");
  104. ps.setString(1, email);
  105. ps.setString(2, pass);
  106. rs = ps.executeQuery();
  107. if (rs.next()) {
  108. st = rs.getInt("icatcliid");
  109. }
  110. } catch (SQLException ex) {
  111. ex.printStackTrace();
  112. Logger.getLogger(Validate.class.getName()).log(Level.SEVERE, null, ex);
  113. } finally {
  114. System.out.println("==================1Cerrando Conexion.........");
  115. if (rs != null) {
  116. try {
  117. rs.close();
  118. } catch (SQLException ex) {
  119. Logger.getLogger(Validate.class.getName()).log(Level.SEVERE, null, ex);
  120. }
  121. }
  122. if (ps != null) {
  123. try {
  124. ps.close();
  125. } catch (SQLException ex) {
  126. Logger.getLogger(Validate.class.getName()).log(Level.SEVERE, null, ex);
  127. }
  128. }
  129. /* if (connection != null) {
  130. try {
  131. connection.close();
  132. } catch (SQLException ex) {
  133. ex.printStackTrace();
  134. }
  135. } */
  136. }
  137. return st;
  138. }
  139.  
  140. public int checkUserChofer(String email, String pass) {
  141. int st = -100;
  142. ResultSet rs = null;
  143. PreparedStatement ps = null;
  144. try {
  145. ps = connection.prepareStatement("select * from tcatchof where ccatchofcodigo=? and ccatchofpass=?");
  146. ps.setString(1, email);
  147. ps.setString(2, pass);
  148. rs = ps.executeQuery();
  149. if (rs.next()) {
  150. st = rs.getInt("icatchofid");
  151. }
  152.  
  153. } catch (SQLException ex) {
  154. ex.printStackTrace();
  155. Logger.getLogger(Validate.class.getName()).log(Level.SEVERE, null, ex);
  156. } finally {
  157. System.out.println("==================1Cerrando Conexion.........");
  158. if (rs != null) {
  159. try {
  160. rs.close();
  161. } catch (SQLException ex) {
  162. Logger.getLogger(Validate.class.getName()).log(Level.SEVERE, null, ex);
  163. }
  164. }
  165. if (ps != null) {
  166. try {
  167. ps.close();
  168. } catch (SQLException ex) {
  169. Logger.getLogger(Validate.class.getName()).log(Level.SEVERE, null, ex);
  170. }
  171. }
  172. /* if (connection != null) {
  173. try {
  174. connection.close();
  175. } catch (SQLException ex) {
  176. ex.printStackTrace();
  177. }
  178. } */
  179. }
  180. return st;
  181. }
  182.  
  183. public int checkUserTransportista(String email, String pass) {
  184. int st = -100;
  185. ResultSet rs = null;
  186. PreparedStatement ps = null;
  187. try {
  188. ps = connection.prepareStatement("select * from tcattransp where ccattranspnomuser=? and ccattranspcontasena=?");
  189. ps.setString(1, email);
  190. ps.setString(2, pass);
  191. rs = ps.executeQuery();
  192. if (rs.next()) {
  193. st = rs.getInt("icattranspid");
  194. }
  195.  
  196. } catch (SQLException ex) {
  197. System.out.println("===Transportista Error ServletLogin catch");
  198. ex.printStackTrace();
  199. Logger.getLogger(Validate.class.getName()).log(Level.SEVERE, null, ex);
  200. } finally {
  201. try {
  202. System.out.println("==================1Cerrando Conexion.........");
  203. if (rs != null) {
  204. try {
  205. System.out.println("===Transportista finally ServletLogin Close RS");
  206. rs.close();
  207. } catch (SQLException ex) {
  208. Logger.getLogger(Validate.class.getName()).log(Level.SEVERE, null, ex);
  209. }
  210. }
  211. if (ps != null) {
  212. try {
  213. System.out.println("===Transportista finally ServletLogin Close PS");
  214. ps.close();
  215. } catch (SQLException ex) {
  216. Logger.getLogger(Validate.class.getName()).log(Level.SEVERE, null, ex);
  217. }
  218. }
  219. if (connection.isClosed()) {
  220. createInstance();
  221. }
  222.  
  223. /* if (connection != null) {
  224. try {
  225. connection.close();
  226. } catch (SQLException ex) {
  227. ex.printStackTrace();
  228. }
  229. } */
  230. } catch (SQLException ex) {
  231. Logger.getLogger(Validate.class.getName()).log(Level.SEVERE, null, ex);
  232. }
  233. }
  234. return st;
  235. }
  236.  
  237. public void createInstance() {
  238. instance = new Validate();
  239. }
  240.  
  241. public String cargaConfiguracionInicialEmailDueno() {
  242. String email1 = "";
  243. Properties prop = new Properties();
  244. InputStream input = null;
  245. String wnomfile = "dbChaol.properties";
  246. try {
  247. //aqui debemos checar r si el archivo existe en caso contrario crearlo
  248. File archivo = new File(wnomfile);
  249. BufferedWriter bw;
  250. if (archivo.exists()) {
  251. input = new FileInputStream(wnomfile);
  252. // load a properties file
  253. prop.load(input);
  254. // get the property value and print it out
  255. System.out.println("PUERTOOOO:" + prop.getProperty("dbport2"));
  256. email1 = prop.getProperty("email1");
  257. } else {
  258. System.out.println("== Error Archivo no existe!!! " + wnomfile);
  259. }
  260. // input = new FileInputStream("config.properties");
  261. } catch (IOException ex) {
  262. ex.printStackTrace();
  263. } finally {
  264. if (input != null) {
  265. try {
  266. input.close();
  267. } catch (IOException e) {
  268. e.printStackTrace();
  269. }
  270. }
  271. }
  272. return email1;
  273. }
  274.  
  275. public void insertPersonal(String codigo, String nombre, Date fechanacimiento) {
  276. ResultSet rs = null;
  277. PreparedStatement ps = null;
  278. try {
  279. ps = connection.prepareStatement("insert into tcat (codigo, nombre, fechnac) values (?,?,?)");
  280. ps.setString(1, codigo);
  281. ps.setString(2, nombre);
  282. ps.setDate(3, new java.sql.Date(fechanacimiento.getTime()));
  283. ps.executeUpdate();
  284. } catch (SQLException ex) {
  285. ex.printStackTrace();
  286.  
  287. if (ps != null) {
  288. try {
  289. ps.close();
  290. } catch (SQLException exx) {
  291. Logger.getLogger(Validate.class.getName()).log(Level.SEVERE, null, exx);
  292. }
  293. }
  294. }
  295. }
  296.  
  297. public List<Persona> getLista() {
  298. List<Persona> lista = new ArrayList<>();
  299. ResultSet rs = null;
  300. PreparedStatement ps = null;
  301. try {
  302. ps = connection.prepareStatement("select * from tcat");
  303. rs = ps.executeQuery();
  304.  
  305. while (rs.next()) {
  306. Persona persona = new Persona(rs.getString("codigo"), rs.getString("nombre"), rs.getDate("fechnac"));
  307. lista.add(persona);
  308. }
  309.  
  310. } catch (Exception e) {
  311. e.printStackTrace();
  312. if (rs != null) {
  313. try {
  314. rs.close();
  315. } catch (SQLException ex) {
  316. Logger.getLogger(Validate.class.getName()).log(Level.SEVERE, null, ex);
  317. }
  318. }
  319. if (ps != null) {
  320. try {
  321. ps.close();
  322. } catch (SQLException ex) {
  323. Logger.getLogger(Validate.class.getName()).log(Level.SEVERE, null, ex);
  324. }
  325. }
  326. }
  327. return lista;
  328. }
  329.  
  330. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement