#java #database #jsp
Вопрос:
Это вопрос о связывании БД JSP. Если вы проверяете ввод в jsp, база данных не обновляется.
Я подтвердил, что он работает, когда я ВСТАВЛЯЮ его непосредственно из недорогой базы данных. Независимо от того, чему я следую, я найду, что это неправильно в моих способностях. Пожалуйста, помогите, это java-код
package db;
class BoUser {
private String u_date;
private String u_room;
private String u_adu<
private String u_kid;
public String getU_date() {
return u_date;
}
public void setU_date(String u_date) {
this.u_date = u_date;
}
public String getU_room() {
return u_room;
}
public void setU_room(String u_room) {
this.u_room = u_room;
}
public String getU_adult() {
return u_adu<
}
public void setU_adult(String u_adult) {
this.u_adult = u_adu<
}
public String getU_kid() {
return u_kid;
}
public void setU_kid(String u_kid) {
this.u_kid = u_kid;
}
}
Это код подключения
package db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class BODAO {
private Connection conn; //자바와 데이터베이스를 연결
private PreparedStatement pstmt; //쿼리문 대기 및 설정
//기본 생성자
//UserDAO가 실행되면 자동으로 생성되는 부분
//메소드마다 반복되는 코드를 이곳에 넣으면 코드가 간소화된다
public BODAO() {
try {
String dbURL = "jdbc:mariadb://localhost:3306/bbs";
String dbID = "root";
String dbPassword = "1234";
Class.forName("org.mariadb.jdbc.Driver");
conn = DriverManager.getConnection(dbURL, dbID, dbPassword);
}catch (Exception e) {
e.printStackTrace();
}
}
public int book(BoUser bouser) {
String sql = "insert into bouser values(?, ?, ?, ?)";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, bouser.getU_date());
pstmt.setString(2, bouser.getU_room());
pstmt.setString(3, bouser.getU_adult());
pstmt.setString(4, bouser.getU_kid());
return pstmt.executeUpdate();
}catch (Exception e) {
e.printStackTrace();
}
return -1;
}
}
This is executable code.
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css"
integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB"
crossorigin="anonymous">
<head>
<meta charset="utf-8">
<% request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");%>
</head>
<body>
<jsp:include page="hotelmenu.jsp"/>
<div class="container"> <!-- 하나의 영역 생성 -->
<div class="col-lg-4"> <!-- 영역 크기 -->
<!-- 점보트론은 특정 컨텐츠, 정보를 두드러지게 하기 위한 큰 박스 -->
<div class="jumbotron" style="padding-top: 20px;">
<form class="form-horizontal" role="form" method="post" action="bookingaction.jsp">
<h3 style="text-align: center;">회원가입 화면</h3>
<div class="form-group">
<input type="text" class="form-control" placeholder="날짜" name="u_date" maxlength="20">
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="방" name="u_room" maxlength="20">
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="어른" name="u_adult" maxlength="20">
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="어린이" name="u_kid" maxlength="20">
</div>
<input type="submit" class="btn btn-primary form-control" value="예약">
</form>
</div>
</div>
</div>
Below is the action code.
<%@page import="java.io.PrintWriter"%>
<%@page import="db.BODAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<% request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");%>
<jsp:useBean id="bouser" class="db.BoUser" scope="page" />
<jsp:setProperty name="bouser" property="u_date" />
<jsp:setProperty name="bouser" property="u_room" />
<jsp:setProperty name="bouser" property="u_adult" />
<jsp:setProperty name="bouser" property="u_kid" />
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<%
if(bouser.getU_date() == null || bouser.getU_room() == null ||
bouser.getU_adult() ==null){
PrintWriter script = response.getWriter();
script.println("<script>");
script.println("alert('입력이 안 된 사항이 있습니다')");
script.println("history.back()");
script.println("</script>");
}else {
PrintWriter script = response.getWriter();
script.println("<script>");
script.println("alert('예약성공')");
script.println("location.href='home.jsp'");
script.println("</script>");
}
%>
</body>
</html>