Tomcat connection to database

I am trying to write a web application using Tomcat on Netbeans. I have a problem when I try to connect to sql database: java.sql.SQLException: No suitable driver found for jdbc:derby://localhost:1527/mydbname

I've already included derby.jar and derbyclient.jar in the classpath and in the WEB-INF / lib folder. I also created a separate java files file through which I can access my database: I don't get any errors , everything is fine, but when I try to connect via Tomcat I get the driver error mentioned above!

Here is my Java servlet file:

public class Servlet extends HttpServlet {

Connection con = null;
String url = "jdbc:derby://localhost:1527/Onlineshop";   

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    try {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        con = DriverManager.getConnection(url);
        System.err.println("connection established");
    } catch (Exception ex) {
        Logger.getLogger(Servlet.class.getName()).log(Level.SEVERE, null,ex);
    }
    response.setContentType("text/plain");
    response.getWriter().println("connected to database");
    request.getRequestDispatcher("/ServletHTML").forward(request, response);
    }
}

      

Indeed, thanks for the help!

+3


source to share


1 answer


Obviously you've confused MySql with Derby config.

In quick steps to successfully connect to your Derby db, you need to change:

String url="jdbc:derby://localhost:1527/Onlineshop;create=true;user=me;password=mine";

      

Then download the driver and connect as follows:



Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
//Get a connection
conn = DriverManager.getConnection(url); 

      

For a complete example see this

Hope it helps

+1


source







All Articles