HTML页面未连接到数据库

时间:2016-11-28 03:53:23

标签: java html mysql database jsp

我要求index.jsp中的用户输入,我将该输入带到另一个search.jsp,我用给定的查询搜索数据库。但是,它没有连接到数据库。有人可以帮助我。

以下是 index.jsp 中的代码,要求输入用户

<form method="searchh" action="search.jsp">
    <table>
        <tr>    
           <td><b class="accent">Enter College Name: </b> &nbsp; &nbsp;</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>&nbsp; &nbsp; <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和catch的catch部分打印出insert失败。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:2)

谢谢。我想到了。它缺少mysql-connecter jar文件。我从项目属性中添加了 - > gt; buildpath - &gt;添加外部jar(到webINF-&gt; lib下的bin文件夹)

链接mysql连接器http://dev.mysql.com/downloads/connector/j/5.0.html