简体   繁体   中英

servlet netbeans mysql connect

I am new to sql and I am using netbeans and mysql.

I am trying to make a servlet that will check login details against a database.

I found some code oline but it doesn't interact with the database. Also every example I find doesn't work either. What am I doing wrong.

Here is some of the code

package login;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

/**
 *
 * @author john
 */
public class LoginServlet extends HttpServlet {



     protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String userName = request.getParameter("userName");


        if (Validate.CheckUser(userName)) {
            RequestDispatcher rs = request.getRequestDispatcher("Welcome");
            rs.forward(request, 
            response          
            );
        }
else
{
          RequestDispatcher rs = request.getRequestDispatcher("index.html");
            rs.include(request ,   response);  
        }
}
}

AND

package login;

import java.sql.*;

/**
 *
 * @author john
 */
public class Validate {

    public static boolean CheckUser(String userName) {
        boolean st = false;

        try {
            Class.forName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");

            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mynewdatabase?zeroDateTimeBehavior=convertToNull", "root", "admin");

            PreparedStatement ps = con.prepareStatement("SELECT userName FROM userData where userName=?");
            ps.setString(1, userName);
            ResultSet rs = ps.executeQuery();
            st = rs.next();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return st;
    }
}

What are the extra steps I need to take to get this to connect to the data base. I am using the latest version of netbeans.

I have already put the 5.1.26-bin.jar file into the library. Mynewdatabase is running just fine(I connected using a connection pool a JSP before in a different application).

This application will not connect to the data base though. I am not using a connection pool here.

Any help would be great.

Connecting to a database has nothing at all do with NetBeans or servlets. I'd advise that you get the database class working before adding in unnecessary complications.

It'd help if you'd post an error. "Not working" isn't helpful.

Your driver class for MySQL doesn't look correct to me. Should be com.mysql.jdbc.Driver .

But there's lots more wrong with your code than that.

Your query brings back a username. No password? Not useful.

No application should access a database using the root admin password. Create an ID for the app and only GRANT sufficient permission to perform its tasks.

You don't close resources in your method. You'll have problems if you ever get this to run.

Come back if you get it to work at all.

To retrieve from database you should use this way

while(rs.next())
{
String coulm1=rs.getString(1);
String column2=rs.getString(2);
}

The code should be like this:

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

 Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mynewdatabase", "root", "admin");

You will also need to add mysql driver jar file to the classpath. Firstly try to connect with database with simple parameters in database url , then move to the complex ones when you are fully ready!

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