gpt4 book ai didi

java - 我无法从 jsp 连接到 mysql,这是怎么回事?

转载 作者:行者123 更新时间:2023-12-01 14:58:22 24 4
gpt4 key购买 nike

我无法从我的 jsp 连接到数据库。代码如下

  <% 
try {
/* Create string of connection url within specified format with machine name,
port number and database name. Here machine name id localhost and
database name is usermaster. */
String connectionURL = "jdbc:mysql://localhost:3306/test";

// declare a connection by using Connection interface
Connection connection = null;

// Load JBBC driver "com.mysql.jdbc.Driver"
Class.forName("com.mysql.jdbc.Driver").newInstance();

/* Create a connection by using getConnection() method that takes parameters of
string type connection url, user name and password to connect to database. */
connection = DriverManager.getConnection(connectionURL, "root", "pass");

// check weather connection is established or not by isClosed() method
if(!connection.isClosed())
%>
<font size="+3" color="green"></b>
<%
out.println("Successfully connected to " + "MySQL server using TCP/IP...");
connection.close();
}
catch(Exception ex){
%>
</font>
<font size="+3" color="red"></b>
<%
out.println("Unable to connect to database.");
}
%>

我连接到数据库并进行事件搜索查询可能出现什么问题?我只是从源代码中进行 c/p 并将我的 jsp.js 文件放入其中。密码和用户名均正确。

最佳答案

MySQL 驱动程序 jar 文件必须放置在已部署的 Web 应用程序的 WEB-INF/lib 目录中。

另请注意:

  • JSP 不应访问数据库。这应该在 servlet 调用的服务中完成。
  • 连接应始终在finally block 中关闭。如果出现任何异常,不这样做将使连接保持打开状态,并很快使系统崩溃
  • 网络应用程序应该使用连接池。请阅读您的应用服务器或 Web 容器文档,了解如何设置连接池。

关于java - 我无法从 jsp 连接到 mysql,这是怎么回事?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14044596/

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