简体   繁体   中英

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM