问题
I have create one maven project.
I am try to connect MSSQL (Microsoft SQL Server 2014) with my J2EE application. But it throws following exception
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1452)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
at com.jagir.ecommerce.database.DatabaseConnection.testConnection(DatabaseConnection.java:27)
at com.jagir.ecommerce.user.servlet.Registration.doGet(Registration.java:34)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(DriverManager.java:315)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(B
asicDataSource.java:1437)
... 18 more
My code :
web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>ecommerce</display-name>
<resource-ref>
<description>Database connection</description>
<res-ref-name>jdbc/testdb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<servlet>
<servlet-name>Registration</servlet-name>
<servlet-class>com.test.user.servlet.Registration</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Registration</servlet-name>
<url-pattern>/registration</url-pattern>
</servlet-mapping>
</web-app>
Registration
public class Registration extends HttpServlet
{
private static final long serialVersionUID = 1L;
public Registration()
{
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.getWriter().append("Served at: ").append(request.getContextPath());
request.setAttribute("title", "User Registration");
new DatabaseConnection().testConnection();
request.getRequestDispatcher("pages/user/registration.jsp").forward(request, response);
}
}
DatabaseConnection
public class DatabaseConnection
{
private DataSource dataSource;
private Connection connection;
private Statement statement;
public void testConnection()
{
//get data source
try
{
Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:/comp/env");
dataSource = (DataSource) envContext.lookup("jdbc/testdb");
connection = dataSource.getConnection();
statement = connection.createStatement();
System.out.println("TEST");
ResultSet rs = statement.executeQuery("select * from temp");
System.out.println("Done");
while(rs.next())
{
System.out.println(rs.getString(0));
}
}
catch (NamingException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/test" docBase="test" crossContext="true"
reloadable="true" debug="1">
<!-- Specify a JDBC datasource -->
<Resource name="jdbc/testdb" auth="Container" type="javax.sql.DataSource"
username="sa" password="password" driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver"
url="jdbc:sqlserver://devmangal;databaseName=testV1.0;integratedSecurity=true"
maxActive="10" maxIdle="4" />
</Context>
回答1:
Hi,
it seems, that you use the wrong driver version for JVM. If you use a 32-Bit-JVM (Java Virtual Machine) , please use file „sqljdbc_auth.dll“ in folder „x86“, though OS is x64. If you use a x64-JVM with a x64-Processor, please use the file „sqljdbc_auth.dll“ in folder „x64“.
... Cannot create JDBC driver of class '' for connect URL 'null' ...
Best regards Axel Arnold Bangert - Herzogenrath 2016
回答2:
Try this one,
Manually try to place your context.xml file under
${CATALINA_HOME}/conf/Catalina/localhost/context.xml
Rename this context.xml to {app-name}.xml
来源:https://stackoverflow.com/questions/37258897/java-sql-sqlexception-no-suitable-driver