Conxion with java to database of hostinguer

1
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package parte_conexion;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class conexion {

    private String usuario;
    private String contraseña;
    private String base_datos;
    private Connection co;

    public conexion(String usuario, String contraseña, String base_datos) {
        this.usuario = usuario;
        this.contraseña = contraseña;
        this.base_datos = base_datos;
        this.co = null;

    }

    public void conectar_a_hostinguer() throws ClassNotFoundException, SQLException {

        Connection c = null;
        Class.forName("com.mysql.jdbc.Driver");
        //el metodo getConnection() hay que adaptarlo para conectarlo a mi base de daots de hostinguer
         c = DriverManager.getConnection("jdbc:mysql://localhost:3306/java", getUsuario(), getContraseña());
        Statement stm = co.createStatement();
        System.out.println("Conectado correctamente a la Base de Datos de Hostinguer");
        this.co = c;
    }

    //---------------------------------------------------------------
    /**
     * @return the usuario
     */
    public String getUsuario() {
        return usuario;
    }

    /**
     * @param usuario the usuario to set
     */
    public void setUsuario(String usuario) {
        this.usuario = usuario;
    }

    /**
     * @return the contraseña
     */
    public String getContraseña() {
        return contraseña;
    }

    /**
     * @param contraseña the contraseña to set
     */
    public void setContraseña(String contraseña) {
        this.contraseña = contraseña;
    }

    /**
     * @return the base_datos
     */
    public String getBase_datos() {
        return base_datos;
    }

    /**
     * @param base_datos the base_datos to set
     */
    public void setBase_datos(String base_datos) {
        this.base_datos = base_datos;
    }

    /**
     * @return the co
     */
    public Connection getCo() {
        return co;
    }

    /**
     * @param co the co to set
     */
    public void setCo(Connection co) {
        this.co = co;
    }

}

// I do not know how to link my phpmyadmin database from hostinguer to my java program

    
asked by Selito95 07.07.2017 в 19:42
source

1 answer

0

I usually create my connection in the following way:

package com.empresa;

import java.sql.*;

import javax.swing.JOptionPane;

public class ConexionDB {
    public static String vUsuario;
    public static String vClave;

    @SuppressWarnings("finally")
    public static Connection GetConnection() {
        Connection conexion=null;       
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String servidor = "jdbc:mysql://198.10.10.16:3306/bd";
            String usuarioDB= vUsuario;
            String passwordDB= vClave;
            conexion= DriverManager.getConnection(servidor,usuarioDB,passwordDB);
        }
        catch(ClassNotFoundException ex) {
            JOptionPane.showMessageDialog(null, ex, "CLASE NO ENCONTRADA", JOptionPane.ERROR_MESSAGE);
            conexion=null;
        }
        catch(SQLException ex) {
            JOptionPane.showMessageDialog(null, "USUARIO O CONTRASEÑA NO COINCIDEN!", "NO SE PUDO ESTABLECER LA CONEXIÓN", JOptionPane.ERROR_MESSAGE);
            conexion=null;
        }
        catch(Exception ex) {
            JOptionPane.showMessageDialog(null, ex, "EXCEPCIÓN GENERAL OCURRIDA", JOptionPane.ERROR_MESSAGE);
            conexion=null;
        }
        finally {
            return conexion;
        }
    }
}

To get the connection, all you have to do is set the user and password for the class and then get the connection:

ConexionDB.vUsuario = txtUsuario.getText();
ConexionDB.vClave = txtClave.getText(); 
Connection conexion = ConexionDB.GetConnection();

I hope it helps you with the doubts you have.

EYE Do not forget to add the corresponding jar to the database manager driver!

    
answered by 07.07.2017 в 21:45