求用java编登录页面,可以连接MySql 数据库

 我来答
pieryon
推荐于2016-09-27 · 知道合伙人数码行家
pieryon
知道合伙人数码行家
采纳数:14411 获赞数:166863
获取软件设计师高级职称 万达金融最佳创新奖

向TA提问 私信TA
展开全部
第一步:创建一个查询过程,因为在登录时要根据用户名查询用户密码

此步要用到pl/sql编程知识,代码如下:

create or replace procedure sel_user(uname in varchar2,pass out varchar2) is
begin
select users.password into pass from users where users.username=uname and rownum = 1;
end;

第二步:编写登录页面(login.java)(采用纯java+servlet编写)

//login.java如下

package cn.hnu;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class testhtml extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html;charset=gbk");
try {
PrintWriter pw = resp.getWriter();
pw.println("<html>");
pw.println("<head>");
pw.println("<title>");
pw.println("用户登录");
pw.println("</title>");
pw.println("</head>");
pw.println("<body>");
pw.println("<h1>用户登录</h1>");
pw.println("<hr>");
pw.println("<form method=post action=loginCl>");
pw.println("用户名:<input type=text name=userName><br>");
pw.println("密  码:<input type=password name=password><br>");
pw.println("<input type=submit value=登录>");
pw.println("<input type=reset value=重置>");
pw.println("</form>");
pw.println("</body>");
pw.println("</html>");
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
this.doGet(req, resp);
}

}

第三步:编程成功登录页面(wel.java) //wel.java如下,它主要用于用户正常登录后显示信息给用户

package cn.hnu;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class Wel extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub

//防止用户非法登录
HttpSession hs = req.getSession();
String s = (String)hs.getAttribute("pass");
if(s == null){
resp.sendRedirect("login");
}
PrintWriter pw = resp.getWriter();
pw.write("welcome,hello");
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
this.doGet(req, resp);
}

}

第四步:编写login处理页面(loginCl.java)

package cn.hnu;

import java.io.IOException;
import java.sql.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class loginCl extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
String u = req.getParameter("userName");
String p = req.getParameter("password");

//查询数据库
String pa=null;
Connection ct = null;
CallableStatement cs = null;

try {
Class.forName("oracle.jdbc.driver.OracleDriver");
ct = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oracle",
"scott", "tiger");
cs = ct.prepareCall("{call sel_user(?,?)}");
cs.setString(1, u);
cs.registerOutParameter(2, oracle.jdbc.OracleTypes.VARCHAR);
cs.execute();
pa = cs.getString(2);

System.out.println("u=" + u + " p=" + pa);

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {

try {
if (cs != null) {
cs.close();
}
if (ct != null) {
ct.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

//验证用户信息是否合法

if (p.equals(pa)) {
HttpSession hs = req.getSession(true);//防止用户非法登录
hs.setAttribute("pass", "OK");
resp.sendRedirect("wel");
} else {
resp.sendRedirect("login");
}
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
this.doGet(req, resp);
}

}

亲,sql可以换成MySQL
这个没关系的,别的都可以照搬来用
追答
亲,给个好评擦
采纳一下把
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式