问:我有2个HTML表单:1.注册/登录2.用户登录后,他用来更新他的详细信息。
我使用MySql数据库来保存用户的登录详细信息。在他登录后,我还有另一个表格可用于更新他的详细信息。我在注册/登录过程中获得了成功。,但是这里出现了问题。,我不知道如何继续进行。,在他登录之后,用户应该能够在其各自的表Used S / w中更新其详细信息: Eclipse,JDBC,HTML Servlet任何帮助将是不二之选。提前致谢
答:这是登录Servlet:
导入javax.servlet。*;
导入javax.servlet.http。*;
导入java.io. *;
导入java.sql。*;
公共类Login扩展HttpServlet
{
公共无效doPost(HttpServletRequest请求,
HttpServletResponse响应)引发ServletException,IOException
{
HttpSession会话= request.getSession(true);
尝试
{
字符串名称= request.getParameter(“名称”);
字符串password = request.getParameter(“ pass”);
字符串member = request.getParameter(“ mem”);
session.setAttribute(name,“ name”);
response.setContentType(“ text / html”);
PrintWriter out = response.getWriter();
Class.forName(“ com.mysql.jdbc.Driver”);
连接con = DriverManager.getConnection
(“ jdbc:mysql:// localhost:3306 / EATWO”,“ root”,“”);
PreparedStatement stmt = con.prepareStatement
(“从歌手那里选择*,其中name =?和password =?以及member =?”);
stmt.setString(1,name);
stmt.setString(2,password);
stmt.setString(3,member);
ResultSet rset = stmt.executeQuery();
if(rset.next()){
if(member.equals(“ Singers”))
{
System.out.println(rset.getString(1));
//out.println("Welcome,“ + name);
//System.out.println(rset.getString(1));
//out.println("<!DOCTYPE html> <head>“ +” <title>歌手页面</ title>“ +” </ head>“ +” <body>“));
//out.println(“ <script> alert('立即创建您的网页')</ script>”));
//out.print(“ </ body> </ html>”);
// ServletContext context = getServletContext();
RequestDispatcher rd = request.getRequestDispatcher(“ Singers.html”);
rd.include(请求,响应);
}
其他{
RequestDispatcher rd = request.getRequestDispatcher(“ Producers.html”);
rd.include(请求,响应);
}
}
其他
{
RequestDispatcher rd = request.getRequestDispatcher(“ login&#95; err.html”);
rd.include(request,response);
con.close();
}
}
catch(异常e)
{
System.out.println(e);
}
}
}
这是用于存储该用户详细信息的代码:
导入java.io.IOException;
导入java.io.PrintWriter;
导入java.sql.Connection;
导入java.sql.DriverManager;
导入java.sql.PreparedStatement;
导入java.sql.ResultSet;
导入java.sql.Statement;
导入javax.servlet.ServletException;
导入javax.servlet.http.HttpServlet;
导入javax.servlet.http.HttpServletRequest;
导入javax.servlet.http.HttpServletResponse;
导入javax.servlet.http.HttpSession;
公共类SingerDetails扩展HttpServlet {
/ **
*
* /
私有静态最终长serialVersionUID = 1L;
受保护的void doPost(HttpServletRequest请求,HttpServletResponse响应)
引发ServletException,IOException {
response.setContentType(“ text / html; charset = UTF-8”);
PrintWriter out = response.getWriter();
HttpSession会话= request.getSession(true);
字符串名称=(String)request.getAttribute(“ name”);
字符串Sdetails = request.getParameter(“ Sdetails”);
字符串SName = request.getParameter(“ SName”);
字符串语言= request.getParameter(“ Language”);
字符串网站= request.getParameter(“ Email”);
String Mobile = request.getParameter(“ Mobile”);
尝试{
//加载mysql驱动程序
Class.forName(“ com.mysql.jdbc.Driver”);
//与数据库建立连接
连接con = DriverManager.getConnection
(“ jdbc:mysql:// localhost:3306 / EATWO”,“ root”,“”);
语句语句=(Statement)con.createStatement();
ResultSet rs = statement.executeQuery(“从歌手那里选择用户ID,其中name ='” + name +“'”);
字符串重复= null;
while(rs.next()){
重复= rs.getString(1);
}
if(duplicate == null){
PreparedStatement ps = con.prepareStatement
(“插入singerdetails(singerdetails(singername,stagename,Language,Email,Mobile)values(?,?,?,?,?))”);
ps.setString(1,Sdetails);
ps.setString(2,SName);
ps.setString(3,语言);
ps.setString(4,网站);
ps.setString(5,Mobile);
int i = ps.executeUpdate();
如果(i> 0)
{
out.println(“您已成功注册”);
//response.sendRedirect("./singerdetails1“);
}
}
其他{
//request.getRequestDispatcher("/register.html").forward(request,response);
//request.setAttribute("error“,”无效的用户名/密码“);
request.getRequestDispatcher(“ / Singers.html”)。forward(request,response);
}
}
catch(异常se)
{
se.printStackTrace();
}
}
}