Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Servlet-在同一文件夹中找不到符号-对象_Java_Mysql_Apache_Oop_Servlets - Fatal编程技术网

Java Servlet-在同一文件夹中找不到符号-对象

Java Servlet-在同一文件夹中找不到符号-对象,java,mysql,apache,oop,servlets,Java,Mysql,Apache,Oop,Servlets,我写了以下两节课: Equipment.java class Equipment{ private int id; private String name; private String local; public void setId(int id){ this.id = id; } public void setName(String n){ this.name = n; } public void setLocal(String

我写了以下两节课:

Equipment.java

class Equipment{

  private int id;
    private String name;
    private String local;

  public void setId(int id){
    this.id = id;
  }

  public void setName(String n){
    this.name = n;
  }
  public void setLocal(String l){
    this.local = l;
  }

  public int getId(){
    return this.id;
  }

  public String getName(){
    return this.name;
  }
  public String getLocal(){
    return this.local;
  }

}
JDBCCommands.java

import java.util.*;
import java.sql.*;

class JDBCCommands{
  private static Connection con = new ConnectionFactory().getConnection();

  public static void Create(String name, String local){
    String sql = "insert into equipment (name,local) values (?,?)";
    try{
      PreparedStatement ps = con.prepareStatement(sql);
      ps.setString(1, name);
      ps.setString(2, local);
      ps.execute();
      ps.close();
    }catch(SQLException erro_sql){
      throw new RuntimeException(erro_sql);
    }
  }

  public static List<Equipment> Show(){
    List <Equipment> Components = new ArrayList <Equipment> ();
    String sql = "select * from equipment";
    try{
      PreparedStatement ps = con.prepareStatement(sql);
      ResultSet rs = ps.executeQuery();
      while(rs.next()) {
        Equipment e = new Equipment();
        e.setId(Integer.parseInt(rs.getString("equip_id"))  );
        e.setName(rs.getString("nome"));
        e.setLocal(rs.getString("local"));
        Components.add(e);
      }
      rs.close();
      ps.close();
      return Components;
    }catch(SQLException erro_sql){
      throw new RuntimeException(erro_sql);
    }
  }

  public static void Update(int id, String name, String local){
    String sql = "update equipment set name = ?, local = ? where equip_id = ?";
    try{
      PreparedStatement ps = con.prepareStatement(sql);
      ps.setString(1, name);
      ps.setString(2, local);
      ps.setInt(3, id);
      ps.execute();
      ps.close();
    }catch(SQLException erro_sql){
      throw new RuntimeException(erro_sql);
    }
  }

  public static void Delete(int id){
    String sql = "delete from equipment where equip_id = ?";
    try{
      PreparedStatement ps = con.prepareStatement(sql);
      ps.setInt(1, id);
      ps.execute();
      ps.close();
    }catch(SQLException erro_sql){
      throw new RuntimeException(erro_sql);
    }
  }

  public static List<Equipment> Search(String name, String local){
    List <Equipment> Components = new ArrayList <Equipment> ();
    String sql = "select * from equipment where name like ? and local like ?";
    try{
      PreparedStatement ps = con.prepareStatement(sql);
      ps.setString(1, "%" + name + "%");
      ps.setString(2, "%" + local + "%");
      ResultSet rs = ps.executeQuery();
      while(rs.next()) {
        Equipment e = new Equipment();
        e.setId(Integer.parseInt(rs.getString("equip_id"))  );
        e.setName(rs.getString("nome"));
        e.setLocal(rs.getString("local"));
        Components.add(e);
      }
      rs.close();
      ps.close();
      return Components;
    }catch(SQLException erro_sql){
      throw new RuntimeException(erro_sql);
    }
  }
}
问题是当我尝试这个课程时:

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

import java.sql.*;

class ListEquipment extends HttpServlet {
  protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        PrintWriter out = response.getWriter();
        JDBCCommands jdbc = new JDBCCommands();

        System.out.println("<table style=\"width:100%\">");
        for(Equipamento c : jdbc.Show()){
          System.out.println("<tr>");
          System.out.print("<th>" + c.getId() + "</th>");
          System.out.print("<th>" + c.getName() + "</th>");
          System.out.print("<th>" + c.getLocal() + "</th>");
          System.out.println("</tr>");
        }
        System.out.println("</table>");
    }
}
符号:类jdbc命令

位置:class ListEquipment

java:13:错误:找不到符号

    JDBCCommands jdbc = new JDBCCommands();
    ^
    JDBCCommands jdbc = new JDBCCommands();
                            ^
    for(Equipment c : jdbc.Show()){
        ^
符号:类jdbc命令

位置:class ListEquipment

java:16:错误:找不到符号

    JDBCCommands jdbc = new JDBCCommands();
    ^
    JDBCCommands jdbc = new JDBCCommands();
                            ^
    for(Equipment c : jdbc.Show()){
        ^
符号:等级设备

位置:class ListEquipment

3个错误


所有文件都在同一文件夹中,且名称正确。如何修复此问题?

我只需要将与MariaDB连接的.jar文件放在lib文件夹中,现在一切正常。

也许可以公开您的类。检查servlet和JDBCCommand类是否在同一个包中,或者是否清理您的项目。在eclipse中,转到Project->clean。在intelij中,转到文件->使缓存无效。然后编译你的项目。我希望这有帮助you@DevilsHnd我公开了这些类,但它不起作用。@Prabath jdbc命令与servlet位于完全相同的文件夹中。