I created these two classes:
Equipment.java
class Equipamento{
private int id;
private String nome;
private String local;
public void setId(int id){
this.id = id;
}
public void setNome(String n){
this.nome = n;
}
public void setLocal(String l){
this.local = l;
}
public int getId(){
return this.id;
}
public String getNome(){
return this.nome;
}
public String getLocal(){
return this.local;
}
}
JDBCComandos.java
import java.util.*;
import java.sql.*;
class JDBCComandos{
private static Connection con = new ConnectionFactory().getConnection();
public static void Create(String nome, String local){
String sql = "insert into equipamento (nome,local) values (?,?)";
try{
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, nome);
ps.setString(2, local);
ps.execute();
ps.close();
}catch(SQLException erro_sql){
throw new RuntimeException(erro_sql);
}
}
public static List<Equipamento> Retorna(){
List <Equipamento> Componentes = new ArrayList <Equipamento> ();
String sql = "select * from equipamento";
try{
PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while(rs.next()) {
Equipamento e = new Equipamento();
e.setId(Integer.parseInt(rs.getString("equip_id")) );
e.setNome(rs.getString("nome"));
e.setLocal(rs.getString("local"));
Componentes.add(e);
}
rs.close();
ps.close();
return Componentes;
}catch(SQLException erro_sql){
throw new RuntimeException(erro_sql);
}
}
public static void Update(int id, String nome, String local){
String sql = "update equipamento set nome = ?, local = ? where equip_id = ?";
try{
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, nome);
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 equipamento 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<Equipamento> retornaEspecifico(String nome, String local){
List <Equipamento> Componentes = new ArrayList <Equipamento> ();
String sql = "select * from equipamento where nome like ? and local like ?";
try{
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, "%" + nome + "%");
ps.setString(2, "%" + local + "%");
ResultSet rs = ps.executeQuery();
while(rs.next()) {
Equipamento e = new Equipamento();
e.setId(Integer.parseInt(rs.getString("equip_id")) );
e.setNome(rs.getString("nome"));
e.setLocal(rs.getString("local"));
Componentes.add(e);
}
rs.close();
ps.close();
return Componentes;
}catch(SQLException erro_sql){
throw new RuntimeException(erro_sql);
}
}
}
I did a test in the following class, and everything worked perfectly:
import java.util.*;
import java.sql.*;
class MainTest{
public static void main(String[] args) throws SQLException {
JDBCComandos jdbc = new JDBCComandos();
//jdbc.Create("Teste novo", "Novo Teste");
for(Equipamento c : jdbc.Retorna()){
System.out.print(c.getId() + " ");
System.out.print(c.getNome() + " ");
System.out.print(c.getLocal() + "\n");
}
System.out.println("-------------------------------------");
for(Equipamento c : jdbc.retornaEspecifico("este n" , "este")){
System.out.print(c.getId() + " ");
System.out.print(c.getNome() + " ");
System.out.print(c.getLocal() + "\n");
}
}
}
The problem was when I tried to compile this class:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.sql.*;
class ListEquipamento extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
PrintWriter out = response.getWriter();
JDBCComandos jdbc = new JDBCComandos();
System.out.println("<table style=\"width:100%\">");
for(Equipamento c : jdbc.Retorna()){
System.out.println("<tr>");
System.out.print("<th>" + c.getId() + "</th>");
System.out.print("<th>" + c.getNome() + "</th>");
System.out.print("<th>" + c.getLocal() + "</th>");
System.out.println("</tr>");
}
System.out.println("</table>");
}
}
I received the following error:
ListEquipamento.java:13: error: cannot find symbol
JDBCComandos jdbc = new JDBCComandos();
^
symbol: class JDBCComandos
location: class ListEquipamento
ListEquipamento.java:13: error: cannot find symbol
JDBCComandos jdbc = new JDBCComandos();
^
symbol: class JDBCComandos
location: class ListEquipamento
ListEquipamento.java:16: error: cannot find symbol
for(Equipamento c : jdbc.Retorna()){
^
symbol: class Equipamento
location: class ListEquipamento
3 errors
Why is this happening because all are in exactly the same folder and the MainTest class has no problem finding the classes?