Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. 'Cliente',
  2. 'CREATE TABLE `cliente` (
  3. `ClientId` int(11) NOT NULL,
  4. `ApellidoPaterno` varchar(255) NOT NULL,
  5. `ApellidoMaterno` varchar(255) DEFAULT NULL,
  6. `Nombre` varchar(255) DEFAULT NULL,
  7. `Telefono` mediumtext,
  8. `Username` varchar(255) NOT NULL,
  9. PRIMARY KEY (`ClientId`)
  10. )
  11. ENGINE=InnoDB DEFAULT CHARSET=utf8'
  12.  
  13. package me.jmll.utm.config;
  14.  
  15. import java.sql.*;
  16.  
  17. public class Conexion{
  18.  
  19. public Connection getConexion(){
  20. Connection con = null;
  21. try {
  22. Class.forName("com.mysql.jdbc.Driver");
  23. con = DriverManager.getConnection(
  24. "jdbc:mysql://localhost:3306/miscelanialabety","root","Patito01");
  25. return con;
  26. }
  27. catch(Exception e){
  28. try {throw e;}
  29. catch (Exception e1) { e1.printStackTrace();}}
  30. return con;
  31. }
  32. }
  33.  
  34. @Override
  35. public List<Cliente> getClientes() {
  36. String sql = "SELECT * FROM Cliente";
  37.  
  38. try {
  39. conexion = new Conexion().getConexion();
  40. PreparedStatement ps = conexion.prepareStatement(sql);
  41. Cliente cliente = null;
  42. List<Cliente> listaClientes = new ArrayList<Cliente>();
  43. ResultSet rs = ps.executeQuery();
  44. while (rs.next()) {
  45. cliente = new Cliente(
  46. rs.getInt(1),
  47. rs.getString("ApellidoPaterno"),
  48. rs.getString("ApellidoMaterno"),
  49. rs.getString("Nombre"),
  50. rs.getLong("Telefono"),
  51. rs.getString("Username")
  52. );
  53. listaClientes.add(cliente);
  54. }
  55. rs.close();
  56. ps.close();
  57. return listaClientes;
  58. } catch (SQLException e) {
  59. throw new RuntimeException(e);
  60. } finally {
  61. if (conexion != null) {
  62. try { conexion.close();}
  63. catch (SQLException e) {}
  64. }
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement