导航:首页 > 编程语言 > java数据库登陆界面

java数据库登陆界面

发布时间:2023-10-26 14:56:32

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

第一步:创建一个查询过程,因为在登录时要根据用户名查询用户密码

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

create or replace procere 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
这个没关系的,别的都可以照搬来用

阅读全文

与java数据库登陆界面相关的资料

热点内容
ie文件夹禁止访问 浏览:543
百川互联网程序员 浏览:781
linuxpython解释器 浏览:665
兴安得力软件加密狗 浏览:490
智能网络摄像头加密 浏览:574
软件毕业程序员培训 浏览:650
安卓陀螺仪低怎么办 浏览:245
一级建造师复习题集pdf 浏览:903
法理学pdf海默 浏览:390
服务器内存储器是用什么的 浏览:817
微帮同城分类信息源码 浏览:808
安卓系统ad是什么 浏览:471
python输出中不加占位符 浏览:596
linux文件夹权限控制 浏览:730
雅虎邮箱怎么加密码 浏览:819
为什么安卓手机登录不了苹果账号 浏览:537
如何复制usb加密狗 浏览:801
哪个app看你微笑时很美 浏览:908
mac启动命令 浏览:602
ngc服务器是什么的简称 浏览:75