Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tomcat Database connection derby

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

I have already included the derby.jar and derbyclient.jar in the classpath and in the WEB-INF/lib folder. I have also created a seperate java file wherer I can access my database: i get no errors whatsoever, everything is fine, but when i try to connect through Tomcat i get the driver error mentioned above!

Here is my servlet java 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);
    }
}

Really appreciate help!

like image 391
Dimitri Tyan Avatar asked Aug 04 '26 05:08

Dimitri Tyan


1 Answers

Clearly you have messed up MySql with the Derby configuration.

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

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

And then load the driver and make a connection like this:

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

For a complete example see this

Hope this helps

like image 69
MaVRoSCy Avatar answered Aug 06 '26 17:08

MaVRoSCy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!