Database connection error

0

I want to connect a database to java but it gives me a mistake and I do not know what it is I hope you can help me

The error is this:

Here is the code:

And here the BD:

    
asked by TheRiks17 26.11.2017 в 19:54
source

2 answers

1

I do not know if it serves you but in the password give a space to the quotes and localhost: 3306 remove the 3306 with localhost is enough.

    
answered by 27.11.2017 в 02:56
1

Here I put a connection code for one of my projects that works very well.

However, two things struck me:

1) Localhost: 3306 , I would leave it in localhost .

2) In the connection url you declare the DB with capital letters Hardware, and that I know whenever I create a new DB in capital letters, the DB engine passes it to me in lowercase. Also in the error I see that says hardware with lowercase.

I would say if you dare to copy this code as it is and paste it, give it run to whatever you are doing, and if it does not change the name of the DB and the url too.

This last I say at the end because it is unlikely but nothing is impossible, I think it should be a configuration issue of each DB engine, so not all act in that way.

Here I leave the code:

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

public class Conexion {

    private final static String user = "root";
    private final static String password = "";
    static Connection connection = null;

    public static Connection getConexion() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {

        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection("jdbc:mysql://localhost/ferreteria", user, password);

        return connection;
    }
}

I also give you an example just in case just as an annex, an insertion to DB, calling that connection directly from the class, since it is a static method.

import java.sql.Connection;
import java.sql.PreparedStatement;
import com.soa.dao.Conexion;

public class ClientController {

    /* Método para insertar a la DB */
    public static void exampleInsertQuery(String name, String lastname) {
        Connection connection = null;
        /* Intentamos conectarnos */
        try {
            connection = Conexion.getConexion();

            /* Si no es nula que entre al método que nos facilita realizar la insercción */
            if (connection != null) {
                PreparedStatement ps;
                String sql = "INSERT INTO clients(name, lastname) VALUES(?,?)";
                ps = connection.prepareStatement(sql);
                ps.setString(1, name);
                ps.setString(2, lastname);
                ps.executeUpdate();
                ps.close();
                System.out.println("Query executed");
            } else {
                System.out.println("Connection appears to be null");
            }
        } catch (Exception error) {
            System.out.println("Cannot even connect");
            error.getMessage();
            error.printStackTrace();
        }
    }
}
    
answered by 27.11.2017 в 03:53