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

标签 java mysql apache oop servlets

我编写了以下两个类:

设备.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.util.*;
import java.sql.*;

class MainTest{
  public static void main(String[] args) throws SQLException {
    JDBCCommands jdbc = new JDBCCommands();
    //jdbc.Create("Teste novo", "Novo Teste");
    for(Equipment c : jdbc.Show()){
      System.out.print(c.getId() + " ");
      System.out.print(c.getName() + " ");
      System.out.print(c.getLocal() + "\n");

    }
    System.out.println("-------------------------------------");
    for(Equipment c : jdbc.Search("est n" , "est")){
      System.out.print(c.getId() + " ");
      System.out.print(c.getName() + " ");
      System.out.print(c.getLocal() + "\n");

    }
  }
}

问题是当我尝试这个类(class)时:

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>");
    }
}

它显示此错误:

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

    JDBCCommands jdbc = new JDBCCommands();
    ^

符号:类 JDBCCommands

位置:类ListEquipment

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

    JDBCCommands jdbc = new JDBCCommands();
                            ^

符号:类 JDBCCommands

位置:类ListEquipment

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

    for(Equipment c : jdbc.Show()){
        ^

符号:设备类

位置:类ListEquipment

3 个错误

所有文件都位于同一文件夹中,并且名称正确。我该如何解决这个问题?

最佳答案

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

关于Java Servlet - 找不到符号 - 同一文件夹中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52032378/

相关文章:

java - 如何读取类中字段的运行时注释

java - Apache Commons JXPath 搜索在 XPath 搜索期间是否编码对象?

java - 带有通配符和空值的sql语句

java - 生成包含球员姓名、FGM、FGA 和投篮命中率(即 FGP)的输出报告

java - 如何将属性注入(inject)到未实例化的对象中?

MySQL 授予用户权限

javascript - 如何在此 ParamQuery Grid Javascript 中使用 PHP 集成 Mysql 数据

mysql - 它的代码无法更新或插入数据到 mysql 数据库

apache - 排除 htaccess 保护目录中的一个文件夹

windows - 我到处寻找改变Apache上的DocumentRoot但无济于事