首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

如何和数据库连接

2012-01-05 
怎么和数据库连接 ?如何将登陆截面和数据库连接呢?jsp:useBeanid alogin scope page class org.p

怎么和数据库连接 ?
如何将登陆截面和数据库连接呢?
<jsp:useBean   id= "alogin "   scope= "page "   class= "org.pan.web.login "   />
<%
String   mesg   =   " ";

if(   request.getParameter( "username ")!=null   &&   !request.getParameter( "username ").equals( " ")){
String   username   =request.getParameter( "username ");
String   passwd   =   request.getParameter( "passwd ");
username   =   new   String(username.getBytes( "ISO8859-1 "));
passwd   =   new   String(passwd.getBytes( "ISO8859-1 "));
alogin.setUsername(username);
alogin.setPasswd(passwd);
if   (alogin.excute()){
session.setAttribute( "username ",username);
String   userid   =   Long.toString(alogin.getUserid());
session.setAttribute( "userid ",userid);
response.sendRedirect( "userinfo.jsp ");
%>
<%
}else   {
mesg   =   "登录出错! ";
}
}
%>

<!DOCTYPE   HTML   PUBLIC   "-//W3C//DTD   HTML   4.01   Transitional//EN ">


<html>
    <head>
        <base   href= " <%=basePath%> ">
       
        <title> Bookstore   login.jsp </title>
       
<meta   http-equiv= "pragma "   content= "no-cache ">
<meta   http-equiv= "cache-control "   content= "no-cache ">
<meta   http-equiv= "expires "   content= "0 ">        
<meta   http-equiv= "keywords "   content= "keyword1,keyword2,keyword3 ">
<meta   http-equiv= "description "   content= "This   is   my   page ">
<!--
<link   rel= "stylesheet "   type= "text/css "   href= "styles.css ">
-->
<script   language= "javascript ">

  function   checkform()   {
if   (document.form1.username.value== " "   ||   document.form1.passwd.value== " "){
alert( "用户名或密码为空! ");
return   false;
}
return   true;

    }

</script>
    </head>
   
    <body>
        <body   bgcolor= "#FFCC99 "   text= "#000000 ">
<div   align= "center ">
    <table   width= "750 "   border= "0 "   cellspacing= "1 "   cellpadding= "1 ">
        <tr>  
            <td   width= "120 "   align= "center "> <img   src= "images/Img20625.jpg "   width= "50 "   height= "50 "> <br>
                <font   color= "#0066cc "> &#32593;&#32476;&#30005;&#23376;&#20070;&#22478; </font> <br> </td>
            <td   align= "center "> <img   src= "images/baners2.gif "   width= "468 "   height= "60 "> </td>


            <td   align= "center "   width= "80 "> <a   href= "# "> 本站公告 </a> <br>
                <br>
            </td>
        </tr>
    </table>

    <table   width= "750 "   border= "0 "   cellspacing= "1 "   cellpadding= "1 ">
        <tr>  
            <td   width= "200 "> &nbsp; </td>
            <td   width= "55 "> <a   href= "index.jsp "> 首页 </a> </td>
            <td   width= "100 "> <a   href= "booklist.jsp "> 在线购物 </a> </td>
            <td   width= "100 "> <a   href= "shoperlist.jsp "> 我的购物车 </a> </td>
            <td   width= "100 "> <a   href= "userinfo.jsp "> 用户信息 </a> </td>
            <td> &nbsp; </td>
        </tr>
    </table>
   
    <form   name= "form1 "   method= "post "   action= "login.jsp ">
        <table   width= "400 "   border= "0 "   cellspacing= "1 "   cellpadding= "1 ">
        <tr>  
            <td   width= "147 "   align= "right "> 用户名: <br>
            </td>
            <td   width= "246 "   valign= "top ">
                    <input   type= "text "   name= "username "   size= "16 "   maxlength= "25 ">
                </td>
        </tr>
        <tr>  
            <td   width= "147 "   align= "right "> 密码: </td>
            <td   width= "246 "   valign= "top ">
                    <input   type= "password "   name= "passwd "   maxlength= "20 "   size= "16 ">
                </td>
        </tr>
        <tr>  
            <td   width= "147 "   align= "right "> &nbsp; </td>
            <td   width= "246 "   valign= "top ">
                    <input   type= "submit "   name= "Submit "   value= "登录 "   onclick= "javascript:return(checkform()); ">


                    <input   type= "reset "   name= "Submit2 "   value= "取消 ">
                </td>
        </tr>
        <tr>  
            <td   colspan= "2 "   align= "center ">
                <p> &nbsp; </p>
                <p> 如果你还不是本站用户,请在此 <a   href= "reg.jsp "> 注册 </a> </p>
            </td>
        </tr>
    </table>
        </form>
    <p> &nbsp; </p> <table   width= "449 "   border= "0 "   cellspacing= "2 "   cellpadding= "2 "   height= "58 ">
        <tr>
            <td   align= "center "> &#27993;&#27743;&#29702;&#24037;&#22823;&#23398;&#20449;&#30005;&#23398;&#38498; <br> 06.&#35745;&#31639;&#26426;&#24212;&#29992; <br> &nbsp;  
                <br>  
                </td>
        </tr>
    </table>
   
</div>
    </body>
</html>
这段代码什么意思呢,尤其是html前的那部分亚

[解决办法]
htm前面的是用JavaBean实现数据库的连接一定会有一个名为org.pan.web.login的class.他有username和passwd 两个私有变量和他们的sertter和getter方法.还有一个excute()方法,我想是用 "select * from <table> where username=*** and passwd=*** "判断是否该拥护注册的,返回一个BOOL.username = new String(username.getBytes( "ISO8859-1 "));
passwd = new String(passwd.getBytes( "ISO8859-1 "));是编码转换.

热点排行