導航:首頁 > 編程語言 > 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資料庫登陸界面相關的資料

熱點內容
exe怎麼知道源碼 瀏覽:383
計算機怎麼實現可編程的 瀏覽:494
軒轅傳奇如何查看玩過的伺服器 瀏覽:37
凌陽單片機選型 瀏覽:480
android手機hosts 瀏覽:97
路由器l2tp伺服器地址是什麼 瀏覽:160
做解壓手套視頻 瀏覽:620
退役命令文件 瀏覽:676
linux遞歸查找文件 瀏覽:245
哪個app上才能看到免費的名門摯愛 瀏覽:839
mysql查看錶欄位的命令 瀏覽:523
ios里的文件夾怎麼用 瀏覽:166
壓縮銀行開戶時間表 瀏覽:429
銀行人員如何解壓 瀏覽:829
newfile命令快捷鍵 瀏覽:569
阿里雲物理伺服器 瀏覽:955
靈狐視頻app哪個好 瀏覽:259
大廠退役程序員自述 瀏覽:254
linux命令watch 瀏覽:889
加密幣哪些平台不撤出中國 瀏覽:553