Servlet没有与JDBC建立连接?

时间:2016-02-10 21:43:11

标签: java mysql servlets jdbc classnotfoundexception

我试图通过使用MySQL后端建立与JDBC的连接我已经为我的MySQL下载了java连接器(JAR)并将其放在项目的lib目录中但仍然无法成功连接错误我正在说 <html> <head> <g:javascript library="jquery" plugin="jquery"/> <jsTree:resources /> </head> <body> <div> <input class="search-input form-control"> </div> <div id="jstree"> </div> <script> $(function() { $(".search-input").keyup(function() { var searchString = $(this).val(); console.log(searchString); $('#jstree').jstree('search', searchString); }); $('#jstree').jstree({ 'core': { 'data': [{ "id": "1.0", "text": "Fresh Products", "icon": "", "state": { "opened": false, "disabled": false, "selected": false }, "children": [{ "id": "2.06.0", "text": "Ethnic & Specialty", "icon": "", "state": { "opened": false, "disabled": false, "selected": false }, "children": false, "liAttributes": null, "aAttributes": null }, { "id": "2.07.0", "text": "Natural & Organic", "icon": "", "state": { "opened": false, "disabled": false, "selected": false }, "children": false, "liAttributes": null, "aAttributes": null }, { "id": "2.08.0", "text": "Prepared Foods", "icon": "", "state": { "opened": false, "disabled": false, "selected": false }, "children": false, "liAttributes": null, "aAttributes": null }, { "id": "2.09.0", "text": "Seafood", "icon": "", "state": { "opened": false, "disabled": false, "selected": false }, "children": false, "liAttributes": null, "aAttributes": null }, { "id": "2.010.0", "text": "Seafood", "icon": "", "state": { "opened": false, "disabled": false, "selected": false }, "children": false, "liAttributes": null, "aAttributes": null }], "liAttributes": null, "aAttributes": null }, { "id": "2.0", "text": "Frozen Products", "icon": "", "state": { "opened": false, "disabled": false, "selected": false }, "children": [], "liAttributes": null, "aAttributes": null }, { "id": "3.0", "text": "Store Equipment ", "icon": "", "state": { "opened": false, "disabled": false, "selected": false }, "children": [], "liAttributes": null, "aAttributes": null }, { "id": "4.0", "text": "Packaged Grocery", "icon": "", "state": { "opened": false, "disabled": false, "selected": false }, "children": [], "liAttributes": null, "aAttributes": null }, { "id": "5.0", "text": "Retail Technology", "icon": "", "state": { "opened": false, "disabled": false, "selected": false }, "children": [], "liAttributes": null, "aAttributes": null }, { "id": "6.0", "text": "HBC/Non-Foods", "icon": "", "state": { "opened": false, "disabled": false, "selected": false }, "children": [], "liAttributes": null, "aAttributes": null }] }, "search": { "case_insensitive": true, "show_only_matches" : true }, "plugins": ["search"] }); }); </script> </body> </html> 以及许多其他错误。

ProcessLoginReq.java

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

的login.jsp

package mybizlogic;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/ProcessLoginReq")
public class ProcessLoginReq extends HttpServlet {
private static final long serialVersionUID = 1L;
Connection con =null;
Statement sm=null;
ResultSet Vtbl=null;

public ProcessLoginReq() {
    super();
        }


public void init(ServletConfig config) throws ServletException {
    }


protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
    String uname=request.getParameter("txtuname");

    try
    {
         Class.forName("com.mysql.jdbc.Driver");
    }
    catch (ClassNotFoundException e)
    {
        e.printStackTrace();
    }

    try
    {
        con=DriverManager.getConnection("jdbc:mysql://localhost:3306/users","root","root");
        sm = con.createStatement();
        String msg = ("Select * from users");
        Vtbl = sm.executeQuery(msg);
        Vtbl.next();
        String nm = Vtbl.getString("username");
        PrintWriter resgen = response.getWriter();
        if(uname.equals(nm))
        {
            resgen.println("<html><head>  </head>");
            resgen.println("<title>Process</title><body>");
            resgen.println("Welcome user"+uname);
            resgen.println("</body></html>");
        }
        else
        {
            resgen.println("sorry u r not valid user");
        }
    }
    catch (SQLException ex)
    {
        ex.printStackTrace();   
    }   

}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    service(request,response);
    response.getWriter().append("Served at: ").append(request.getContextPath());
}


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    service(request,response);
    doGet(request, response);
}
}

的web.xml

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login Page</title>
</head>
<body>
<form action="procLogin" method="post">
UserName:<input type="text" name="txtuname"/><br/>
     <input type="submit" value ="click to submit"/><br/>
      <input type="reset" value ="click to clear"/><br/>


</form>
</body>

0 个答案:

没有答案