简体   繁体   中英

How to fix “No suitable driver found for jdbc:mysql://localhost:3306/emp”?

This is my first jsp web app. I have a html page that posts some info which is received by the jsp page.

Machine stats:

64bit machine Windows 7  
64bit Netbeans 8.1  
64bit Java - jdk1.7.0_55, jdk1.8.0_05  
32bit Java - none   
64bit Jre - jre8  
32bit Jre - jre1.8.0_25  

Environment variables:

CLASSPATH - .;C:\PROGRA~2\IBM\SQLLIB\java\db2java.zip;C:\PROGRA~2\IBM\SQLLIB\java\db2jcc.jar;C:\PROGRA~2\IBM\SQLLIB\java\db2jcc_license_cu.jar;C:\PROGRA~2\IBM\SQLLIB\bin;%JAVA_HOME7%\jre\lib;E:\All Softwares\Programming Languages And Tools\mysql-connector-java-5.0.8.zip

JAVA_HOME - C:\Program Files\Java\jdk1.7.0_55\  
JAVA_HOME8 - C:\Program Files\Java\jdk1.8.0_05\  
JRE_HOME - C:\Program Files\Java\jre7\  
JRE_HOME8 - C:\Program Files\Java\jre8\  
Path - C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files (x86)\Windows Resource Kits\Tools\;C:\Program Files\Dell\DW WLAN Card;C:\Program Files (x86)\AMD APP\bin\x86_64;C:\Program Files (x86)\AMD APP\bin\x86;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Common Files\Adobe\AGL;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files\WIDCOMM\Bluetooth Software\;C:\Program Files\WIDCOMM\Bluetooth Software\syswow64;C:\Program Files\Microsoft\Web Platform Installer\;C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\;c:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files (x86)\Microsoft SQL Server\100\DTS\Binn\;c:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\;%JAVA_HOME%bin\;C:\Program Files (x86)\Microsoft SQL Server\90\Tools\binn\;C:\PROGRA~2\IBM\SQLLIB\BIN;C:\PROGRA~2\IBM\SQLLIB\FUNCTION;C:\Program Files (x86)\Zend\MySQL55\bin;%JRE_HOME%

JSP Code:

    <%@ page import="java.sql.*" %>
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
        </head>
        <body>
            <%
                if (!"POST".equalsIgnoreCase(request.getMethod())) {
            %>
            <h1>Invalid Token!</h1>
            <%
                } else {
                    Connection connection = null;

                    String[] arr = new String[18];
                    arr[0]   = (request.getParameter("ignoualr")!=null)?request.getParameter("ignoualr").toString():"0";
                    arr[1]   = request.getParameter("enrlno").toString();
                    arr[2]   = request.getParameter("firstname").toString();
                    arr[3]   = request.getParameter("middlename").toString();
                    arr[4]   = request.getParameter("lastname").toString();
                    arr[5]   = request.getParameter("ffirstname").toString();
                    arr[6]   = request.getParameter("fmiddlename").toString();
                    arr[7]   = request.getParameter("flastname").toString();
                    arr[8]   = request.getParameter("dob").toString();
                    arr[9]   = (request.getParameter("sex")!=null)?request.getParameter("sex").toString():"0";
                    arr[10]   = request.getParameter("progc").toString();
                    arr[11]   = request.getParameter("address").toString();
                    arr[12]   = request.getParameter("state").toString();
                    arr[13]   = request.getParameter("city").toString();
                    arr[14]   = request.getParameter("district").toString();
                    arr[15]   = request.getParameter("pincode").toString();
                    arr[16]   = request.getParameter("mobileno").toString();
                    arr[17]   = request.getParameter("submit").toString();    

                    if(arr[17].equalsIgnoreCase("Submit")) {
                        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/emp", "root", "");

                        Statement statement = connection.createStatement();
                        String command = "INSERT INTO registration (id, ignoualr, enrlno, firstname, middlename, lastname, ffirstname, fmiddlename, flastname, dob, sex, progc, address, state, city, district, pincode, mobileno) VALUES (null";

                        for (int i=0; i<17; i++)
                            command = command + ", '" + arr[i] + "'";
                        command = command + ")";
                        statement.executeUpdate(command);
            %>
            <h1>Hello World!</h1>
            <%
                    } else {
            %>
            <h1>Invalid Token!</h1>
            <%
                    }
                }
            %>
        </body>
    </html>

Also, I added mysql-connector-java-5.0.8.zip into the libraries folder of the project.
PS I have basic knowledge of java and working knowledge of PHP.

You load the wrong driver.

change:

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

to:

Class.forName("com.mysql.jdbc.Driver");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM