Skip to content

Looking for databases drivers

A simple Java servlet that looks for some well known databases drivers.

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestDB extends HttpServlet {
    public void tryDataBase(String name, String url, PrintWriter out){
        String fail = "fail";
        String sucess = "Ok";
        out.println("Looking for the "+ name +" driver...");
        try {
            Class.forName(url);
            out.println(sucess);
        } catch (ClassNotFoundException ex) {
            out.println(fail);
        }
        out.println("
");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            out.println("Looking for databases");
            out.println("

Looking for databases

"); tryDataBase("MySQL", "com.mysql.jdbc.Driver", out); tryDataBase("Derby", "org.apache.derby.jdbc.EmbeddedDriver", out); tryDataBase("PostgreSQL", "org.postgresql.Driver", out); tryDataBase("Oracle", "oracle. jdbc.driver.OracleDriver", out); tryDataBase("SQLite", "org.sqlite.JDBC", out); out.println(""); } finally { out.close(); } } }

When compiling and running this make sure you put your drivers in your classpath.

A example of output:

example

Published inenglish

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *