Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

cari kode di servlet ke mysql?

Saya membuat proyek demo sederhana yang Anda sebutkan, saya menggunakan jstl:core perpustakaan yang dapat diunduh dari di sini

web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
        id="WebApp_ID"
        version="3.1">
        <display-name>TestServlet</display-name>
        <welcome-file-list>
            <welcome-file>index.html</welcome-file>
            <welcome-file>index.htm</welcome-file>
            <welcome-file>index.jsp</welcome-file>
            <welcome-file>default.html</welcome-file>
            <welcome-file>default.htm</welcome-file>
            <welcome-file>default.jsp</welcome-file>
        </welcome-file-list>
        <display-name>Archetype Created Web Application</display-name>
        <servlet>
            <servlet-name>MyController</servlet-name>
            <servlet-class>controller.MyController</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>MyController</servlet-name>
            <url-pattern>/MyController</url-pattern>
        </servlet-mapping>

</web-app>

index.jsp

 <%@ 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">
    <%@ taglib
        uri="http://java.sun.com/jsp/jstl/core"
        prefix="c"%>
    <html>
    <head>
    <meta
        http-equiv="Content-Type"
        content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
<form
        action="MyController"
        method="post">
        <input
            type="text"
            name="name"
            value="yourUserName"> <input
            type="submit"
            value="Search">
    </form>
        <table title="Users List">
            <c:forEach
                items="${users}"
                var="user">
                <tr>
                    <td>${user.id}</td>
                    <td>${user.name}</td>
                </tr>
            </c:forEach>
        </table>
    </body>
    </html>

MyController.java

package controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import domain.User;

public class MyController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public MyController() {
        super();
    }

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        RequestDispatcher rd = null;
        rd = request.getRequestDispatcher("index.jsp");
        rd.forward(request, response);
    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // Get name request parameter
        String name = request.getParameter("name");

        // User user1 = new User();
        // User user2 = new User();
        // user1.setId(0);
        // user1.setName("test1");
        // user2.setId(0);
        // user2.setName("test2");

        List<User> users = new ArrayList<User>();

        // users.add(user1);
        // users.add(user2);



         //DB Operation query your users
         users=query(name);

        request.setAttribute("users", users);

        RequestDispatcher rd = null;
        rd = request.getRequestDispatcher("index.jsp");
        rd.forward(request, response);
    }
}

jadi MyController#query metode harus dipanggil dari lapisan layanan Anda, dan kueri sesuai dengan kriteria Anda (dalam nama demo atau bisa apa saja)

EDIT: Pastikan user telah mendapatkan metode properti, seperti getId dan getName dalam contoh ini.




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Terowongan SSH untuk koneksi Python MySQLdb

  2. Java/Mysql..SQLException:Sebuah tabel harus memiliki setidaknya 1 kolom SQLState:42000 VendorError:1113

  3. Jalankan SQL mentah dalam migrasi

  4. LOAD DATA INFILE pada mesin jarak jauh

  5. MySQL:Bagaimana cara saya bergabung dengan tabel yang sama beberapa kali?