Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 向NetBeans项目添加MySQL驱动程序库_Java_Mysql_Netbeans_Nullpointerexception - Fatal编程技术网

Java 向NetBeans项目添加MySQL驱动程序库

Java 向NetBeans项目添加MySQL驱动程序库,java,mysql,netbeans,nullpointerexception,Java,Mysql,Netbeans,Nullpointerexception,我对JavaEE编程非常陌生,我应该做一些关于它的练习(以便很快完成学业)。 我的问题可能很简单,但我还没有找到答案。 在一个练习中,我应该能够登录到java/jsp/mysql类型的服务 Login.htm <html> <head> <title>Login</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <lin

我对JavaEE编程非常陌生,我应该做一些关于它的练习(以便很快完成学业)。 我的问题可能很简单,但我还没有找到答案。 在一个练习中,我应该能够登录到java/jsp/mysql类型的服务

Login.htm

<html>
<head>
<title>Login</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="css/style.css" />
</head>

<body>
<h3>Login</h3>
<form name="login" method="post" action="ServletLogin">
<table>
    <tr> 
      <td>Email:</td>
      <td><input name="email" type="text" id="email"></td>
    </tr>
    <tr> 
      <td>Salasana</td>
      <td><input name="password" type="password" id="password"></td>
    </tr>
    <tr> 
      <td colspan="2">
          <input type="submit" name="Submit" value="Log in;">
        </td>
    </tr>
  </table>
</form>
</body>
</html>
ServletLogin.java
中,我们的导师留下了一条提示,整个项目应该包括一个MySQL驱动程序库,以避免
NullPointerException

我的问题是,如何使用Netbeans实现这一点?Netbeans告诉我,我已经安装了,但很明显,它要么不工作,要么不工作

下面是
ServletLogin.java
code:

/*
 * Remember to install a MYSQL driver library
 * or the file will alert
 * NullPointerException.
 */

package Servlets; //the package in which the Servlet is part of.


import java.io.*;
import java.sql.*;
import javax.servlet.*;
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 javax.servlet.http.HttpSession;

@WebServlet(name = "ServletLogin", urlPatterns = {"/ServletLogin"})
public class ServletLogin extends HttpServlet {

    Connection conn = null;

    /**
     * Initializes the servlet.
     */
    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);

        //Connect to database with database-class
        try {
        conn = Classes.SQL.openConnection();
        }
        catch (Exception e) {
           System.out.println("Cannot connect to database " + e);
        }
    }

    /**
     * Destroys the servlet.
     */
    @Override
    public void destroy() {

    //Closing the database connection
         try {
          conn.close();
        } catch ( SQLException se ) {
           System.out.println("Exception " + se);
        }
    }

    //Everything is done in doPost method.
    //This Servlet does not use doGet() or ProcessRequest()

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {


    PrintWriter out = response.getWriter();

    /*Unless there is a session, one is created.
        It is used to check if the user has logged in.*/

     HttpSession session = request.getSession(true);

        //Reading the parameters from login form.
        String email = request.getParameter("email");
        String password = request.getParameter("password");

        out.print(email);

        //Created login_ok and set it to false by default.
        boolean login_ok = false;


     try {
     //let's create a Statement with which the sql can be run with.
     Statement stmt = conn.createStatement();

     //Run SQL and save it to ResultSet rs.
     ResultSet rs = stmt.executeQuery("SELECT email, password FROM clients");

     //Go trough the results with while loop and next-method, which returns the value true, until we've reached last result.
     while(rs.next())
     {
       //Reading the data
       String email2 = rs.getString("email");
       String salasana2 = rs.getString("password);

       //If user input has been found from database,
       //login_ok becomes true and looping ends.
       if(email.compareTo(email2) == 0 && password.compareTo(password2) == 0)
       {
           login_ok = true;
           break;
       }

     }


     //If login_ok is true, save info about logging to session and guide the user to Clients.jsp
        if( login_ok == true )
        {
            //session is informed about login
            session.setAttribute("login", "ok");
            //Debugging printed to console.
            System.out.println("Login ok");
            //Proceeding to clients
            response.sendRedirect("clients.jsp");
            //Return stops the servlet run.
            return;
        }
      //Login false -> redirected to login page.
        else {
            response.sendRedirect("login.htm");
        }

    }

     catch(SQLException se){

     out.println("Error: " + se);

     }

        out.close();
    }


}
我是不是错过了什么,比如进口还是进口? 我已经安装了
mysql-connector-java-5.1.23-bin.jar
并将其设置到多个不同的地方,结果是一样的。我现在所需要的就是能够看到代码是否工作,但是
NullPointerException
阻止我这样做。 在Netbeans 8.0.2上通过项目属性->库添加
mysql-connector-java-5.1.23-bin.jar似乎也解决不了这个问题。
将MySQL注册到GlassFish,仍然使用
NullPointerException

  • 转到“服务”选项卡
  • 选择数据库
  • 右键单击并选择注册MYSQL驱动程序
  • 右键单击项目中的库,然后从添加库中选择MYSQL

  • 您是否在Glassfish中注册了mysql驱动程序?你可以找到如何做到这一点。这很简单,不要像那样迭代SQL结果集。数据库的职责是查找单个记录。学习使用
    选择。。。其中…
    您是否在服务器日志中看到“无法连接到数据库(…)”?你能提供更多关于如何在Java中实现连接的详细信息吗?不允许注册MySQL驱动程序,该选项不可用。
    /*
     * Remember to install a MYSQL driver library
     * or the file will alert
     * NullPointerException.
     */
    
    package Servlets; //the package in which the Servlet is part of.
    
    
    import java.io.*;
    import java.sql.*;
    import javax.servlet.*;
    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 javax.servlet.http.HttpSession;
    
    @WebServlet(name = "ServletLogin", urlPatterns = {"/ServletLogin"})
    public class ServletLogin extends HttpServlet {
    
        Connection conn = null;
    
        /**
         * Initializes the servlet.
         */
        @Override
        public void init(ServletConfig config) throws ServletException {
            super.init(config);
    
            //Connect to database with database-class
            try {
            conn = Classes.SQL.openConnection();
            }
            catch (Exception e) {
               System.out.println("Cannot connect to database " + e);
            }
        }
    
        /**
         * Destroys the servlet.
         */
        @Override
        public void destroy() {
    
        //Closing the database connection
             try {
              conn.close();
            } catch ( SQLException se ) {
               System.out.println("Exception " + se);
            }
        }
    
        //Everything is done in doPost method.
        //This Servlet does not use doGet() or ProcessRequest()
    
        /** 
         * Handles the HTTP <code>POST</code> method.
         * @param request servlet request
         * @param response servlet response
         */
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    
    
        PrintWriter out = response.getWriter();
    
        /*Unless there is a session, one is created.
            It is used to check if the user has logged in.*/
    
         HttpSession session = request.getSession(true);
    
            //Reading the parameters from login form.
            String email = request.getParameter("email");
            String password = request.getParameter("password");
    
            out.print(email);
    
            //Created login_ok and set it to false by default.
            boolean login_ok = false;
    
    
         try {
         //let's create a Statement with which the sql can be run with.
         Statement stmt = conn.createStatement();
    
         //Run SQL and save it to ResultSet rs.
         ResultSet rs = stmt.executeQuery("SELECT email, password FROM clients");
    
         //Go trough the results with while loop and next-method, which returns the value true, until we've reached last result.
         while(rs.next())
         {
           //Reading the data
           String email2 = rs.getString("email");
           String salasana2 = rs.getString("password);
    
           //If user input has been found from database,
           //login_ok becomes true and looping ends.
           if(email.compareTo(email2) == 0 && password.compareTo(password2) == 0)
           {
               login_ok = true;
               break;
           }
    
         }
    
    
         //If login_ok is true, save info about logging to session and guide the user to Clients.jsp
            if( login_ok == true )
            {
                //session is informed about login
                session.setAttribute("login", "ok");
                //Debugging printed to console.
                System.out.println("Login ok");
                //Proceeding to clients
                response.sendRedirect("clients.jsp");
                //Return stops the servlet run.
                return;
            }
          //Login false -> redirected to login page.
            else {
                response.sendRedirect("login.htm");
            }
    
        }
    
         catch(SQLException se){
    
         out.println("Error: " + se);
    
         }
    
            out.close();
        }
    
    
    }