从HTML搜索框输入生成SQL查询

时间:2016-03-24 17:02:47

标签: java sql postgresql jsp

当我在HTML搜索框中输入字符串并搜索它时,我试图弄清楚如何生成SQL查询(名称,姓氏)。

到目前为止,我有一个包含SQL查询的java类。我还编写了一个jsp文件。

你知道我怎么能接近这个吗?

JSP

<%@ page import="java.util.*" %>
<%@ page import="patientensuche.PatientenSuche" %> 
<jsp:useBean id="suchen" class="patientensuche.AuslesenBean"
scope="session" />

<jsp:setProperty property="*" name="suchen" />

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="utf-8">
<title>ErgoTrace-W</title>

<link href="css/mini-reset.css" rel="stylesheet" />
<link href="css/screen.css" rel="stylesheet" media="screen" />
<link rel="icon" href="img/favicon.ico" type="image/vnd.microsoft.icon" />
<link href='https://fonts.googleapis.com/css?family=Oxygen:400,700'
rel='stylesheet' type='text/css'>

<meta name="description" content="ErgoTrace-W Webprojekt" />
<meta name="keywords" content="ZHAW, WEB1, ErgoTrace" />
<meta name="robots" content="index, follow" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>


<body>
<header> <nav>
<ol>
    <li class="waehlen"><a href="waehlen.jsp">Patient auswählen</a></li>
    <li class="erfassen"><a href="erfassen.jsp">Patient erfassen</a></li>
    <li class="suchen"><a href="suchen.jsp">Patient suchen</a></li>
    <li class="logout"><a href="index.jsp">Logout</a></li>
    <li class="hilfe"><a href="hilfe.jsp">Hilfe</a></li>
</ol>
</nav> </header>

<main> <section> <article>
<h2>Patient suchen</h2>
<form id="searchbox" action="">
    <input id="search" type="text" placeholder="Bitte Namen eingeben">
    <input id="submit" type="submit" value="Suchen">
</form>

<table border="2">
    <thead>
        <tr>
            <th>Name</th>
            <th>Vorname</th>
            <th>Strasse</th>
            <th>Hausnummer</th>
            <th>Ort</th>
            <th>Postleitzahl</th>
            <th>Krankenkasse</th>
            <th>Versicherungsnummer</th>
        </tr>
    </thead>
    </article>
    </section>
    </main>



    <footer> 
    <%@include file="footer.jsp"%>
    </footer>
</body>

</html>

.java文件

package patientensuche;

import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class PatientenSuche {

    public static void DatenbankAbfrage() {

        // Stellt die Verbindung zum Server her
        String host = "jdbc:postgresql://localhost:5432/dbergotrace";
        String uName = "ergotrace";
        String uPass = "ergotrace";
        Connection con = DriverManager.getConnection(host, uName, uPass);

        try {

            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT Name, Vorname FROM Patienten");

            while (rs.next()) {
                String Anrede = rs.getString("Anrede");
                String Vorname = rs.getString("Vorname");
                String Nachname = rs.getString("Nachname");
                String Strasse = rs.getString("Strasse");
                int Hausnummer = rs.getInt("Hausnummer");
                int Plz = rs.getInt("Postleitzahl");
                String Ort = rs.getString("Ort");
                String Krankenkasse = rs.getString("Krankenkasse");
                String Versichertennummer = rs.getString("Versichertennummer");

                System.out.println(Anrede + " " + Vorname + " " + Nachname + " " + Strasse + " " + Hausnummer + " "
                        + Plz + " " + " " + Ort + " " + Krankenkasse + " " + Versichertennummer);
            }
        }

        catch (SQLException err) {
            System.out.println(err.getMessage());
        }

    }

}

1 个答案:

答案 0 :(得分:0)

执行此操作的一种方法是创建Servlet。然后,您可以将JSP表单提交到此servlet,您可以在其中收集用户输入(request.getParameter("search"))以构建SQL并返回结果。