同一个包内的两个类为什么无法正常调用?

ecaol 2005-06-01 02:06:03
想在一个类里面调用另一个类,在编译主调类的时候系统提示找不到被调用的类:

DisServlet.java:37:cannot resolve symbol
symbol : class DBbean
location : class mypack.DisServlet

DBbean db = new DBbean();

在DisServlet类里面要用到DBbean,DBbean已经编译完成

在运行javac DisServlet.java的时候,出现以上的出错信息,请高手帮忙!
...全文
518 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
chinamao 2005-06-01
  • 打赏
  • 举报
回复
要import吧,或者写出完整的包名.类名
dmy22271 2005-06-01
  • 打赏
  • 举报
回复
我也出现这种情况,一个bean调另一bean总是那个调别的bean的bean不好使,呵呵说的有点像绕口令
ecaol 2005-06-01
  • 打赏
  • 举报
回复
DBbean.java源代码

package mypack;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;

public class DBbean{

//数据库连接字符串
String url ="jdbc:postgresql://192.168.0.40:5432/test" ;
String user="postgre";
String psw="postgre";
Statement stmt = null;
Connection conn = null;
ResultSet rs = null;

//从数据库取出的value
String username = "abc";
String mailaddress = null;
String pw = null;

public ResultSet executeQuery(String sqlString){

try{
//加载驱动程序
Class.forName("org.postgresql.Driver").newInstance();
conn= DriverManager.getConnection(url,user,psw);
stmt=conn.createStatement();
rs=stmt.executeQuery(sqlString);

}catch (Exception e){}
return rs;
}
}
ecaol 2005-06-01
  • 打赏
  • 举报
回复
DispatcherServlet.java源代码

package mypack;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;

public class DispatcherServlet extends HttpServlet{

private String target="/hello.jsp";

public void init(ServletConfig config)
throws ServletException{
uper.init(config);
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,IOException{

doPost(request,response);
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,IOException{

//get id
String id=request.getParameter("id");
//get password
String password=request.getParameter("password");
//从数据库取出的value
String username = "abc";
String mailaddress = null;
String pw = null;



String sqlString = "SELECT name,password,mailaddress FROM test Where id='" + id + "'";

DBbean db = new DBbean();

ResultSet rs = db.executeQuery(sqlString);

while(rs.next()){
username = rs.getString("name");
pw = rs.getString("password");
mailaddress = rs.getString("mailaddress");
}





if(password.equals(pw)){

//Add the user to the request
request.setAttribute("USER",username);
request.setAttribute("MAILADDRESS",mailaddress);

//Forward the request to the target named
ServletContext context=getServletContext();

System.out.println("Redirecting to" + target);
RequestDispatcher dispatcher=context.getRequestDispatcher(target);
dispatcher.forward(request,response);
}else{
//Forward the request to the target named
ServletContext context=getServletContext();

System.out.println("Redirecting to /login.jsp");
RequestDispatcher dispatcher=context.getRequestDispatcher("/login.jsp");
dispatcher.forward(request,response);
}
}
}
ecaol 2005-06-01
  • 打赏
  • 举报
回复
这两个类是在一个包里面的(mypack),DisServlet调用DBbean类里面的连接数据库的方法.
ariel_521 2005-06-01
  • 打赏
  • 举报
回复
DBbean db = new DBbean();这是啥意思?是你写的还是系统提示的???
这两各类应该在一个包里
foxty 2005-06-01
  • 打赏
  • 举报
回复
javac -cp . DisServlet.java
jFresH_MaN 2005-06-01
  • 打赏
  • 举报
回复
这种路径问题一般只要把两个包的根目录放在同一个目录里面就可以了

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧