2013-01-07 22 views
0

This table is the one that I want to displayThis is Clients (System users) table如何根據哪個用戶登錄來分離數據庫內容,以便每個用戶只能看到他的內容。我不確定你是否需要MySql中的實際表,如果你需要,我會編輯它們。JSP中用戶名登錄的單獨結果

登錄

<%@ page language="java" import="java.sql.*" import="java.text.*" errorPage="" %> 
<% 

Connection conn = null; 
Class.forName("com.mysql.jdbc.Driver").newInstance(); 
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/database","root",   "root"); 

ResultSet rsLoginFunction = null; 
PreparedStatement psLoginFunction=null; 

String sUserID=request.getParameter("username"); 
String sPassword=request.getParameter("password"); 
String message="User login successfully "; 

try{ 
String sql ="Select * from clients where username=? and password=?"; 

psLoginFunction=conn.prepareStatement(sql); 
psLoginFunction.setString(1,sUserID); 
psLoginFunction.setString(2,sPassword); 

rsLoginFunction=psLoginFunction.executeQuery(); 

if(rsLoginFunction.next()) 
{ 
    String username=rsLoginFunction.getString("Email")+"  "+rsLoginFunction.getString("clientID"); 

    session.setAttribute("Username",rsLoginFunction.getString("username")); 
    session.setAttribute("Email",rsLoginFunction.getString("Email")); 

// session.setAttribute("iUserLevel",rsLoginFunction.getString("iUserLevel")); 
// session.setAttribute("sUserName",sUserName); 

    response.sendRedirect("view_menu.jsp?error="+message); 
} 
else 
{ 
    message="No user or password matched" ; 
    response.sendRedirect("Login.jsp?error="+message); 
} 
} 
catch(Exception e) 
{ 
    e.printStackTrace(); 
} 


/// close object and connection 
try{ 
    if(psLoginFunction!=null){ 
     psLoginFunction.close(); 
    } 
    if(rsLoginFunction!=null){ 
     rsLoginFunction.close(); 
    } 

    if(conn!=null){ 
     conn.close(); 
    } 
} 
catch(Exception e) 
{ 
    e.printStackTrace(); 
} 

%> 

查看但不能分隔

<%@ include file="include/commonStrings.jsp"%> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
<script type="text/javascript"> 
function del() { 
    if (confirm("Do You Want to Delete this Menu?")) { 
    } else { 
     return false; 
    } 
} 
</script> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title></title> 
<link rel="stylesheet" href="images/style.css" type="text/css" 
charset="utf-8" /> 
</head> 
<body> 
<% 
    menu_slno1 = request.getParameter("menu_slno"); 
    if (menu_slno1 != null) 
     menu_slno = Integer.parseInt(menu_slno1); 
    delete_menu = request.getParameter("delete_menu"); 


    if ("yes".equals(delete_menu)) { 
     MenuId = request.getParameter("MenuId"); 
     x = stmt1 
       .executeUpdate("Delete from menu where MenuId=" 
         + MenuId); 
    } 
%> 


<h2>VIEW MENUS</h2> 

<table width="736" height="97" border="1"> 
    <% 
     if (x == 1) { 
    %> 
    <tr bgcolor="#000000"> 
     <th height="35" colspan="9"><div align="center"> 
       Menu (<%=MenuId%>) deleted successfully 
      </div></th> 
    </tr> 
    <% 
     } 
    %> 
    <tr bgcolor="#089937"> 
         </div></td> 
     <td><div align="center"> 
       <strong>MENU ID </strong> 
      </div></td> 
     <td><div align="center"> 
       <strong>MENU NAME </strong> 
      </div></td> 
     <td><div align="center"> 
       <strong>MENU INFO </strong> 
      </div></td> 
     <td><div align="center"> 
       <strong>MENU PRICE </strong> 
      </div></td> 
     <td><div align="center"> 
       <strong>MODIFY </strong> 
      </div></td> 
     <td colspan="2"><div align="center"> 
       <strong>DELETE</strong> 
      </div></td> 
    </tr> 
    <% 
     int icount = 0; 

//here i only know how to display whole table 
     rs = stmt.executeQuery("select * from menu"); 
     while (rs.next()) { 
      //menu_slno = rs.getInt("menu_slno"); 
      MenuId = rs.getString("MenuId"); 
    %> 
    <tr> 
     <td><div align="center"><%=++icount%></div></td> 

     <td><%=rs.getString("Name")%></td> 
     <td><%=rs.getString("Info")%></td> 
     <td><%=rs.getDouble("Price")%></td> 

     <td><div align="center"> 
       <a href="edit_menu.jsp?MenuId=<%=MenuId%>">Edit</a> 
      </div></td> 
     <td><div align="center"> 
       <a 
        href="view_menu.jsp?delete_menu=yes&MenuId=  <%=MenuId%>&MenuId=<%=MenuId%>" 
        onclick="return del()">Delete</a> 
      </div></td> 
    </tr> 
    <% 
     } 
    %> 
</table> 
<a href="add_menu.jsp">Add Menu</a> 

</body> 
</html> 
+1

不要將密碼存放在純文本。 – SLaks

+0

我在學習過程中,這個時候讓我們忽略這個事實,但是謝謝你的建議。 –

+0

@ user1703849我編輯了我的答案,請讓我知道如果我沒有得到您的問題 – Bhushan

回答

0

首先在數據庫中調用role將存儲權限創建其他領域。意思是如果用戶具有管理角色,他可以訪問所有內容,如果沒有,他將會有一些限制。

然後,您可以在向用戶顯示輸出之前輕鬆檢查它。讓我知道你是否需要更多幫助。

編輯

您可以使用下面的代碼: 在表中增加第三欄之後:

role=rsLoginFunction.getString("role") 
if(role.equalsIgnoreCase("admin")) 
{ 
    //show the contents with full functionality. 
    //like Delete, Edit, Add option 
} 
else 
{ 
    //show the contents with limited access 
    //like Allow user to only see the data, not allow him to modify 
} 

編輯:

可以使用SQL查詢一樣做到這一點:

select * from menu where login.username=menu.username 

因此它會顯示與特定用戶相關的記錄。

編輯:

String s = request.getParameter("username"); 
String query = "select * from menu where menu.username= s"; 

,然後執行此查詢這會給你造成的,你想......

+0

嗨,我可能有點遲,但無論如何。我很喜歡編程,所以我不知道該怎麼做。我有5個用戶必須能夠看到他們的內容(2個表格中的4列)。我的登錄可以檢查最終用戶是否登錄,但是我不知道如何顯示他們需要的列(在我發佈的第二個代碼中) –

+0

感謝您的努力。但我可能忘記提及所有用戶都具有相同的訪問級別,但他們必須訪問不同的內容(如只有他們自己的個人信息),這是我不知道該怎麼做的。 –

+0

你可以發佈相關的數據庫結構嗎? – Bhushan