我是Java的新手,试图通过连接到mysql数据库来创建一个简单的登录和注册页面。我按照这个链接
http://www.javaknowledge.info/login-and-registration-example-in-jsp-with-session/
我已将registration.jsp更改为
<%@ page import ="java.sql.*" %>
<%
String user = request.getParameter("uname");
String pwd = request.getParameter("pass");
String fname = request.getParameter("fname");
String lname = request.getParameter("lname");
String email = request.getParameter("email");
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/myDB",
"root", "password");
Statement st = con.createStatement();
//ResultSet rs;
int i = st.executeUpdate("insert into members(first_name, last_name, email, uname, pass, regdate) values ('" + fname + "','" + lname + "','" + email + "','" + user + "','" + pwd + "', CURDATE())");
if (i > 0) {
//session.setAttribute("userid", user);
response.sendRedirect("welcome.jsp");
// out.print("Registration Successfull!"+"<a href='index.jsp'>Go to Login</a>");
} else {
response.sendRedirect("index.jsp");
}
%>
但是,当我尝试注册时,我收到此错误
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: javax.servlet.ServletException: java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:502)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:412)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
javax.servlet.ServletException: java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:865)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:794)
org.apache.jsp.registration_jsp._jspService(registration_jsp.java:80)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1075)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3566)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3498)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:919)
com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4004)
com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1284)
com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2312)
com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2122)
com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:774)
com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:49)
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
java.lang.reflect.Constructor.newInstance(Constructor.java:526)
com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:375)
com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:289)
java.sql.DriverManager.getConnection(DriverManager.java:571)
java.sql.DriverManager.getConnection(DriverManager.java:215)
org.apache.jsp.registration_jsp._jspService(registration_jsp.java:62)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
帮助我,导致问题的原因。
修改:我可以使用相同的用户名&amp;密码(root和密码)