gpt4 book ai didi

java - 在 jsp 中找不到合适的驱动程序,但不作为 java 应用程序

转载 作者:行者123 更新时间:2023-12-02 11:27:11 25 4
gpt4 key购买 nike

所以我正在启动一个 Web 应用程序,我想使用 JSP 页面(我以前使用过它们)来动态访问数据库并从不同的表中检索数据。

现在我已经有了一个基本的 DatabaseHelper 类、一个仅测试数据库助手连接和一个简单查询的 App 类以及一个 jsp 文件。我面临的问题是,如果从 App.java 类运行,数据库连接工作得很好,但如果我从 JSP 文件运行它,它将抛出一个 SQLException,说它没有找到合适的驱动程序。 (我将在下面留下代码和错误消息)

我尝试了在 StackOverflow 和其他页面中阅读过的不同选项:我将驱动程序放在服务器的类路径中、WEB-INF/lib 中、项目的构建路径中作为外部 JAR(这个适用于之前提到的 Java 应用程序类)...

这是环境:

  • 数据库:Microsoft SQL Server 2017

  • 驱动程序:sqljdbc42.jar

  • 服务器:Glassfish 4.0

  • IDE:Eclipse Oxygen.3

因此,任何有关如何解决此问题并使用 jsp 文件中的数据库帮助程序的提示都将受到热烈欢迎。

谢谢!

错误消息:

[2018-03-28T12:39:48.883+0200] [glassfish 4.0] [SEVERE] [] [pl.mais.db.DBHelper] [tid: _ThreadID=21 _ThreadName=http-listener-1(3)] [timeMillis: 1522233588883] [levelValue: 1000] [[

java.sql.SQLException: No suitable driver found for jdbc:sqlserver://localhost;databaseName=campus_db;user=******;password=*****
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at pl.mais.db.DBHelper.open(DBHelper.java:41)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:58)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:748)
]]

App.java

package pl.mais.general;

import pl.mais.db.DBHelper;

public class App {
public static void main (String[] args) {
DBHelper db = new DBHelper();
db.open();
db.testSelectFaculties();
db.close();
}
}

DBHelper.java

package pl.mais.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author sergi
*
*/
public class DBHelper {
// JDBC driver name and database URL

private final String JDBC_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private final String DB_URL = "jdbc:sqlserver://localhost;databaseName=campus_db;";

// Database credentials
private static final String DB_USER = "*****";
private static final String DB_PASS = "*****";

private Connection conn = null;
private Statement stmt = null;

public DBHelper() {
try {
Class.forName(JDBC_DRIVER);
} catch (Exception e) {
e.printStackTrace();
}
}

public void open() {
try {
String connectionUrl = DB_URL + "user=" + DB_USER + ";password=" + DB_PASS;
//System.out.println("Connecting to database...");
conn = DriverManager.getConnection(connectionUrl);
//System.out.println("Creating statement...");
stmt = conn.createStatement();
} catch (SQLException ex) {
Logger.getLogger(DBHelper.class.getName()).log(Level.SEVERE, null, ex);
}
}

public void close() {
try {
stmt.close();
conn.close();
} catch (SQLException ex) {
Logger.getLogger(DBHelper.class.getName()).log(Level.SEVERE, null, ex);
}
}

public String[] testSelectFaculties() {
try {
// Create and execute an SQL statement that returns some data.
String SQL = "SELECT * FROM faculties";
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(SQL);
ArrayList<String> results = new ArrayList<String>();
// Iterate through the data in the result set and display it.
while (rs.next()) {
results.add(rs.getString(1) + " - " + rs.getString(2));
System.out.println(rs.getString(1) + " - " + rs.getString(2));
}

return (String[])results.toArray();
}

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

}
return null;
}
}

index.jsp

<%@page import="pl.mais.db.DBHelper"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>

<body>
<%
DBHelper db = new DBHelper();
db.open();
String[] faculties = db.testSelectFaculties();
db.close();
for (int i = 0; i < faculties.length; i++) {
%>
<h2>
<%=
faculties[i]
%>
</h2>
<%
}
%>

</body>
</html>

最佳答案

使 JDBC 驱动程序 JAR 文件可访问:

要将 JDBC 驱动程序集成到 GlassFish Server 域中,请将 JAR 文件复制到 domain-dir/lib 目录中,然后重新启动服务器。这使得部署在共享相同配置的服务器上的所有应用程序或模块都可以访问类。

来源:Oracle 文档。

关于java - 在 jsp 中找不到合适的驱动程序,但不作为 java 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49534105/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com