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

Asked

Viewed 13,197 times

0

I am 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 looked on the internet how to solve and I can not find solution. Thanks for the help.

I created a Contact Class, a Servlet Createclient, Connectionfactory, Contactdao and addClient.jsp.

I put the code of 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 
     ...
    }

contactdao class:

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");
    }
  }
}
  • have the server log stacktrace? Put it here to help people.

1 answer

0


Looking at your code, you can notice an error right in the variable String sql, where the value declared for the variable has no spaces between itself, thus removing the reserved words from SQL and also the name of the table itself to be consulted. Perhaps this is the major cause of your problem. Follows the proposed correction:

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 for reasons of personal habit for optimization of code and performance (even if in this case it is not a big impact, it is already something that brought to my programming).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.