【发布时间】:2017-04-11 17:45:30
【问题描述】:
我在 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部分打印出插入失败。任何帮助表示赞赏。
【问题讨论】:
-
尝试打印实际异常以查看发生错误的位置。
-
@Aleksandar 谢谢你这是一个找不到类的错误。知道我应该如何解决这个问题吗?
-
好吧,你没有提到 ClassNotFound 异常的类名,但如果是关于驱动程序,试试这个问题stackoverflow.com/questions/1585811/…
标签: java html mysql database jsp