简体   繁体   中英

Cannot get JDBC driver to work in tomcat jsp

I try to make a simple example connection to a mysql database in jsp running on tomcat.

Running the code below I get the response: No suitable driver found for jdbc:mysql://localhost/TestingDB

<HTML>
    <HEAD><TITLE>Testing mysql</TITLE></HEAD>
<BODY>

<%@ page import = "java.sql.*"%>

<%  
        String url = "jdbc:mysql://localhost/TestingDB";

        String user = "mysql";
        String password = "pwd";

    try {
        Connection con = DriverManager.getConnection(url, user, password);
        out.println("Success");
        Statement st = null;
        ResultSet rs = null;
        st = con.createStatement();
        rs = st.executeQuery("SELECT * FROM TestTable");
        while(rs.next()){ 
            out.println(rs.getString("String1"));
        }

    } catch (Exception e) {
        out.println(e.getMessage());
            e.printStackTrace();
        }
%>

</BODY>
</HTML>

I have put the mysql-connector-java-5.1.xx-bin.jar in the CATALINA_HOME/lib directory and when it did not work I tried to put it in the {appname}/WEB-INF/lib directory.

I have restarted tomcat between each try using services tomcat restart .

There should not be any problems accessing the database as it works fine using (pretty much) the same code in a standalone java class.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;

class JDBCTest {

    private static final String url = "jdbc:mysql://localhost/TestingDB?useSSL=false";

    private static final String user = "mysql";     
    private static final String password = "pwd";

    public static void main(String args[]) {
            try {
            Connection con = DriverManager.getConnection(url, user, password);
            System.out.println("Success");
            Statement st = null;
            ResultSet rs = null;
            st = con.createStatement();
            rs = st.executeQuery("SELECT * FROM TestTable");
            while(rs.next()){ 
                  System.out.println(rs.getString("String1"));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

I'm really running out of ideas. All help is welcome!

I have put the mysql-connector-java-5.1.xx-bin.jar in the CATALINA_HOME/lib directory

That should work, but you would have to restart Tomcat before testing it.

and when it did not work I tried to put it in the {appname}/WEB-INF/ directory.

Wrong. Should be the {appname}/WEB-INF/lib directory.

Howver you should be defining this database as a Resource in your META-INF/context.xml and getting it from Tomcat via a JNDI lookup on the java:comp/env/jdbc namespace. What you're doing is poor practice.

You forgot to put the port in the URL access.

Here is a simple example for a connection URL:

jdbc:mysql://localhost: 3306 /TestingDB;

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