Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pruebaforoaccesodatos;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author RadW
- */
- public class PruebaForoAccesoDatos {
- /* objeto de conexión */
- private static Connection conexion = null;
- /* comando para enviar sentencias a través de la conexión */
- private static Statement sentencia = null;
- /* objeto ResultSet para recoger los registros devueltos por la consulta */
- private static ResultSet result = null;
- private static final String URL = "jdbc:mysql://sql7.freemysqlhosting.net:3306/sqlYYYYYY";
- private static final String USER = "sqlYYYYYY";
- private static final String PASSWORD = "XXXXXXX";
- private static final String ORDEN_CREAR = ""
- + "CREATE TABLE infoGente ( "
- + "id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY," +
- "nombre VARCHAR(30) NOT NULL," +
- "apellido VARCHAR(30) NOT NULL," +
- ");";
- private static final String ORDEN_INSERTAR = ""
- + "INSERT INTO infoGente (firstname, lastname, email) "
- + "VALUES ('John', 'Doe', 'john@example.com'); ";
- private static final String ORDEN_SELECT = ""
- + "SELECT * FROM infoGente;";
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- try {
- // TODO code application logic here
- /* carga el driver previamente añadido a las 'Bibliotecas' */
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- /* abre la conexión */
- conexion = DriverManager.getConnection(URL, USER, PASSWORD);
- /* crea el comando para enviar sentencias SQL */
- sentencia = conexion.createStatement();
- /* ejecuta */
- sentencia.executeUpdate(ORDEN_CREAR);
- sentencia.executeUpdate(ORDEN_INSERTAR);
- result = sentencia.executeQuery(ORDEN_SELECT);
- while ( result.next()){
- System.out.printf("%4s %10s \n",
- result.getInt(1), result.getString(2) );
- }
- } catch (ClassNotFoundException ex) {
- Logger.getLogger(PruebaForoAccesoDatos.class.getName()).log(Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- Logger.getLogger(PruebaForoAccesoDatos.class.getName()).log(Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- Logger.getLogger(PruebaForoAccesoDatos.class.getName()).log(Level.SEVERE, null, ex);
- } catch (SQLException ex) {
- System.err.println("HA OCURRIDO UNA EXCEPCIÓN");
- System.err.println("Mensaje: "+ ex.getMessage());
- System.err.println("SQL estado: "+ ex.getSQLState());
- System.err.println("Código Error: "+ ex.getErrorCode());
- Logger.getLogger(PruebaForoAccesoDatos.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement