首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >向NetBeans项目添加MySQL驱动程序库

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

Stack Overflow用户
提问于 2015-06-24 04:51:52
回答 1查看 11.5K关注 0票数 0

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

Login.htm

代码语言:javascript
运行
复制
Login





Login


     
      Email:
      
    
     
      Salasana

相当标准的登录,是的。当传递表单时,它应该转到一个

启动登录序列。相反,我是从Glassfish得到的:

代码语言:javascript
运行
复制
HTTP Status 500 - Internal Server Error

type Exception report

messageInternal Server Error

descriptionThe server encountered an internal error that prevented it from fulfilling this request.

exception

java.lang.NullPointerException
note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 4.1 logs.

GlassFish Server Open Source Edition 4.1

,我们的导师留下了一张纸条,整个项目应该包括一个MySQL驱动程序库,以避免

..。

我的问题是,如何使用Netbeans做到这一点?Netbeans告诉我我已经安装了,但显然它要么没有安装,要么无法工作。

这是

代码:

代码语言:javascript
运行
复制
/*
 * 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 POST 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();
    }


}

我是不是遗漏了什么,比如在导入或者?我已经安装了

并将其设置到多个不同的位置,结果相同。我现在所需要的就是能够看到代码是否工作,但是

阻止我这样做。添加

通过

项目属性->库

在Netbeans 8.0.2上似乎也不能解决这个问题。将MySQL注册到GlassFish,仍然适用于

..。

EN

回答 1

Stack Overflow用户

发布于 2015-06-24 13:32:06

转到服务选项卡

选择数据库

右键单击并选择注册MYSQL驱动程序

右键单击项目中的库,然后从“添加库”中选择“MYSQL

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31013289

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档