I'm trying to connect to a database in a Java Servlet, but I am unable to connect. I searched on google and each website is showing different way I don't know why.
Here is the code that I tried to connect, but nothing(data) is going to MySQL database rows.
public class DBConnection{
private static final long serialVersionUID = 1L;
private final Connection connection;
private final String dbURL = "jdbc:mysql://localhost:3306/Servlet";
private final String user = "root";
private final String pwd = "";
public DBConnection() throws ClassNotFoundException, SQLException{
Class.forName("com.mysql.jdbc.Driver");
this.connection = DriverManager.getConnection(dbURL, user, pwd);
}
public Connection gC(){
return this.connection;
}
}
Here is I'm trying to insert data in database (but data is not adding to DB).
public class ReServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
try{
String uName=req.getParameter("uName");
String uEmail=req.getParameter("uEmail");
String uPass=req.getParameter("uPass");
DBConnection conn=new DBConnection();
PreparedStatement ps;
ps = conn.gC().prepareStatement("insert into users(name,email,password) values (?,?,?)");
ps.setString(1, uName);
ps.setString(2, uEmail);
ps.setString(3, uPass);
ps.execute();
}catch(SQLException | ClassNotFoundException se){
se.printStackTrace();
}finally{
}
}
}
I also request to all, please give me simple and easy database connection example, about each website is showing with full registration or any other system. But I just want simple db connection example as I am trying in my code.
Edited
By replacing se.printStackTrace();
to this throw new RuntimeException(se);
I'm getting this Exception java.lang.RuntimeException: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver