How to resolve the following error: HTTP Status 500- Internal Server Error

0

I'm trying to make a web application using java, jsp, servlets and database. But when I run as in addClient.jsp Class this error appears:

  

HTTP Status 500 - Internal Server Error.

I have already looked at the internet on how to solve it and I can not find a solution. Thanks for the help.

I created a Contact Class, a CreateClient Servlet, ConnectionFactory, ContactDAO, and addClient.jsp.

I put the code for some of the classes

Contact class:

public class Contact {
    private String name;
    private String email;
    private String address;
    private int telephoneNumber;

    public Contact() {

    }
    public Contact (String name, String email, String address, int phone) {
        this.name=name;
        this.email=email;
        this.address=address;
        this.telephoneNumber=phone;
    }
    //Getters e Setters 
     ...
    }

class ContactDAO:

public class ContactDAO {
    private Connection con;


    public Connection getConnection() {
    return con;
    }

    public ContactDAO() {
      ConnectionFactory conF = new ConnectionFactory();
      con=conF.getBDConnection();
    }

   public void addClient(Contact contact) {
    String sql= "INSERT INTO contatos" + "(nome,email,endereco,telefone)" + 
   "values (?,?,?,?)";

    try {
        PreparedStatement stmt= con.prepareStatement(sql);


        stmt.setString(1,contact.getName());
        stmt.setString(2,contact.getEmail());
        stmt.setString(3,contact.getAddress());
        stmt.setInt(4,contact.getTelephoneNumber());    


        stmt.executeUpdate();
        stmt.close();
    } catch (SQLException sqle) {
        sqle.printStackTrace();
    } finally {
        try {
            con.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
  }     
}

ConnectionFactory class:

public class ConnectionFactory {


public Connection getBDConnection() {
    Connection con = null;
    System.out.println("Testing access to BD MySQL\n");

    try {
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root", "");
        System.out.println("Connection successful!!!"); 
    } catch (ClassNotFoundException cnfe){
        cnfe.printStackTrace();
        System.out.println("ClassNotFoundException");
    } catch (SQLException sqle) {
        sqle.printStackTrace();
        System.out.println("SQLException");
    }
    return con;
}

public void closeConnection(Connection con) {
    try {
     con.close();
     System.out.println("\n Connection successfully close!!!");
    }
    catch (SQLException sqle) {
        System.out.println("SQLException");
    }
  }
}
    
asked by anonymous 26.08.2017 / 22:42

1 answer

0

Looking at your code, you can notice an error in the String sql variable, where the declared value for the variable has no spaces between itself, thus removing the SQL reserved words and also the name of the table itself to be consulted. Maybe that's what's causing your problem. Here is the correction proposal:

public void addClient(Contact contact) {
    StringBuilder sql = new StringBuilder();
    sql.append("INSERT INTO contatos ");
    sql.append("(nome,email,endereco,telefone) "); 
    sql.append("values (?,?,?,?)");
    try {
        PreparedStatement stmt= con.prepareStatement(sql.toString());
        ... //Restante de código omitido por não haver alteração
    }
}

The use of StringBuilder is a matter of personal habit for code optimization and performance (even if it is not a big impact in this case, it's already something I brought to my programming).

    
27.08.2017 / 14:34