当前位置:首页 > 技术问答集
firefox

mysql在j2ee服务器当中为何不能正常连接?

 所属目录:asp.net   |   类型:技术问答   |   时间:2007-05-12
 问题:

文件如下  
  package   lib;  
  import   java.sql.*;  
  import   java.io.*;  
  import   javax.sql.*;  
   
  public   class     login  
  {  
  user   user;  
  boolean   flag=false;  
          connection   con=null;  
  statement   st=null;  
  resultset   rs=null;  
   
  public   login()  
  {try{  
   
  class.forname("com.mysql.jdbc.driver");//这一行抛出异常,但是在我实际测试时,如果不在j2ee容器当中时,这个文件运行正常,可以返回正确的结果。  
   
  con=drivermanager.getconnection("jdbc:mysql://localhost/ghf");  
   
  st=con.createstatement();  
          }catch(exception   e)  
  {system.out.println("连接失败!");}  
  }  
   
  public   void   setuser(user   user)  
  {  
  this.user=user;  
  try{  
  rs=st.executequery("select   *   from   user   where   name=eoe   and   password=eoe");  
  if(rs.next())  
  {this.flag=true;  
  //system.out.println(new   string(rs.getstring(1).getbytes("iso-8859-1"),"gb2312"));  
  }  
  else  
  {this.flag=false;  
  }  
  }catch(exception   e)  
  {system.out.println("自形时出错!");}  
  finally  
  {try{  
  con.close();  
  st.close();  
  rs.close();  
  }catch(exception   e)  
  {system.out.println("关闭连接时出错!");}  
  }  
  }  
  public     void   setflag(boolean   flag)  
  {  
  this.flag=flag;  
  }  
   
  public   user   getuser()  
  {  
  return   user;  
  }  
  public   boolean   isflag()  
  {  
  return   flag;  
  }  
   
  public   static   void   main(string[]   args)    
  {  
  //new   login().setuser();  
  }  
  }  
  user.java  
  package   lib;  
   
  public   class     user  
  {  
  string   name;  
  string   password;  
   
  public     void   setname(string   name)  
  {  
  this.name=name;  
  }  
  public   void   setpassword(string   password)  
  {  
  this.password=password;  
  }  
   
  public   string   getname()  
  {  
  return   name;  
  }  
  public   string   getpassword()  
  {  
  return   password;  
  }  
   
  public   static   void   main(string[]   args)    
  {  
  }  
  }  
  login.jsp  
  <!doctype   html   public   "-//w3c//dtd   html   4.0   transitional//en">  
  <html>  
  <head>  
  <title>   new   document   </title>  
  <meta   name="generator"   content="editplus">  
  <meta   name="author"   content="">  
  <meta   name="keywords"   content="">  
  <meta   name="description"   content="">  
  </head>  
   
  <body>  
  <%!  
  string   name;  
  string   password;  
  %>  
  <%  
  name=request.getparameter("name").trim();//得到用户的输入  
  password=request.getparameter("password").trim();  
  %>  
   
  <jsp:usebean   id="user"   class="lib.user"/>  
  <jsp:setproperty   name="user"   property="name"   value="eoe"   />  
   
  <jsp:setproperty   name="user"   property="password"   value="eoe"/>  
  <jsp:usebean   id="login"   class="lib.login"/>  
   
   
  <%  
  login.setuser(user);  
  if(login.isflag())  
  {//登陆成功//相应的处理  
  out.println("ok");%><%="ok"%><%  
  }  
  else  
  {//用户不存在或者密码不对//相应的处理  
  out.println("no");%><%  
  }  
  %>  
  <jsp:getproperty   name="login"   property="user"     />  
  <%=name%>  
  <%=password%>  
  </body>  
  </html>  
  那位大哥帮帮忙,解释一下这是为何?

· 网友精彩回答:

发表者:usherlight(xingtian)

在j2ee容器中,   没有把mysql-connector.jar放在类路径中。

.
处理 SSI 文件时出错
© 2006-2008 All Rights Reserved