Guest User

Untitled

a guest
Jun 27th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. public class Conexion {
  2.  
  3. /**
  4. * Crea una instancia de la clase MySQL y realiza todo el código
  5. * de conexión, consulta y muestra de resultados.
  6. */
  7. public Conexion()
  8. {
  9. // Se mete todo en un try por los posibles errores de MySQL
  10. try
  11. {
  12. // Se registra el Driver de MySQL
  13. DriverManager.registerDriver(new org.gjt.mm.mysql.Driver());
  14. [![En la imagen me muestra los datos enteros, pero debajo de cada cantidad me reduce la cantidad a la primera cifra][1]][1]
  15. // Se obtiene una conexión con la base de datos. Hay que
  16. // cambiar el usuario "root" y la clave "la_clave" por las
  17. // adecuadas a la base de datos que estemos usando.
  18. Connection conexion = DriverManager.getConnection ("jdbc:mysql://localhost:3306/BD","root", "");
  19.  
  20.  
  21.  
  22. // Se crea un Statement, para realizar la consulta
  23. Statement s = conexion.createStatement();
  24.  
  25. // Se realiza la consulta. Los resultados se guardan en el
  26. ResultSet rs;
  27. rs = s.executeQuery ("select * from cfe ");
  28.  
  29. // Se recorre el ResultSet, mostrando por pantalla los resultados.
  30. // while (rs.next())
  31. // {
  32. // System.out.println (rs.getInt ("Id") + " " + rs.getString (2)+
  33. // " " + rs.getString(3)+ " " + rs.getString(4)+ " " + rs.getString(5)
  34. // + " " + rs.getString(6)+ " " + rs.getString(7)+ " " + rs.getString(8)+ " " + rs.getString(9));
  35. // }
  36.  
  37. while (rs.next()){
  38. if (rs.getInt(6) !=30) {
  39. System.out.println(rs.getString(7));
  40. double i = Double.valueOf(rs.getInt(7)).intValue();
  41. System.out.println(i); }
  42. }
  43.  
  44. // Se cierra la conexión con la base de datos.
  45. conexion.close();
  46. }
  47. catch (Exception e)
  48. {
  49. e.printStackTrace();
  50. }
  51. }
  52.  
  53. /**
  54. * Método principal, instancia una clase PruebaMySQL
  55. *
  56. * @param args the command line arguments
  57. */
  58. public static void main(String[] args)
  59. {
  60. new Conexion();
  61. }
  62. }
Add Comment
Please, Sign In to add comment