我正在使用Eclipse和MySQL,但在插入相应字段中的值时,错误显示为"用户访问被拒绝' root' localhost' (使用密码:是)"
以下是代码:
package pkg1;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
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("/loginreg")
public class loginreg extends HttpServlet {
private static final long serialVersionUID = 1L;
Connection con = null;
PreparedStatement stmt = null;
public loginreg() {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hms","root","12sanil1992");
System.out.print("\n Insert Values: ");
stmt = con.prepareStatement("insert into users values(?,?)");
stmt.setString(1, request.getParameter("Username"));
stmt.setString(2, request.getParameter("user_Password"));
Object data = "No of Rows Affected: "+ stmt.executeUpdate();
con.close();
request.setAttribute("data", data);
request.getRequestDispatcher("login1.jsp").forward(request, response);
}
catch(Exception ex)
{
System.out.println(ex);
request.setAttribute("data", ex);
request.getRequestDispatcher("login1.jsp").forward(request, response);
}
finally
{
}
}
}
答案 0 :(得分:0)
您使用12sanil1992作为密码,这可能是错误的,这就是您拒绝访问的原因。
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hms","root","12sanil1992");