我在 index.jsp 中请求用户输入,然后将该输入带到另一个 search.jsp,我在其中使用给定查询搜索数据库。但是,它没有连接到数据库。有人可以帮帮我吗。
这是我在 index.jsp 中要求用户输入的代码
<form method="searchh" action="search.jsp">
<table>
<tr>
<td><b class="accent">Enter College Name: </b> </td>
<td><input type="text" name="college" STYLE="color: #f4d442; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #72A4D2;" size="10" maxlength="30"></td>
<td> <input type="submit" value="Search Your College" <a href="#submit" class="btn btn-default btn-lg btn-satact" role="button"></a>></td>
</tr>
</table>
</form>
这是我在 search.jsp 中的代码。数据库名称/url/id/密码都输入正确。我对不同的搜索文件使用了相同的代码。它在该文件中完美运行。但是,它不会连接到此 .jsp 文件中的数据库。这是我的 search.jsp 代码
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!--Import some libraries that have classes that we need -->
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*"%>
<!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>
<%
try {
//Create a connection string
String url = "my_data_base_link";
//Load JDBC driver - the interface standardizing the connection procedure. Look at WEB-INF\lib for a mysql connector jar file, otherwise it fails.
Class.forName("com.mysql.jdbc.Driver");
//Create a connection to your DB
Connection con = DriverManager.getConnection(url, "user", "pass");
//Create a SQL statement
Statement stmt = con.createStatement();
String college = request.getParameter("college");
String str = "SELECT College, TUITIONFEE_IN, State FROM my_project1.all WHERE College LIKE " + "'" + college + "%'";
ResultSet result = stmt.executeQuery(str);
con.close();
} catch (Exception ex) {
out.print("insert failed");
}
在try and catch的catch部分打印insert fail。感谢您的帮助。
最佳答案
谢谢。我想到了。它缺少 mysql-connecter jar 文件。我从项目属性->构建路径-> 添加外部 jar(到 webINF->lib 下的 bin 文件夹)
mysql 连接器链接 http://dev.mysql.com/downloads/connector/j/5.0.html
关于java - HTML 页面未连接到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40836926/