자바 웹 은 간단 한 조회 상품 기능 을 실현 합 니 다.
CustomerServlet.java
package com.subing.web;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/CustomerServlet")
public class CustomerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private SqlDemo sql = null;
private final static String s1 = "<h1> </h1><form action='CustomerServlet' method='post'> "
+ " :<input type='text' name='jqmess'/><br>"
+ " :<input type='text' name='mhmess'/><br>"
+ "<input type='submit' value=' ' name='sub'/>"
+ "</form>";
//
private boolean isLoginProv(String userinfo, String password) {
if (userinfo != null && userinfo.length() > 0 && password != null
&& password.length() > 0) {
return true;
}
return false;
}
public CustomerServlet() throws Exception {
super();
sql = new SqlDemo(); //
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=gb2312");
request.setCharacterEncoding("gb2312");
PrintWriter pw = response.getWriter();
String sub = request.getParameter("sub");
String login = request.getParameter("login");
if (login != null && login.length() > 0) {
String admin_id = request.getParameter("admin_id");
String password = request.getParameter("password");
if (isLoginProv(admin_id, password)) {
try {
if (sql.loginVerify(admin_id, password)) {
pw.println(s1);
} else {
pw.println("<h1> !</h2>2 !");
response.setHeader("refresh", "2;url=login.html");
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
pw.println("<h1> !</h2>5 !");
response.setHeader("refresh", "5;url=login.html");
}
} else if (sub != null && sub.length() > 0) {
pw.println(s1);
String jqmess = request.getParameter("jqmess");
String mhmess = request.getParameter("mhmess");
if (jqmess != null && jqmess.length() > 0) {
try {
String s = sql.getJqMess(jqmess);
String mess[] = s.split(",");
String html = "<table border='5'>" + "<tr>" + "<th>Id </th>"
+ "<th> </th>" + "<th> </th>"
+ "<th> </th>" + "<th> </th>";
String main = "<tr>" + "<td>" + mess[0] + "</td>" + "<td>"
+ mess[1] + "</td>" + "<td>" + mess[2] + "</td>"
+ "<td>" + mess[3] + "</td>" + "<td>" + mess[4]
+ "</td></tr></table>";
String head = html + main;
pw.println(head);
} catch (Exception e) {
e.printStackTrace();
}
} else if (mhmess != null && mhmess.length() > 0) {
try {
String head = "";
String html = "<table border='5'>" + "<tr>" + "<th>Id </th>"
+ "<th> </th>" + "<th> </th>"
+ "<th> </th>" + "<th> </th>";
head += html;
String s = sql.getMhMess(mhmess);
String m[] = s.split(",,");
for (int i = 0; i < m.length; i++) {
String mess[] = m[i].split(",");
String main = "<tr>" + "<td>" + mess[0] + "</td>"
+ "<td>" + mess[1] + "</td>" + "<td>" + mess[2]
+ "</td>" + "<td>" + mess[3] + "</td>" + "<td>"
+ mess[4] + "</td></tr>";
head += main;
}
head += "</table>";
pw.println(head);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
데이터베이스 접근 클래스:SqlDemo.java
package com.subing.web;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class SqlDemo {
private Connection conn = null;
private PreparedStatement preparedStatement = null;
public SqlDemo() throws Exception {
conn = getConnection();
}
private Connection getConnection() throws Exception {
String driverClass = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql:///shop?useUnicode=true&characterEncoding=gb2312";
String user = "root";
String password = "12345";
//
Class.forName(driverClass);
//
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println(conn);
return conn;
}
//
public boolean loginVerify(String userinfo, String password)
throws Exception {
String sql = "select * from admin where admin_id = ?";
preparedStatement = conn.prepareStatement(sql);
preparedStatement.setString(1, userinfo);
ResultSet rs = preparedStatement.executeQuery();
if (rs.next()) {
if (rs.getString("password").equals(password)) {
System.out.println(" !");
return true;
}
}
System.out.println(" !");
return false;
}
public String getJqMess(String admin_id) throws Exception {
String s = "";
String sql = "select * from product1 where product_id = ? OR product_name LIKE ?"
+ "OR product_price LIKE ?"
+ "OR product_num LIKE ?"
+ "OR product_describe LIKE ?";
preparedStatement = conn.prepareStatement(sql);
preparedStatement.setString(1, admin_id);
preparedStatement.setString(2, admin_id);
preparedStatement.setString(3, admin_id);
preparedStatement.setString(4, admin_id);
preparedStatement.setString(5, admin_id);
// , resultSet, null
ResultSet rs = preparedStatement.executeQuery();
while (rs.next()) {
s = rs.getInt(1) + "," + rs.getString(2) + "," + rs.getInt(3) + ","
+ rs.getInt(4) + "," + rs.getString(5);
}
return s;
}
public String getMhMess(String admin_id) throws Exception {
String mess = "";
String sql = "select * from product1 where product_id like ? OR product_name LIKE ? OR product_price LIKE ? OR product_num LIKE ?"
+ "OR product_describe LIKE ?";
preparedStatement = conn.prepareStatement(sql);
preparedStatement.setString(1, "%" + admin_id + "%");
preparedStatement.setString(2, "%" + admin_id + "%");
preparedStatement.setString(3, "%" + admin_id + "%");
preparedStatement.setString(4, "%" + admin_id + "%");
preparedStatement.setString(5, "%" + admin_id + "%");
ResultSet rs = preparedStatement.executeQuery();
while (rs.next()) {
String s = rs.getInt(1) + "," + rs.getString(2) + ","
+ rs.getInt(3) + "," + rs.getInt(4) + "," + rs.getString(5);
mess += s + ",,";
}
return mess;
}
public static void main(String[] args) throws Exception {
SqlDemo sqlDemo = new SqlDemo();
String s = sqlDemo.getMhMess("xi");
String m[] = s.split(",,");
for (int i = 0; i < m.length; i++) {
System.out.println(m[i]);
}
}
}
html 파일:login.html
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1> </h1>
<form action="CustomerServlet" method="post">
:<input type="text" name="admin_id"/>
:<input type="password" name="password"/>
<input type="submit" value=" " name="login"/>
</form>
</body>
</html>
데이터베이스 안의 표 데이터실행 효과
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Javaweb에서 양식 데이터를 가져오는 다양한 방법Javaweb에서 양식 데이터를 가져오는 몇 가지 방법 1. 키 값이 맞는 형식으로 폼 데이터를 얻는다 getParameter(String name): 키를 통해 value를 반환합니다. getParameterVal...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.