Connection database in JSP

1

I am trying to connect to the database using JSP and in eclipse, but I get an error.

I have imported the MySQL libraries, I have put connectors .. and it still does not work, this is the code:

<%@page import="java.util.*"%>
<%@page import="java.sql.*"%> 
<%@page import="java.io.*"%>  
<%
    //Parámetro
    String nombre = request.getParameter("nombre-form");
    String apellidos = request.getParameter("apellidos-form");
    String edad = request.getParameter("edad-form");    

    String mensaje = nombre + " " + apellidos + " " + edad;


    Connection con = null;
    Statement set = null;
    try{
        Class.forName("com.mysql.jdbc.Driver");
        con = (Connection)DriverManager.getConnection("jdbc:mysql://localhost/personas","root", "root");
        set = con.createStatement();
        System.out.print("La conexion se ha aprobado");

    }catch(Exception e){
        System.out.println("Error en acceso a base de datos" + e);
    }

When I try to connect it does not enter me by try , it makes me catch , The error that comes to me is the following:

  

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

    
asked by Csc99 22.05.2018 в 21:16
source

1 answer

0

The error is very clear. You are trying to use the com.mysql.jdbc.Driver class and none of your import is included. Surely, in addition, you do not have it in the bookstores of your project either.

As with any "3rd-party" library that is used as a JAR file in a webapp, you only need to copy / paste the JAR file into the / WEB-INF / lib folder of your application. This will be available in the default classpath of the application. In addition, Eclipse is able to detect the new library and use it in the project without major mess.

The JAR download (as of May 22, 2018) is here .

I also recommend you to read how JDBC connections work since you were using code without understanding it.

    
answered by 22.05.2018 в 21:26