2016-11-28 110 views
4

我在index.jsp中詢問用戶輸入,並將該輸入傳送到不同的search.jsp,其中我使用給定查詢搜索數據庫。但是,它沒有連接到數據庫。有人可以幫幫我嗎。HTML頁面未連接到數據庫

這是我在的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的代碼代碼。數據庫名稱/網址/ 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

嘗試打印實際的異常,看看那裏發生的錯誤。 – Aleksandar

+0

@Aleksandar謝謝你這是一個沒有找到錯誤的類。任何想法,我該如何解決這個問題? – AP6

+0

好吧,您沒有提到ClassNotFound異常的類名,但如果是關於驅動程序,請嘗試使用此問題http://stackoverflow.com/questions/1585811/classnotfoundexception-com-mysql-jdbc-driver – Aleksandar

回答