简体   繁体   中英

"no suitable driver found for jdbc" when using DataSource (Java Eclipse)

I've reviewed a number of other posts on Stack Overflow on "no suitable driver found for jdbc". However, the solutions in the other posts seem to be around using "DriverManager", placing the database connector JAR in "Java Build Path" in Eclipse, like this:

Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://example.com:3306/foo","john","secret");

I'm taking a different approach. I've IBM Liberty application server runtime in Eclipse. Per IBMs documentation, I've placed the JAR on Liberty and I've configured liberty with a JNDI of "jdbc/mySQL".

server.xml

<dataSource id="mySQL" jndiName="jdbc/mySQL">
  <jdbcDriver libraryRef="mySQLlibrary"/>
  <properties serverName="example.com" portNumber="3306" databaseName="foo" user="john" password="secret"/>
</dataSource>

<library id="mySQLlibrary">
  <fileset name="/opt/IBM/Liberty/usr/shared/resources/mysql/mysql-connector-java-8.0.11.jar"/>
</library>

THE GOOD

I am able to make a connection to mySQL when I create a servlet in Eclipse with the following markup. In this example, the servlet is "foo.java". When running this servlet on Liberty in Eclipse, database connection successful is displayed.

package com.main;

import java.io.*;
import java.sql.*;
import javax.annotation.Resource;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
import javax.sql.DataSource;

@WebServlet("/foo")
public class foo extends HttpServlet {

  @Resource(name = "jdbc/mySQL")
  private DataSource ds1;
  private Connection conn = null;
  private static final long serialVersionUID = 1L;

  public foo() {
    super();
  }
  public void doGet(HttpServletRequest request, HttpServletResponse response) 
                                   throws ServletException, 
IOException {
    PrintWriter out = response.getWriter();
    try {
        conn = ds1.getConnection();
        } 
     catch (SQLException e) {
        e.printStackTrace();
        } 
         finally {
            if (conn != null){
                out.println("Database connection successful");
            }
          }
    }
}

THE BAD

In Eclipse, I've created foo.jsp with the following markup. Notice I'm using JNDI "jdbc/mySQL" here as well, just like I used in the foo.java servlet.

<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<sql:query var="foo" dataSource="jdbc/mySQL">
    select * from foo;
</sql:query>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
</head>
<body>
</body>
</html>

THE UGLY

When I run the project in Eclipse and navigate to the foo.jsp page, No suitable driver found for jdbc/mySQL is returned.

Exception thrown by application class 'org.apache.taglibs.standard.tag.common.sql.QueryTagSupport.getConnection:284'
javax.servlet.jsp.JspException: Unable to get connection, DataSource invalid: "java.sql.SQLException: No suitable driver found for jdbc/mySQL"
at org.apache.taglibs.standard.tag.common.sql.QueryTagSupport.getConnection(QueryTagSupport.java:284)
at [internal classes]
at com.ibm._jsp._test._jspx_meth_sql_query_0(_test.java:137)
at com.ibm._jsp._test._jspService(_test.java:102)
at com.ibm.ws.jsp.runtime.HttpJspBase.service(HttpJspBase.java:100)
at [internal classes]

I'm not sure what needs to be changed to make a successful connection to the database in foo.jsp. I should mention that using JSTL is not a requirement here. Some other approach is totally fine as long as the JSP page can ultimately make the connection to the database via the DataSource on the Liberty application server.

You are missing imports. try importing them.

<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>

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