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