//insertForm.jsp
<body>
<form action="insert.jsp" method="post">
<table border="1">
<th>이름</th>
<th>핸드폰</th>
<th>주소</th>
<tr>
<td align="center" width="100" height="30"><input type="text" name="name"></td>
<td align="center" width="200" height="30"><input type="text" name="phone"></td>
<td align="center" width="230" height="30"><input type="text" name="address"></td>
</tr>
</table>
<br>
<input type="submit" value="입력">
</body>
//inset.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import = "java.sql.DriverManager" %>
<%@ page import = "java.sql.Connection" %>
<%@ page import = "java.sql.PreparedStatement" %>
<%@ page import = "java.sql.ResultSet" %>
<%@ page import = "java.sql.SQLException" %>
<% request.setCharacterEncoding("UTF-8"); %>
<table border="1">
<th>이름</th>
<th>핸드폰</th>
<th>주소</th>
<tr>
<td align="center" width="100" height="30"><%=request.getParameter("name")%></td>
<td align="center" width="200" height="30"><%=request.getParameter("phone")%></td>
<td align="center" width="230" height="30"><%=request.getParameter("address")%></td>
</tr>
</table>
<br>
<%
String name = request.getParameter("name");
String phone = request.getParameter("phone");
String address = request.getParameter("address");
Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
PreparedStatement pstmt = null;
String jdbcUrl = "jdbc:mysql://localhost:3306/test01?useUnicode=true&characterEncoding=utf8";
String username = "root";
String pw = "rootpw";
conn = DriverManager.getConnection(jdbcUrl, username, pw);
String query = "insert into jsptest(name, phone, address) values(?, ?, ?)";
pstmt = conn.prepareStatement(query);
pstmt.setString(1, name);
pstmt.setString(2, phone);
pstmt.setString(3, address);
int result = pstmt.executeUpdate(); %>
<%= result %>건 입력 완료
[mysql]DBtable에 데이트 insert하기.txt
0.00MB
'MySQL' 카테고리의 다른 글
Stringboot 연동하는 법 (0) | 2022.08.30 |
---|---|
[eclipse] ajax사용해서 DB insert하기 (0) | 2022.08.30 |
[eclipse] Jquery ajax 사용해서 JSP로 DB 조회하기 (0) | 2022.08.30 |
[eclipse] Dbtable 만들고 JDBC로 데이터 select하는 JSP 코드 (0) | 2022.08.30 |