没有连上数据库啊,sql的dll文件放到类所在文件夹里试一下~可能在那个类里看不到~

解决方案 »

  1.   

    那为什么在main函数里面的连上呢,main与doGet属于同一个类,若一个能连上,那么另一个应该也能连上
      

  2.   

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;public class DBCon {
    public static String driver="oracle.jdbc.driver.OracleDriver";
    public static String url="jdbc:oracle:thin:@192.168.0.174:1521:ORCL";
    public static String user="scott";
    public static String pwd="tiger";

    public static Connection getConnection(){
    try {
    Class.forName(driver);
    Connection con=DriverManager.getConnection(url, user, pwd);
    System.out.println("con success");
    return con;
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return null;
    }

    public static void close(Connection con,Statement stm,ResultSet rs){
    if(con!=null){
    try {
    con.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    if(stm!=null){
    try {
    stm.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    if(rs!=null){
    try {
    rs.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }

    public static void main(String[] args) {
    DBCon.getConnection();
    }
    }
      

  3.   

    不是.dll 文件,我想应当是jar 包,web应用的jar包要放到lib目录下,或者看看是不是jar包同时有不同版本.
      

  4.   

    不一定,你这里的else也会跳到那里,
    if(npassword.equals(password)){
                    request.getRequestDispatcher("/main.jsp").forward(request, response);
                  }else{
                     // request.getRequestDispatcher("/main.jsp").forward(request, response);
                     response.sendRedirect("http://localhost:8080/HelleWorld/main.jsp");
                  }你可以:
    if(conn==null){
           System.out.println("天哪,为什么我就是取不到!");
                  response.sendRedirect("http://localhost:8080/HelleWorld/register.jsp?name=jiang5495");
              }
    看看这句输出没有?
      

  5.   

    到Tomcat的项目里的lib里看看有没有你想要的包,看看后台报什么错
      

  6.   


    这一句有输出:输出如下
    at java.lang.Thread.run(Unknown Source)
    you who you
    天哪,为什么我就是取不到!
    java.lang.IllegalStateException
    at org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:435)
    at login.doPost(login.java:30)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
      

  7.   

    debug 一下 好不 ~~~~
      

  8.   

    因为main和serlvet使用的classpath不同,driver在servlet环境中没有进入classpath.
      

  9.   

    这个真的很奇怪噢
    楼主能不能看看
    DataBase.getConnection();
    这个方法的代码啊?
    怀疑使用了静态变量,而在别的地方把conn设为null了
      

  10.   

    public static Connection getConnection(){
    Connection conn=null;
    String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";   
    String url = "jdbc:microsoft:sqlserver://localhost:1433;databasename=temp";   
        String uName = "sa";   
        String uPwd = "123456";   
    try{
    Class.forName(driver);
    conn=DriverManager.getConnection(url,uName,uPwd);
    }catch(ClassNotFoundException e){
    e.printStackTrace();
    }catch(SQLException e2){
    e2.printStackTrace();
    }
    if(conn==null){
    System.out.println("you who you");
    }
    return conn;
    }
      

  11.   

    catch(SQLException e2){
    e2.printStackTrace();

    ->看不出什么问题,这样看看有没有其他异常呢?
    catch(SQLException e2){
    e2.printStackTrace();
    } catch(Exception e3){
    e3.printStackTrace();
      

  12.   


    我觉得应该是这个原因,因为我碰过类似的问题LZ到tomcat里看看对应的包有没有,或者包冲突了
      

  13.   

    我迷糊了,可是上面说的tomcat执行的时候没有lib包的话,应该会报ClassNotFoundException 的啊。
      

  14.   

    涉及到的主要java程序如下,
    我想其它jsp页面应该不会有错吧
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    import jiang5495.util.*;import java.sql.*;public class login extends HttpServlet{

    public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
      String name=request.getParameter("name");
      String password=request.getParameter("password");
      Connection conn=null;
      Statement stmt=null;
      ResultSet rs=null;
     
      conn=DataBase.getConnection();
      if(conn==null){
      response.sendRedirect("http://localhost:8080/HelleWorld/register.jsp?name=jiang5495");
      }
      try{

      /*
      if(conn==null){
      response.sendRedirect("http://localhost:8080/HelleWorld/main.jsp?name=jiang5495");
      }
      */
      if(conn==null){ 
          System.out.println("天哪,为什么我就是取不到!"); 
                  response.sendRedirect("http://localhost:8080/HelleWorld/register.jsp?name=jiang5495"); 
              }    
       stmt=conn.createStatement();
      String sql="select password from student where name='"+password+"'";
       rs=stmt.executeQuery(sql);
      String npassword="iyuihjknhjkhxuihuihjhl";
      while(rs.next()){
      npassword=rs.getString(1);
      }
      if(npassword.equals(password)){
    request.getRequestDispatcher("/main.jsp").forward(request, response);
      }else{
     // request.getRequestDispatcher("/main.jsp").forward(request, response);
         response.sendRedirect("http://localhost:8080/HelleWorld/main.jsp");
      }
      }catch(Exception e){
      e.printStackTrace();
      }
    }

    public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
    doPost(request,response);
    }
    /*
    public static void main(String[] args){
     
      
      Connection conn=null;
      Statement stmt=null;
      ResultSet rs=null;
      conn=DataBase.getConnection();
      try{
      stmt=conn.createStatement();
      String sql2="select * from bkliuhecai";
      rs=stmt.executeQuery(sql2);
      while(rs.next()){
      System.out.println(rs.getString(1));
      }
      }catch(Exception e){
      e.printStackTrace();
      }
    }
    */
    }
    package jiang5495.util;
    import java.sql.*;
    public class DataBase {

    public static Connection getConnection(){
    Connection conn=null;
    String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";   
    String url = "jdbc:microsoft:sqlserver://localhost:1433;databasename=temp";   
        String uName = "sa";   
        String uPwd = "123456";   
    try{
    Class.forName(driver);
    conn=DriverManager.getConnection(url,uName,uPwd);
    }catch(ClassNotFoundException e){
    e.printStackTrace();
    }catch(SQLException e2){
    e2.printStackTrace();
    }
    if(conn==null){
    System.out.println("you who you");
    }
    return conn;
    }
    public static void main(String[] args){
    Connection conn=DataBase.getConnection();
    Statement stmt=null;
    ResultSet rs=null;
    System.out.println("you are not a good boy!");
           try{
            stmt=conn.createStatement();
            String sql="select * from web";
            rs=stmt.executeQuery(sql);
            while(rs.next()){
            System.out.println(rs.getString(1));
            }
            
           }catch(Exception e3){
            e3.printStackTrace();
           }
    }}
      

  15.   

    我决定开jb测试下连oracle,你的login的jsp可以共享不啊?我懒得写了。。
      

  16.   

    楼主,确实是你引入的jar的问题。
    你可能添加jar的时候,是:工程右键->properties->java build path->Libraries->add jars...的方式,从外面某个目录(不是你的WEB-INF下面的lib)把sqlserver的lib弄进来的。
    解决方法如下:
    把那个sqlserver的驱动包,放到你的WEB-INF下的lib下,然后,把javabuildpath里面上面说到的那种方式下的jar删了。然后,再右键工程-》refresh,然后再试,肯定可以了。我刚才这么重现了你的问题,也通过这种方式解决了。不过我刚才那里报了驱动找不到,你这里怎么没报就奇怪了。,
      

  17.   

     我之间遇到的连接数据库的错误(以连接SQL Server 2000为例)
       1. sql没有升级sql server;
       2. 没有导入sql连接的三个jar;
       3. 没有创建数据库;
       4. 没有设置好classpath;lz的问题可以试试这样:
        建议不要把数据库连接写在dopost()中.
       
      

  18.   

    在两个不同的函数里面conn却会有不同的值?? 
    这是个肯定句,在两个不同的函数里面conn会有不同的值。因为你这个2个conn是不同的内存单元,除非你把conn设置在函数外,用static修饰它就是一个值了。
      if(conn==null){
                  response.sendRedirect("http://localhost:8080/HelleWorld/register.jsp?name=jiang5495");
              }
              try{
                
                  /*
                  if(conn==null){
                      response.sendRedirect("http://localhost:8080/HelleWorld/main.jsp?name=jiang5495");
                  }
                  */
                  if(conn==null){ 
                      System.out.println("天哪,为什么我就是取不到!"); 
                              response.sendRedirect("http://localhost:8080/HelleWorld/register.jsp?name=jiang5495"); 
                          } 这个地方怎么觉得你的if语句是一个意思,你怎么写了3遍
      

  19.   

    application和servlet是不一样的环境的。
    1.application运行成功说明程序没问题。那么就是环境的问题了。
    2.servlet的话,就要把数据库的驱动包放在tomcat的lib下。
      

  20.   

    谁知道你Connection是怎么实现的?DataBase.getConnection()是否报错了,即出现ClassNotFoundException异常?或者什么时候自己释放掉了?如果是出现ClassNotFoundException异常,出现的原因就是你jar包放错了地方!那就需要将sql的jar包放到lib下面,或者放到tomcat/common/lib里
      

  21.   

    DataBase.getConnection()不知道LZ这个是怎么写的。。开放代码给大家看看吧。
      

  22.   

    初始化connection的代码,你应该写在init方法里。
      

  23.   

    servlet,doPost,和doGet,是接受前台 post和get的请求,并返回结果。
    你直接写main函数调用,当然不行。相当于没有调用的。如果你写在其他的函数里,
    我想是可以获取连接的。
    希望你试试。