Advertisement
Guest User

Untitled

a guest
Nov 20th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. package pruebaforoaccesodatos;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.logging.Level;
  9. import java.util.logging.Logger;
  10.  
  11. /**
  12. *
  13. * @author RadW
  14. */
  15. public class PruebaForoAccesoDatos {
  16.  
  17. /* objeto de conexión */
  18. private static Connection conexion = null;
  19. /* comando para enviar sentencias a través de la conexión */
  20. private static Statement sentencia = null;
  21. /* objeto ResultSet para recoger los registros devueltos por la consulta */
  22. private static ResultSet result = null;
  23.  
  24.  
  25. private static final String URL = "jdbc:mysql://sql7.freemysqlhosting.net:3306/sqlYYYYYY";
  26. private static final String USER = "sqlYYYYYY";
  27. private static final String PASSWORD = "XXXXXXX";
  28.  
  29. private static final String ORDEN_CREAR = ""
  30. + "CREATE TABLE infoGente ( "
  31. + "id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY," +
  32. "nombre VARCHAR(30) NOT NULL," +
  33. "apellido VARCHAR(30) NOT NULL," +
  34. ");";
  35.  
  36. private static final String ORDEN_INSERTAR = ""
  37. + "INSERT INTO infoGente (firstname, lastname, email) "
  38. + "VALUES ('John', 'Doe', 'john@example.com'); ";
  39.  
  40. private static final String ORDEN_SELECT = ""
  41. + "SELECT * FROM infoGente;";
  42.  
  43. /**
  44. * @param args the command line arguments
  45. */
  46. public static void main(String[] args) {
  47. try {
  48. // TODO code application logic here
  49.  
  50. /* carga el driver previamente añadido a las 'Bibliotecas' */
  51. Class.forName("com.mysql.jdbc.Driver").newInstance();
  52. /* abre la conexión */
  53. conexion = DriverManager.getConnection(URL, USER, PASSWORD);
  54. /* crea el comando para enviar sentencias SQL */
  55. sentencia = conexion.createStatement();
  56. /* ejecuta */
  57. sentencia.executeUpdate(ORDEN_CREAR);
  58. sentencia.executeUpdate(ORDEN_INSERTAR);
  59. result = sentencia.executeQuery(ORDEN_SELECT);
  60.  
  61. while ( result.next()){
  62. System.out.printf("%4s %10s \n",
  63. result.getInt(1), result.getString(2) );
  64.  
  65. }
  66.  
  67. } catch (ClassNotFoundException ex) {
  68. Logger.getLogger(PruebaForoAccesoDatos.class.getName()).log(Level.SEVERE, null, ex);
  69. } catch (InstantiationException ex) {
  70. Logger.getLogger(PruebaForoAccesoDatos.class.getName()).log(Level.SEVERE, null, ex);
  71. } catch (IllegalAccessException ex) {
  72. Logger.getLogger(PruebaForoAccesoDatos.class.getName()).log(Level.SEVERE, null, ex);
  73. } catch (SQLException ex) {
  74. System.err.println("HA OCURRIDO UNA EXCEPCIÓN");
  75. System.err.println("Mensaje: "+ ex.getMessage());
  76. System.err.println("SQL estado: "+ ex.getSQLState());
  77. System.err.println("Código Error: "+ ex.getErrorCode());
  78. Logger.getLogger(PruebaForoAccesoDatos.class.getName()).log(Level.SEVERE, null, ex);
  79.  
  80. }
  81. }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement