做树并不复杂,但我们通常做的是2层或3层,那样的数据一般来自多个表,比如:部门,员工 然而这种自连接的表,其没有确定的层次,可能是无限多级 比如:a是b的上级,b是c的上级,c是d的上级... 每个上级有几个下级,下级的层次,都是动态的 解决这个问题,其实主要用到js的知识 可以使用div的innerHTML属性 当然也可以用table,用append的方法 下面就用div的innerHTML属性来实现 主要思路是通过super,在document中查找id与自身super关联的div,这个div就是自身的上级,把自己附加到这个div中就可以了 另外,通过设置自己的title,保存了自己上级的id 为了看到层层缩进效果,设置了div的style为左边距有10px + 和 - 通过div中的span实现,这里改变的是span的innerHTML,也可以换为用图片,改变img的src即可 而在点击这些 + 或 - 的时候,会调用一个js函数,传入自己的id,这个函数会在document的所有div中查找自己的下级,由于每个div的title保存了其上级的id,所以只有找到那些title为自己id的就可以了 找到这些下级,就设置其隐藏或显示即可 建表 use tempdb go create table tb ( id int primary key, name varchar(50) not null, super int references tb ) insert into tb values (1,'总公司',null); insert into tb values (2,'长沙分公司',1); insert into tb values (3,'株洲分公司',1); insert into tb values (4,'湘潭公司',1); insert into tb values (5,'长沙东区分点',2); insert into tb values (6,'长沙南区分点',2); insert into tb values (7,'湘潭东区分点',4); insert into tb values (8,'长沙东区分点一处',5); insert into tb values (9,'长沙东区分点二处',5); insert into tb values (10,'长沙东区分点三处',5); insert into tb values (11,'长沙南区分点一处',6); insert into tb values (12,'长沙南区分点二处',6); insert into tb values (13,'湘潭东区分点一处',7); insert into tb values (14,'湘潭东区分点二处',7); insert into tb values (15,'长沙东区分点一处一门市部',8); insert into tb values (16,'长沙东区分点一处二门市部',8); select id,name,isnull(super,0) as super from tb order by super ------------------------------------------------------------------------------------------------------------------------- tree.jsp文件 <%@ page language="java" import="java.sql.*,java.util.*" pageEncoding="gbk" contentType="text/html; charset=gbk" isELIgnored="false"%> <% //获取数据库的数据,并保存为双层集合,然后放到pageContext中 //这样与使用dao和servlet得到的,放到request中的方式是一致的 Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); String url = "jdbc:microsoft:sqlserver://localhost:1433;databasename=tempdb"; Connection cn = DriverManager.getConnection(url,"sa","sa"); Statement st = cn.createStatement(); String sql = "select id,name,isnull(super,0) as super from tb order by super"; ResultSet rs = st.executeQuery(sql); ArrayList lstAll = new ArrayList(); ArrayList lstLine; while(rs.next()) { lstLine = new ArrayList(); lstLine.add(rs.getString(1)); lstLine.add(rs.getString(2)); lstLine.add(rs.getString(3)); lstAll.add(lstLine); } pageContext.setAttribute("lstAll",lstAll); %> <script> //初始化树 function ini() { var str = "${lstAll}"; var ary = str.split("], ["); var i,j; var len = ary.length; var ary2,len2; var str,str2,obj; for(i=0;i<len;i++) { ary[i] = ary[i].replace("[[",""); ary[i] = ary[i].replace("]]",""); ary2 = ary[i].split(", "); if(ary2[2]=="0") { //顶层:设置其上级为div0 obj = document.getElementById("div0"); //定义自身内容,设置自身可见 -- display:block //span用于确定 + 或 - ,并且ope函数用于点击时显示或隐藏下级 str2 = "<div style='display:block' id='div" + ary2[0] + "'><span id='span" + ary2[0] + "' onclick='ope(" + ary2[0] + ")'>+</span>  " + ary2[1] + "</div>"; } else { //其他:查找其上级,即:ID为 'div' + ary2[2] 的div //如:如果ary2[2]为3,那么其上级为 div3 obj = document.getElementById("div" + ary2[2]); //定义自身内容,其中title用于存储其上级ID,设置自身不可见 -- display:none str2 = "<div style='display:none' id='div" + ary2[0] + "' title='" + ary2[2] + "'><span id='span" + ary2[0] + "' onclick='ope(" + ary2[0] + ")'>+</span>  " + ary2[1] + "</div>"; } str = obj.innerHTML; //获取上级原来的内容 str = str + str2; //附加当前div obj.innerHTML = str; //设置上级的新内容 } } //点某个节点时,展开或隐藏其下级 function ope(id) { //首先改变 + 和 - var obj = document.getElementById("span" + id); if(obj.innerHTML == "+") { obj.innerHTML = "-"; } else { obj.innerHTML = "+"; } //然后找到下级,并改变其可见性 var objs = document.getElementsByTagName("div"); //获取所有的div var len = objs.length; //遍历这些div,找到所有的下级,即: title为id 的div,对于这些下级 //如果原来是隐藏,就让其显示;否则,隐藏--通过设置其style的display即可 var i,title; for(i=0;i<len;i++) { obj = objs[i]; title = obj.title; if(title==null || isNaN(title)) { continue; } if(parseInt(title)==parseInt(id)) { if(obj.style.display=="none") { obj.style.display = "block"; } else { obj.style.display = "none"; } } } } </script> <style> div{margin-left:10px;color:darkblue} span{color:red;cursor:hand} </style> <body onload="ini();"> <div id="div0"></div> ------------------------------------------------------------------------------------------------------------------------------ 大家可以进一步想,实际点每个内容的时候,一般会链接到某个url,那么,这里怎么处理? 如果你熟悉html和js,当然就是很容易的 思路:数据库中该表增加一个字段,url,定义链接地址 在js中修改代码 大家还可以考虑把它做成标签,

声明:作者原创,版权所有。未经授权,不得转载 在jsp中用bean和servlet联合实现用户注册、登录 作者:imagebear版权:imagebear 本例需要的软件和运行环境:1、Windows2000 Server操作系统2、jdk1.43、JCreator2.5(java源码编辑调试器,吐血推荐!)4、Macromedia JRun MX5、Macromedia Dreamweaver MX(非必需)6、MySQL数据库(最好安装MySQL Control Center) 一、数据库设计用MySQL Control Center打开MySQL数据库,新建数据库shopping,在其下新建表tbl_user,其中各字段设置如下:二、编写连接数据库bean:DBConn.java//DBConn.java//include required classesimport java.sql.*;//==========================================// Define Class DBConn//==========================================public class DBConn{ public String sql_driver = "org.gjt.mm.mysql.Driver"; public String sql_url = "jdbc:mysql://localhost:3306"; public String sql_DBName = "shopping"; public String user = "sa"; public String pwd = "; Connection conn = null; Statement stmt = null; ResultSet rs = null; public boolean setDriver(String drv) {  this.sql_driver = drv;  return true; } public String getDriver() {  return this.sql_driver; } public boolean setUrl(String url) {  this.sql_url = url;  return true; } public boolean setDBName(String dbname) {  this.sql_DBName = dbname;  return true; } public String getDBName() {  return this.sql_DBName; } public boolean setUser(String user) {  this.user = user;  return true; } public String getUser() {  return this.user; } public boolean setPwd(String pwd) {  this.pwd = pwd;  return true; } public String getPwd() {  return this.pwd; } public DBConn() {  try{   Class.forName(sql_driver);//加载数据库驱动程序   this.conn = DriverManager.getConnection(sql_url + "/" + sql_DBName + "?user=" + user + "&password=" + pwd + "&useUnicode=true&characterEncoding=gb2312");   this.stmt = this.conn.createStatement();  }catch(Exception e){   System.out.println(e.toString());  } }                //执行查询操作 public ResultSet executeQuery(String strSql) {  try{   this.rs = stmt.executeQuery(strSql);   return this.rs;  }catch(SQLException e){   System.out.println(e.toString());   return null;  }catch(NullPointerException e){   System.out.println(e.toString());   return null;  } }                //执行数据的插入、删除、修改操作 public boolean execute(String strSql) {  try{   if(this.stmt.executeUpdate(strSql) == 0)    return false;   else    return true;  }catch(SQLException e){   System.out.println(e.toString());   return false;  }catch(NullPointerException e){   System.out.println(e.toString());   return false;  } }                //结果集指针跳转到某一行 public boolean rs_absolute(int row) {  try{   this.rs.absolute(row);   return true;  }catch(SQLException e){   System.out.println(e.toString());   return false;  } } public void rs_afterLast() {  try{   this.rs.afterLast();  }catch(SQLException e){   System.out.println(e.toString());  } } public void rs_beforeFirst() {  try{   this.rs.beforeFirst();  }catch(SQLException e){   System.out.print(e.toString());  } } public void rs_close() {  try{   this.rs.close();  }catch(SQLException e){   System.out.print(e.toString());  } } public void rs_deleteRow() {  try{   this.rs.deleteRow();  }catch(SQLException e){   System.out.print(e.toString());  } } public boolean rs_first() {  try{   this.rs.first();   return true;  }catch(SQLException e){   System.out.print(e.toString());   return false;  } } public String rs_getString(String column) {  try{   return this.rs.getString(column);  }catch(SQLException e){   System.out.println(e.toString());   return null;  } }                //此方法用于获取大段文本,                //将其中的回车换行替换为<br>                //输出到html页面 public String rs_getHtmlString(String column) {  try{   String str1 = this.rs.getString(column);   String str2 = "\r\n";   String str3 = "<br>";   return this.replaceAll(str1,str2,str3);  }catch(SQLException e){   System.out.println(e.toString());   return null;  } }                //把str1字符串中的str2字符串替换为str3字符串 private static String replaceAll(String str1,String str2,String str3) {  StringBuffer strBuf = new StringBuffer(str1);     int index=0;  while(str1.indexOf(str2,index)!=-1)  {   index=str1.indexOf(str2,index);   strBuf.replace(str1.indexOf(str2,index),str1.indexOf(str2,index)+str2.length(),str3);   index=index+str3.length();    str1=strBuf.toString();  }  return strBuf.toString(); }  public int rs_getInt(String column) {  try{   return this.rs.getInt(column);  }catch(SQLException e){   System.out.println(e.toString());   return -1;  } } public int rs_getInt(int column) {  try{   return this.rs.getInt(column);  }catch(SQLException e){   System.out.println(e.toString());   return -1;  } } public boolean rs_next() {  try{   return this.rs.next();  }catch(SQLException e){   System.out.println(e.toString());   return false;  } }                //判断结果集中是否有数据 public boolean hasData() {  try{   boolean has_Data = this.rs.first();      this.rs.beforeFirst();   return has_Data;  }catch(SQLException e){   System.out.println(e.toString());   return false;  } } public boolean rs_last() {  try{   return this.rs.last();  }catch(SQLException e){   System.out.println(e.toString());   return false;  } } public boolean rs_previous() {  try{   return this.rs.previous();  }catch(Exception e){   System.out.println(e.toString());   return false;  } }                //main方法,调试用 public static void main(String args[]) {  try{   DBConn myconn = new DBConn();   //myconn.setDBName("shopping");   //myconn.DBConn();   //myconn.execute("Insert Into tbl_test(id,name) values('10','shandaer')");   //myconn.execute("Update tbl_test set name='yyyyyyyyyyyy' where id=10");   //myconn.execute("Delete from tbl_test where id=1");   ResultSet rs = myconn.executeQuery("select * from tbl_user order by id desc limit 1");   //boolean hasData = myconn.hasData();   //System.out.println("has data:" + hasData);   //rs.first();   while (myconn.rs.next())     {    int id = myconn.rs_getInt("id") + 1;    System.out.print(id);    System.out.println(myconn.rs_getInt("id") + myconn.rs_getString("name"));    //System.out.println('\n' + myconn.rs_getHtmlString("name"));    //System.out.println(myconn.rs.getString("name") + myconn.rs_getInt(1));   }  }catch(Exception e){   System.err.println(e.toString());  } }}声明:因为使用的是MySQL数据库,所以需要MySQL数据库的驱动下载后请将org包放至DBConn.java所在目录下以确保该bean能正常运行 三、编写用户注册的bean:reg.java//reg.java//import required classesimport java.sql.*;public class reg{ public int newID = 0; public boolean result = false; public boolean reg(String username,String password,String confirm,String email) {  try{   if(!this.checkUser(username))    return false;   if(!this.checkPwd(password))    return false;   if(!this.verifyPwd(password,confirm))    return false;   if(!this.checkEmail(email))    return false;   if(!this.userNotExit(username))    return false;   this.getNewID();     this.result = this.register(username,password,confirm,email);   return this.result;  }catch(Exception e){   System.out.println(e.toString());   return false;  } }//End boolean reg public boolean checkUser(String user) {  try{      if(user.indexOf("'")!=-1)   {    System.out.println("姓名中含有非法字符!");    return false;   }else    return true;  }catch(Exception e){   System.out.println(e.toString());   return false;   }  } public boolean checkPwd(String pwd) {  try{   if(pwd.indexOf("'")!=-1)   {    System.out.println("密码中含有非法字符!");    return false;   }else    return true;  }catch(Exception e){   System.out.println(e.toString());   return false;   } } public boolean verifyPwd(String pwd,String confirm) {  try{   if(!pwd.equals(confirm))   {    System.out.println("两次输入的密码不一致!");    return false;   }else    return true;  }catch(Exception e){   System.out.println(e.toString());   return false;  } } public boolean checkEmail(String email) {  try{   if(email.indexOf("'")!=-1)   {    System.out.println("E-mail中含有非法字符!");    return false;   }else    return true;  }catch(Exception e){   System.out.println(e.toString());   return false;   } } public boolean userNotExit(String user) {  try{   DBConn userDBConn = new DBConn();   userDBConn.executeQuery("select * from tbl_user where name='" + user + "'");   if(userDBConn.rs_next())   {    System.out.println("用户名已存在,请选择其它的用户名!");    return false;   }else    return true;  }catch(Exception e){   System.out.println(e.toString());   return false;   }  } public int getNewID() {  try{   DBConn newIDDBConn = new DBConn();   newIDDBConn.executeQuery("select * from tbl_user order by id desc limit 1");   if(newIDDBConn.rs_next())   {    this.newID = newIDDBConn.rs_getInt("id") + 1;    System.out.println(this.newID);   }else{    this.newID = 1;   }   return this.newID;  }catch(Exception e){   System.out.println(e.toString());   return -1;   }     } public int getID() {  return this.newID; } public boolean register(String username,String password,String confirm,String email) {  try{   DBConn regDBConn = new DBConn();   String strSQL = "insert into tbl_user(id,name,pwd,email) values('" + this.newID +"','" + username + "','" + password + "','" + email + "')";   regDBConn.execute(strSQL);   return true;  }catch(Exception e){   System.out.println(e.toString());   return false;   }  } public static void main(String args[]) {  try{   reg newreg = new reg();      System.out.println(newreg.reg("sssssssss","ssssss","ssssss","[email protected]"));   DBConn myconn = new DBConn();   myconn.executeQuery("select * from tbl_user");   while(myconn.rs_next())   {    System.out.println(myconn.rs_getInt("id") + "    " + myconn.rs_getString("name") + "    " + myconn.rs_getString("pwd") + "    " + myconn.rs_getString("email"));   }   System.out.println(newreg.getID());  }catch(Exception e){   System.err.println(e.toString());  } }};说明:1、该bean文件应和上文所述DBConn.class文件放于同一目录下2、本例主要研究注册的过程,其中的Email检测等方法并不完善,若要应用请自行设计方法 四、编写用户登陆的Servlet:login.java//login.java//import required classesimport java.io.*;import javax.servlet.*;import javax.servlet.http.*;import java.sql.*;//class loginpublic class login extends HttpServlet{ public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException {  String username = req.getParameter("username");  String password = req.getParameter("password");  if(this.checklogin(username,password))  {   Cookie mylogin = new Cookie("username",username);   mylogin.setVersion(1);   mylogin.setPath("/");   mylogin.setComment("Your login username");   res.addCookie(mylogin);   }  //Cookie[] myCookies = req.getCookies();  //String nameValue = this.getCookieValue(myCookies,"username","not found");  //PrintWriter out = res.getWriter();  //out.println("username" + ":" + nameValue);  //out.println("Test Cookie Success!");  res.sendRedirect("/index.jsp"); } public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException {  doGet(req,res); } public static String getCookieValue(Cookie[] cookies,String cookieName,String defaultValue) {  for(int i=0;i<cookies.length;i++) {  Cookie cookie = cookies[i];  if (cookieName.equals(cookie.getName()))  return(cookie.getValue()); }  return(defaultValue); }  public boolean checklogin(String username,String password) {  try{   DBConn loginConn = new DBConn();   loginConn.executeQuery("select * from tbl_user where name='" + username + "'");   if(loginConn.rs_next())   {    System.out.println("Connection created!");    if(loginConn.rs_getString("pwd").trim().equals(password))    {     System.out.println(loginConn.rs_getString("name"));     return true;    }    else    {     return false;    }   }   System.out.println("Test Login Success!");   return false;  }catch(Exception e){   System.out.println(e.toString());   return false;   }  } public static void main(String args[]) {  login mylogin = new login();  System.out.println(mylogin.checklogin("shandong","shandong")); }}说明:1、默认的jdk1.4中并没有servlet包,请至sun公司网页下载servlet.jar,放至jdk目录下的jre\lib\目录下,并在JCreator中设置jdk处添加servlet.jar包  2、本Servlet用于检验用户名和密码,若正确则将用户名写入Cookie,完成后将当前页重定向到index.jsp页 五、编写检测用户是否已经登陆的bean:checkLogin.java//checkLogin.java//import required classesimport java.io.*;import javax.servlet.*;import javax.servlet.http.*;//class checkLoginpublic class checkLogin{ public String username = "; public boolean check(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException {  String cookieName = "username";  Cookie[] myCookies = req.getCookies();  this.username = this.getCookieValue(myCookies,cookieName,"not found");  PrintWriter out = res.getWriter();  if(this.username != null)  {      //out.println("早上好," + this.username + "!");   return true;  }else{   out.println("登陆失败!");   return false;   } } public String getUserName() {  return this.username; } public static String getCookieValue(Cookie[] cookies,String cookieName,String defaultValue) {  for(int i=0;i<cookies.length;i++) {  Cookie cookie = cookies[i];  if (cookieName.equals(cookie.getName()))  return(cookie.getValue()); }  return(defaultValue); }}说明:此bean检测cookie中的username,若不为空则说明已登录,反之说明没有登录。方法不够完善,您可以自行扩充。 六、在JRun中建立shopping服务器打开JRun Administrator,新建shopping服务器,这里端口为8101。将上文所述所有编译后的class文件连同org包拷至JRun的shopping服务器所在目录中的classes文件夹下,路径为:C:\JRun4\servers\shopping\default-ear\default-war\WEB-INF\classes\七、建立jsp文件应用DW,在C:\JRun4\servers\shopping\default-ear\default-war\目录下新建如下的jsp文件:index.jsp:<%@ page contentType="text/html;charset=gb2312" pageEncoding="gb2312" %><html><head><title>Shopping123</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><link href="styles/shoppingstyle.css" rel="stylesheet" type="text/css"></head><body bgcolor="#FFFFFF" leftmargin="0" topmargin="0"><jsp:useBean id="checklogin" class="checkLogin" scope="page"/><% boolean login = checklogin.check(request,response);%><table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">  <tr bgcolor="#990000">     <td height="80" colspan="5"><table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">        <tr>          <td width="120"> </td>          <td class="caption">Shopping123</td>          <td width="200"> </td>        </tr>      </table></td>  </tr>  <tr>     <td width="200" align="center" valign="top"><table width="100%" height="20" border="0" cellpadding="0" cellspacing="0">        <tr>          <td> </td>        </tr>      </table>   <%    if(!login){   %>      <table width="90%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">   <form name="form1" method="post" action="/servlet/login">        <tr align="center" bgcolor="#CCCCCC">           <td height="30" colspan="2" class="deepred">卖场入口</td>        </tr>        <tr>           <td width="50%" height="24" align="center" bgcolor="#FFFFFF">会员</td>          <td align="center" bgcolor="#FFFFFF"><input name="username" type="text" id="username" size="10"></td>        </tr>        <tr>           <td height="24" align="center" bgcolor="#FFFFFF">密码</td>          <td align="center" bgcolor="#FFFFFF"><input name="password" type="text" id="password" size="10"></td>        </tr>        <tr>           <td height="24" align="center" bgcolor="#FFFFFF"><a href="reg.jsp" target="_blank" class="red">注册</a></td>          <td align="center" bgcolor="#FFFFFF"><input type="submit" name="Submit" value="进入"></td>        </tr>  </form>      </table>   <%    }   else  {   out.println("您好," + checklogin.getUserName() + "!");  }   %>   </td> <td width="1" valign="top" bgcolor="#CCCCCC"></td>    <td width="400"> </td> <td width="1" valign="top" bgcolor="#CCCCCC"></td>    <td width="200"> </td>  </tr>  <tr align="center" bgcolor="#990000">     <td height="60" colspan="5" class="white">copyright© 2003 Shopping123</td>  </tr></table></body></html>reg.jsp<%@ page contentType="text/html;charset=gb2312" pageEncoding="gb2312" %><html><head><title>Shopping123</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><link href="styles/shoppingstyle.css" rel="stylesheet" type="text/css"></head><body bgcolor="#FFFFFF" leftmargin="0" topmargin="0"><table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">  <tr bgcolor="#990000">     <td height="80" colspan="5"><table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">        <tr>           <td width="120"> </td>          <td class="caption">Shopping123</td>          <td width="200"> </td>        </tr>      </table></td>  </tr>  <tr>     <td width="100" align="center" valign="top"> </td>    <td width="1" valign="top"></td>    <td width="400" align="center" valign="top"><table width="100%" height="20" border="0" cellpadding="0" cellspacing="0">        <tr>          <td> </td>        </tr>      </table>      <table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">   <form action="regpost.jsp" method="post" name="form1">        <tr align="center">           <td height="30" colspan="2" bgcolor="#CCCCCC" class="deepred">会员注册</td>        </tr>        <tr>           <td width="50%" height="24" align="center" bgcolor="#FFFFFF">会员</td>          <td align="center" bgcolor="#FFFFFF"><input name="username" type="text" id="username" size="16"></td>        </tr>        <tr>           <td width="50%" height="24" align="center" bgcolor="#FFFFFF">密码</td>          <td align="center" bgcolor="#FFFFFF"><input name="password" type="password" id="password" size="16"></td>        </tr>        <tr>           <td width="50%" height="24" align="center" bgcolor="#FFFFFF">验证密码</td>          <td align="center" bgcolor="#FFFFFF"><input name="confirm" type="password" id="confirm" size="16"></td>        </tr>        <tr>           <td width="50%" height="24" align="center" bgcolor="#FFFFFF">E-mail</td>          <td align="center" bgcolor="#FFFFFF"><input name="email" type="text" id="email" size="16"></td>        </tr>        <tr>           <td width="50%" height="24" align="center" bgcolor="#FFFFFF"><input type="submit" name="Submit" value="重写"></td>          <td align="center" bgcolor="#FFFFFF"><input type="submit" name="Submit2" value="注册"></td>        </tr>  </form>      </table></td>    <td width="1" valign="top"></td>    <td width="100"> </td>  </tr>  <tr align="center" bgcolor="#990000">     <td height="60" colspan="5" class="white">copyright© 2003 Shopping123</td>  </tr></table></body></html> regpost.jsp:注册表单提交页面<%@ page contentType="text/html;charset=gb2312" pageEncoding="gb2312" %><%@ page import="reg"%><html><head><title>Shopping123</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><link href="styles/shoppingstyle.css" rel="stylesheet" type="text/css"></head><body bgcolor="#FFFFFF" leftmargin="0" topmargin="0"><% String username = new String(request.getParameter("username").getBytes("ISO8859_1")).trim(); String password = new String(request.getParameter("password").getBytes("ISO8859_1")).trim(); String confirm = new String(request.getParameter("confirm").getBytes("ISO8859_1")).trim(); String email = new String(request.getParameter("email").getBytes("ISO8859_1")).trim();%><table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">  <tr bgcolor="#990000">     <td height="80" colspan="5"><table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">        <tr>           <td width="120"> </td>          <td class="caption">Shopping123</td>          <td width="200"> </td>        </tr>      </table></td>  </tr>  <tr>     <td width="100" align="center" valign="top"> </td>    <td width="1" valign="top"></td>    <td width="400" align="center" valign="top"><table width="100%" height="20" border="0" cellpadding="0" cellspacing="0">        <tr>          <td> </td>        </tr>      </table><jsp:useBean id="regID" class="reg" scope="session"/><% if(regID.reg(username,password,confirm,email)) {  out.print("ok");  String newID = regID.getID() + "; %>      <table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">        <tr align="center">           <td height="30" colspan="2" bgcolor="#CCCCCC" class="deepred">恭喜您,注册成功!</td>        </tr>        <tr>           <td width="50%" height="24" align="center" bgcolor="#FFFFFF">编号</td>          <td align="center" bgcolor="#FFFFFF"><%=newID%></td>        </tr>        <tr>           <td width="50%" height="24" align="center" bgcolor="#FFFFFF">会员</td>          <td align="center" bgcolor="#FFFFFF"><%=username%></td>        </tr>        <tr>           <td width="50%" height="24" align="center" bgcolor="#FFFFFF">密码</td>          <td align="center" bgcolor="#FFFFFF"><%=password%></td>        </tr>        <tr>           <td width="50%" height="24" align="center" bgcolor="#FFFFFF">E-mail</td>          <td align="center" bgcolor="#FFFFFF"><%=email%></td>        </tr>      </table><%  out.print("<br>");  out.print("<a href=javascript:window.close()>关闭</a>"); }else{  out.print("注册失败!<br>");  out.print("该用户名已有人使用,请使用另外的用户名!");  out.print("<a href=javascript:history.go(-1)>返回</a>"); } %>   </td>    <td width="1" valign="top"></td>    <td width="100"> </td>  </tr>  <tr align="center" bgcolor="#990000">     <td height="60" colspan="5" class="white">copyright© 2003 Shopping123</td>  </tr></table></body></html> 至此,我们已经完成了一个用户注册、登录的系统。 因为这是本人自己边学边做完成的,所以代码一定有很多不完善的地方,欢迎大家批评指正。 以上所有代码均经本人测试通过。

用户权限管理主要有以下作用: 1. 可以限制用户访问哪些库、哪些表 2. 可以限制用户对哪些表执行SELECT、CREATE、DELETE、DELETE、ALTER等操作 3. 可以限制用户登录的IP或域名 4. 可以限制用户自己的权限是否可以授权给别的用户一、用户授权复制代码 代码如下:mysql> grant all privileges on *.* to 'yangxin'@'%' identified by 'yangxin123456' with grant option; •all privileges:表示将所有权限授予给用户。也可指定具体的权限,如:SELECT、CREATE、DROP等。 •on:表示这些权限对哪些数据库和表生效,格式:数据库名.表名,这里写“*”表示所有数据库,所有表。如果我要指定将权限应用到test库的user表中,可以这么写:test.user •to:将权限授予哪个用户。格式:”用户名”@”登录IP或域名”。%表示没有限制,在任何主机都可以登录。比如:”yangxin”@”192.168.0.%”,表示yangxin这个用户只能在192.168.0IP段登录 •identified by:指定用户的登录密码 •with grant option:表示允许用户将自己的权限授权给其它用户 可以使用GRANT给用户添加权限,权限会自动叠加,不会覆盖之前授予的权限,比如你先给用户添加一个SELECT权限,后来又给用户添加了一个INSERT权限,那么该用户就同时拥有了SELECT和INSERT权限。 用户详情的权限列表请参考MySQL官网说明:http://dev.mysql.com/doc/refman/5.7/en/privileges-provided.html 二、刷新权限对用户做了权限变更之后,一定记得重新加载一下权限,将权限信息从内存中写入数据库。mysql> flush privileges;三、查看用户权限复制代码 代码如下:mysql> grant select,create,drop,update,alter on *.* to 'yangxin'@'localhost' identified by 'yangxin0917' with grant option;mysql> show grants for 'yangxin'@'localhost';四、回收权限删除yangxin这个用户的create权限,该用户将不能创建数据库和表。mysql> revoke create on *.* from '[email protected]';mysql> flush privileges;五、删除用户mysql> select host,user from user;+---------------+---------+| host | user |+---------------+---------+| % | root || % | test3 || % | yx || 192.168.0.% | root || 192.168.0.% | test2 || 192.168.0.109 | test || ::1 | yangxin || localhost | yangxin |+---------------+---------+8 rows in set (0.00 sec)mysql> drop user 'yangxin'@'localhost';六、用户重命名shell> rename user 'test3'@'%' to 'test1'@'%';七、修改密码1> 更新mysql.user表mysql> use mysql;# mysql5.7之前mysql> update user set password=password('123456') where user='root';# mysql5.7之后mysql> update user set authentication_string=password('123456') where user='root';mysql> flush privileges;2> 用set password命令语法:set password for ‘用户名'@'登录地址'=password(‘密码')mysql> set password for 'root'@'localhost'=password('123456');3> mysqladmin语法:mysqladmin -u用户名 -p旧的密码 password 新密码mysql> mysqladmin -uroot -p123456 password 1234abcd注意:mysqladmin位于mysql安装目录的bin目录下八、忘记密码1> 添加登录跳过权限检查配置修改my.cnf,在mysqld配置节点添加skip-grant-tables配置[mysqld]skip-grant-tables2> 重新启动mysql服务shell> service mysqld restart3> 修改密码此时在终端用mysql命令登录时不需要用户密码,然后按照修改密码的第一种方式将密码修改即可。4> 还原登录权限跳过检查配置将my.cnf中mysqld节点的skip-grant-tables配置删除,然后重新启动服务即可。以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

清除浮动//IE浏览器.clearfloat{ zoom:1}//其他浏览器.clearfloat ; after{ display:bloak; clean:both; content:""; visibility: hidden; height:

在连接数据库的url地址有点不一样复制代码 代码如下:jdbc:oracle:thin:@(DESCRIPTION=(LOAD_BALANCE=on)(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=IP1)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=IP2)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=服务名)))url=jdbc:oracle:thin:

分类:腾博会手机版下载

时间:2016-08-22 12:05:08