st=ConnectSql.executeQuery(sql);//就提示一个错误, ConnectSql类找不到
ConnectSql需要实例化
ConnectSql con=new ConnectSql();除非ConnectSql内的方法你定义为类方法:加static关键字如:
public static ResultSet executeQuery这时才可以直接使用类名.方法。

解决方案 »

  1.   

    ConnectSql cs = new ConnectSql();
    st=cs.executeQuery(sql);
      

  2.   

    ConnectSql.java:39: non-static variable stmt cannot be referenced from a static
    context
                     stmt=conn.createStatement();
                     ^
    ConnectSql.java:39: non-static variable conn cannot be referenced from a static
    context
                     stmt=conn.createStatement();
                          ^
    ConnectSql.java:40: non-static variable rs cannot be referenced from a static co
    ntext
                       rs=stmt.executeQuery(sql);
                       ^
    ConnectSql.java:40: non-static variable stmt cannot be referenced from a static
    context
                       rs=stmt.executeQuery(sql);
                          ^
    ConnectSql.java:46: non-static variable rs cannot be referenced from a static co
    ntext
               return rs;
                      ^
      

  3.   

    还是找不到啊
    D:\tomcat4.1\webapps\examples\WEB-INF\classes\myclass>javac Lookup.java
    Lookup.java:18: cannot resolve symbol
    symbol  : variable ConnectSql
    location: class myclass.Lookup
                    st=ConnectSql.executeQuery(sql);
                       ^
    1 error
      

  4.   

    package myclass;
    import java.sql.*;public class ConnectSql{
      static String driver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
       static String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=example1";
       static String user="sa";
       static String password="ms";
       static Connection conn=null;
       static ResultSet rs=null;
       static Statement stmt=null;
       public ConnectSql(){
       try{
       Class.forName(driver);
       }
       catch(java.lang.ClassNotFoundException e){
       System.out.println("Jdbc_conn():"+e.getMessage());
       }
       }
     
       public static void executeUpdate(String sql)throws Exception{
      
       try{
       conn=DriverManager.getConnection(url,user,password);
      stmt=conn.createStatement();
       stmt.executeUpdate(sql);
       
       }
       catch(SQLException ex){
       System.out.println("sql.executeUpdate:"+ex.getMessage());
       }
       }   public static ResultSet executeQuery(String sql)throws Exception{
       rs=null;
       try{

       conn=DriverManager.getConnection(url,user,password);
     stmt=conn.createStatement();
       rs=stmt.executeQuery(sql);
       
       }
       catch(SQLException ex){
       System.out.println("sql.executeQuery:"+ex.getMessage());
       }
       return rs;   
       }
       
       public void closeStmt(){
    try{
    stmt.close();
         }catch(SQLException e){
    System.out.println("closeStmt error:"+e.getMessage());
       }
       }
    public void closeConn(){
    try{
    conn.close();
        }catch(SQLException e){
    System.out.println("closeConn error:"+e.getMessage());

          }
    }
    }
      package myclass;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.sql.*;
    public class Lookup {
    String goodsname;
    String introduction;
    String unit;
    String company;
    Float  price;
    Integer total;
    Statement stmt=null;
    ResultSet st=null;
    ConnectSql cql=new ConnectSql();//加了这一句还是没用啊
    String sql="select goodsname,introduction,unit,company,price,total from goods";
    ServletContext application;
    public void excuteQury1(){
    try{
    st=cql.executeQuery(sql);
    if(st.next()){ application.setAttribute("goodsname",st.getString("goodsname"));
    application.setAttribute("introduction",st.getString("introduction"));
    application.setAttribute("unit",st.getString("unit"));
    application.setAttribute("company",st.getString("company"));
    application.setAttribute("price",st.getString("price"));
    application.setAttribute("total",st.getString("total"));

    }
    stmt.close();
    st.close();
      }catch(SQLException e){
       System.out.println("the sql error:"+e.getMessage());
    }

    }
    }
      

  5.   

      上面的回复你看到了么??
      
      看得懂中文吗?ConnectSql需要实例化:ConnectSql con=new ConnectSql();
    st=con.executeQuery(sql);
      

  6.   

      真晕了:你要么声明为static  要么实例化。声明了static就不要再实例化了!!
      

  7.   

    天啊我都被搞晕了
    我三种方法都试了都出错啊
    第一是加static
    第而是实力化
    第三是即satic 又实力
    都WRONG
      

  8.   

        你晕?你晕是正常的,不想看书打基础还想一步登天。浮躁!    说实话,这样帮你都要被好多高手骂死。授人以鱼不如以渔。   我估计你连编译都是犀利糊涂的不知其然,确定CLASSPATH设置
    正确,并且2个源文件放在同一包中再编译!
      

  9.   

    大吓, 不好意思我没别的意思
    主要搞了一天还是弄不好 是有点急, 呵呵 请包涵
    set classpath=.;d:\jdk1.4\lib\tools.jar;d:\jdk1.4\jre\lib\ext\servlet.jar;C:\Program Files\Microsoft SQL Server 2000 Driver for JDBC\lib\msutil.jar;C:\Program Files\Microsoft SQL Server 2000 Driver for JDBC\lib\mssqlserver.jar;C:\Program Files\Microsoft SQL Server 2000 Driver for JDBC\lib\msutil.jar
    这两个程序编译后都放在D:\tomcat4.1\webapps\examples\WEB-INF\classes\myclass里面的
      

  10.   


      你试试吧:
    1、将D:\tomcat4.1\webapps\examples\WEB-INF\classes\添加到classpath
    2、先把JAVA源文件放在myclass中再编译?先,后明白?  不行的话,只能等高手了~