0

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

Asif Mushtaq
  • 3,658
  • 4
  • 44
  • 80
  • Why `DBConection` extends `HTTPServlet`? You get any exceptions? – Jens Oct 18 '15 at 07:48
  • @Jens Sorry, I was trying different way, and forget to remove that. Where I can see exception? there is nothing during compile time. – Asif Mushtaq Oct 18 '15 at 07:52
  • 1
    You can see it in the logfiles of your server – Jens Oct 18 '15 at 07:54
  • @Jens Sorry I'm new to Servlet, I'm using netbeans and GlassFish, where I can see? In which folder? – Asif Mushtaq Oct 18 '15 at 07:55
  • For debugging only, you might want to consider replacing `se.printStackTrace()` with `throw new RuntimeException(se)` so the exception is displayed in the browser. Most likely reason btw is the fact your password is empty. – Mark Rotteveel Oct 18 '15 at 08:01
  • @MarkRotteveel there is no password, I'm using default password. – Asif Mushtaq Oct 18 '15 at 08:03
  • i am not working with netbeans and glassfish so i can not say where the logs are. Maybe it is in the console or you can ask google. – Jens Oct 18 '15 at 08:06
  • @Jens by using Mark method, I got this Exception. `java.lang.RuntimeException: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver` now what to do? – Asif Mushtaq Oct 18 '15 at 08:07
  • You should also check if mysql is running and the database must exists – Jens Oct 18 '15 at 08:07
  • For logs (you will need them): http://stackoverflow.com/questions/13835913/location-of-glassfish-server-logs – PeterMmm Oct 18 '15 at 08:07
  • You may set `Connection.setAutocommit(true)` to get out of doubts. – PeterMmm Oct 18 '15 at 08:10
  • @PeterMmm where to add? I'm getting this Exception `java.lang.RuntimeException: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver` – Asif Mushtaq Oct 18 '15 at 08:11

1 Answers1

-1

You nedd to register your driver with DriverManager like this:

   Class c = Class.forName("com.mysql.jdbc.Driver");
   DriverManager.registerDriver( c.newInstance() );

Please remember to close your connection in finally block.

k0staa
  • 319
  • 2
  • 5