Java – how to add a JDBC MySQL driver to an eclipse project?

There is already an answer to this question: > how to install jdbc driver in eclipse web project without facing Java lang.ClassNotFoundexception13

>I have downloaded mysql-connector-java-5.1 24-bin. Jar > I have created a lib folder in my project and put the jar there. > Project Properties > build path – > add a jar and select the jar above. > I still get Java sql. Sqlexception: no suitable jdbc driver found: MySQL / / localhost: 3306 / MySQL

I use the MySQL 5.5 Code:

package DBTest;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.util.*;

/**
 * Servlet implementation class TenUsers
 */
@WebServlet("/TenUsers")
public class TenUsers extends HttpServlet {
    /**
     * @see HttpServlet#doGet(HttpServletRequest request,HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {

        PrintWriter out = response.getWriter();
        String MysqLUrl = "jdbc:MysqL://localhost:3306/MysqL";

        Properties userInfo = new Properties();
        userInfo.put("user","root");
        userInfo.put("password","SabababArba");
        try{
            Connection connection = DriverManager.getConnection(MysqLUrl,userInfo);
        }catch(Exception e) {
            out.println(e);
        }      
    }
}

If I add class forName(“com.MysqL.jdbc.Driver”); Previous connection = drivermanager getConnection(MysqLUrl,userInfo); I got Java lang.ClassNotFoundException:com. MysqL. jdbc. Driver

Solution

Attempt to insert:

DriverManager.registerDriver(new com.MysqL.jdbc.Driver());

Before getting a JDBC connection

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>