我也太遂了! 这个问题也能遇到
我想用同一个包的一个PUBLIC 类的方法
却被告知找不到哪个类, 你们看看
这个程序:
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;
String sql="select goodsname,introduction,unit,company,price,total from goods";
ServletContext application;
public void excuteQury(){
try{
st=ConnectSql.executeQuery(sql);//就提示一个错误, ConnectSql类找不到
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());
}
}
}
但是我用import myclass.ConnectSql; 他仍然提示找不到该类
该类和上面的类是一个包的啊, 怎么搞啊
我给ConnectSql的代码:这两个JAVA文件编译后都放在myclass文件里面
package myclass;
import java.sql.*;
public class ConnectSql{
String driver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=example1";
String user="sa";
String password="ms";
Connection conn=null;
ResultSet rs=null;
Statement stmt=null;
public ConnectSql(){
try{
Class.forName(driver);
}
catch(java.lang.ClassNotFoundException e){
System.out.println("Jdbc_conn():"+e.getMessage());
}
}
public 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 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());
}
}
}
这代码没问题, 我测试过
问题点数:0、回复次数:11Top
1 楼ikevin(菜无心)回复于 2004-09-01 20:27:50 得分 0
st=ConnectSql.executeQuery(sql);//就提示一个错误, ConnectSql类找不到
ConnectSql需要实例化
ConnectSql con=new ConnectSql();
除非ConnectSql内的方法你定义为类方法:加static关键字如:
public static ResultSet executeQuery
这时才可以直接使用类名.方法。Top
2 楼TinyJimmy(Jimmy)回复于 2004-09-01 20:33:25 得分 0
ConnectSql cs = new ConnectSql();
st=cs.executeQuery(sql);Top
3 楼freedom1980(力促)回复于 2004-09-01 20:34:27 得分 0
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;
^
Top
4 楼freedom1980(力促)回复于 2004-09-01 20:36:21 得分 0
还是找不到啊
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
Top
5 楼freedom1980(力促)回复于 2004-09-01 20:42:23 得分 0
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());
}
}
}
Top
6 楼ikevin(菜无心)回复于 2004-09-01 20:43:07 得分 0
上面的回复你看到了么??
看得懂中文吗?
ConnectSql需要实例化:
ConnectSql con=new ConnectSql();
st=con.executeQuery(sql);
Top
7 楼ikevin(菜无心)回复于 2004-09-01 20:47:46 得分 0
真晕了:你要么声明为static
要么实例化。声明了static就不要再实例化了!!
Top
8 楼freedom1980(力促)回复于 2004-09-01 20:55:29 得分 0
天啊我都被搞晕了
我三种方法都试了都出错啊
第一是加static
第而是实力化
第三是即satic 又实力
都WRONGTop
9 楼ikevin(菜无心)回复于 2004-09-01 21:04:34 得分 0
你晕?你晕是正常的,不想看书打基础还想一步登天。浮躁!
说实话,这样帮你都要被好多高手骂死。授人以鱼不如以渔。
我估计你连编译都是犀利糊涂的不知其然,确定CLASSPATH设置
正确,并且2个源文件放在同一包中再编译!
Top
10 楼freedom1980(力促)回复于 2004-09-01 21:26:30 得分 0
大吓, 不好意思我没别的意思
主要搞了一天还是弄不好 是有点急, 呵呵 请包涵
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里面的Top
11 楼ikevin(菜无心)回复于 2004-09-01 21:41:13 得分 0
你试试吧:
1、将D:\tomcat4.1\webapps\examples\WEB-INF\classes\添加到classpath
2、先把JAVA源文件放在myclass中再编译?先,后明白?
不行的话,只能等高手了~Top




