· 9 years ago · Nov 20, 2016, 08:48 PM
1package pruebaforoaccesodatos;
2
3import java.sql.Connection;
4import java.sql.DriverManager;
5import java.sql.ResultSet;
6import java.sql.SQLException;
7import java.sql.Statement;
8import java.util.logging.Level;
9import java.util.logging.Logger;
10
11/**
12 *
13 * @author RadW
14 */
15public 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/sqlYYYYYYYY";
26 private static final String USER = "sqlYYYYYYYY";
27 private static final String PASSWORD = "XXXXXXXX";
28
29 private static final String ORDEN_CREAR = ""
30 + "CREATE TABLE IF NOT EXISTS 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 (nombre, apellido) "
38 + "VALUES ('John', 'Doe'); ";
39
40 private static final String ORDEN_SELECT = ""
41 + "SELECT * FROM infoGente;";
42
43 public static synchronized void orden(String nombre, String tipo) throws SQLException{
44 if (tipo.equals("update")){
45 sentencia.executeUpdate(nombre);
46 } else {
47 result = sentencia.executeQuery(nombre);
48 }
49 }
50 /**
51 * @param args the command line arguments
52 */
53 public static void main(String[] args) {
54 try {
55 // TODO code application logic here
56
57 /* carga el driver previamente añadido a las 'Bibliotecas' */
58 Class.forName("com.mysql.jdbc.Driver").newInstance();
59 /* abre la conexión */
60
61 conexion = DriverManager.getConnection(URL, USER, PASSWORD);
62
63 /* crea el comando para enviar sentencias SQL */
64 sentencia = conexion.createStatement();
65 /* ejecuta */
66
67 orden(ORDEN_CREAR, "update");
68 orden(ORDEN_INSERTAR, "update");
69 orden(ORDEN_SELECT, "query");
70
71 while ( result.next()){
72 System.out.printf("%4s %10s \n",
73 result.getInt(1), result.getString(2) );
74 }
75
76 } catch (ClassNotFoundException ex) {
77 Logger.getLogger(PruebaForoAccesoDatos.class.getName()).log(Level.SEVERE, null, ex);
78 } catch (InstantiationException ex) {
79 Logger.getLogger(PruebaForoAccesoDatos.class.getName()).log(Level.SEVERE, null, ex);
80 } catch (IllegalAccessException ex) {
81 Logger.getLogger(PruebaForoAccesoDatos.class.getName()).log(Level.SEVERE, null, ex);
82 } catch (SQLException ex) {
83 System.err.println("HA OCURRIDO UNA EXCEPCIÓN");
84 System.err.println("Mensaje: "+ ex.getMessage());
85 System.err.println("SQL estado: "+ ex.getSQLState());
86 System.err.println("Código Error: "+ ex.getErrorCode());
87 Logger.getLogger(PruebaForoAccesoDatos.class.getName()).log(Level.SEVERE, null, ex);
88
89 }
90 }
91
92}