Advertisement
andresdiaz

Untitled

Jan 22nd, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.85 KB | None | 0 0
  1. package Modelo;
  2.  
  3. import Modelo.beans.Equipos;
  4. import Modelo.beans.Usuarios;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.PreparedStatement;
  8. import java.sql.SQLException;
  9. import java.sql.ResultSet;
  10. import java.sql.Statement;
  11. import java.util.ArrayList;
  12. import java.util.logging.Level;
  13. import java.util.logging.Logger;
  14.  
  15. /**
  16. *
  17. * @author andres
  18. */
  19. public class Conexion {
  20. private String driver;
  21. private String url;
  22. private String user;
  23. private String pass;
  24. private Connection cn;
  25.  
  26. public Conexion() throws SQLException{
  27.  
  28. try {
  29. driver = "com.mysql.jdbc.Driver";
  30. url = "jdbc:mysql://localhost:3306/gestorconectividad";
  31. user = "root";
  32. pass = "";
  33.  
  34.  
  35. Class.forName(driver);
  36. cn = DriverManager.getConnection(url,user,pass);
  37. } catch (ClassNotFoundException ex) {
  38. Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
  39. }
  40.  
  41.  
  42.  
  43. }
  44.  
  45. public void cerrarConexion(){
  46. try {
  47. cn.close();
  48. } catch (SQLException ex) {
  49. Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
  50. }
  51.  
  52. }
  53.  
  54. public boolean registrarUsuarios(String nombre, String cedula, String centro, String correoelectronico, String correomisena, String lugarresidencia, String direccion, String telefono, String rol) {
  55.  
  56.  
  57. try {
  58. String sql = "Insert into usuarios(nombre,cedula,centro,correoelectronico,correomisena,lugarresidencia,direccion,telefono,rol)"
  59. + " values ('"+nombre+"','"+cedula+"','"+centro+"','"+correoelectronico+"','"+correomisena+"','"+lugarresidencia+"','"+direccion+"','"+telefono+"', '"+rol+"');";
  60. System.out.println(sql);
  61. Statement st = cn.createStatement();
  62. st.executeUpdate(sql);
  63. return true;
  64.  
  65. } catch (SQLException ex) {
  66. Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
  67. }
  68. return false;
  69.  
  70. }
  71.  
  72. public String ingresarSistema(String nombre, String password){
  73.  
  74. String sql = "select * from usuarios where nombre = '"+nombre+"' and cedula = '"+password+"';";
  75. try {
  76. PreparedStatement st = cn.prepareStatement(sql);
  77. ResultSet rs = st.executeQuery(sql);
  78.  
  79. if (rs.next()) {
  80. String rol = rs.getString("rol");
  81. return rol;
  82.  
  83.  
  84. } else {
  85. return null;
  86. }
  87.  
  88. } catch (SQLException ex) {
  89. Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
  90. }
  91. return null;
  92. }
  93. public ArrayList<Usuarios> mostrarUsuarios(){
  94.  
  95. try {
  96. ArrayList<Usuarios>user = new ArrayList<Usuarios>();
  97. String sql = "select * from usuarios;";
  98. PreparedStatement st = cn.prepareStatement(sql);
  99. ResultSet rs = st.executeQuery(sql);
  100.  
  101. while (rs.next()) {
  102. Usuarios usuarios = new Usuarios();
  103. usuarios.setNombre(rs.getString("nombre"));
  104. usuarios.setCedula(rs.getString("cedula"));
  105. usuarios.setCentro(rs.getString("centro"));
  106. usuarios.setCorreoelectronico(rs.getString("correoelectronico"));
  107. usuarios.setCorreomisena(rs.getString("correomisena"));
  108. usuarios.setLugarresidencia(rs.getString("lugarresidencia"));
  109. usuarios.setDireccion(rs.getString("direccion"));
  110. usuarios.setTelefono(rs.getString("telefono"));
  111. user.add(usuarios);
  112. }
  113. return user;
  114.  
  115.  
  116.  
  117.  
  118. } catch (SQLException ex) {
  119. Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
  120. }
  121. return null;
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128. }
  129. public boolean registrarEquipos(String serial, String marca , String tipo){
  130.  
  131. try {
  132. String sql = "Insert into equipos(Serial,marca,tipo) values ('"+serial+"', '"+marca+"', '"+tipo+"');";
  133.  
  134. Statement st = cn.createStatement();
  135. st.executeUpdate(sql);
  136. return true;
  137. } catch (SQLException ex) {
  138. Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
  139. }
  140. return false;
  141.  
  142.  
  143. }
  144.  
  145. public ArrayList<Equipos> mostrarEquipos(){
  146.  
  147. try {
  148. ArrayList<Equipos> equipos = new ArrayList<Equipos>();
  149. String sql = "select * from equipos;";
  150. PreparedStatement st = cn.prepareStatement(sql);
  151. ResultSet rs = st.executeQuery(sql);
  152.  
  153. while (rs.next()) {
  154. Equipos lista = new Equipos();
  155. lista.setMarca(rs.getString("Serial"));
  156. lista.setSerial(rs.getString("marca"));
  157. lista.setTipo(rs.getString("tipo"));
  158. equipos.add(lista);
  159.  
  160. }
  161. return equipos;
  162. } catch (SQLException ex) {
  163. Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
  164. }
  165. return null;
  166.  
  167.  
  168.  
  169. }
  170. public boolean eliminarEquipo(String eliminar){
  171. try {
  172. String sql = "delete from equipos where id = '"+eliminar+"';";
  173.  
  174. PreparedStatement st = cn.prepareStatement(sql);
  175. st.executeUpdate(sql);
  176. return true;
  177. } catch (SQLException ex) {
  178. Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
  179. }
  180. return false;
  181.  
  182.  
  183. }
  184.  
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement