본문 바로가기
Language/JSP

데이터베이스 연동

by 태윤2 2020. 6. 7.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.PreparedStatement"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
//한글값 받을수 있게 셋팅
request.setCharacterEnconding("utf-8");
 
//form에서 값 리퀘스트하기
//Integer.parseInt(); 받은값 정수값으로 변경
int num = Interger.parseInt(request.getParameter("num"));
String name =request.getParameter("name");
//1단계 드라이버 불러오기
Class.forName("com.mysql.jdbc.Driver");
 
//2단계 디비열결 => 연결정보 저장
String dbUrl = "jdbc:mysql://localhost:3306/jspdb5";
String dbUser = "root";
String dbPass = "1234";
//Connection 패키지 import하기
//DriverManager 패키지 import 하기
Connection con = DriverManager.getConnection(dbUrl, dbUser, dbPass);
//3단계 연결정보를 이용해서 sql 구문 만들고 실행할 객체 생성
String sql = "insert into student values("+ num +",'"+ name + " ')";
//PreparedStatement 패키지 import하기
PreparedStatement pstmt = con.prepareStatement(sql);
 
4단계 객체(sql 구문) 실행
pstmt.executeUpdate();
%>
 
</body>
</html>
cs

 

'Language > JSP' 카테고리의 다른 글

MVC  (1) 2020.08.04
Database  (0) 2020.06.07
session  (0) 2020.06.02
response 내장객체  (0) 2020.06.02
application 내장객체  (0) 2020.06.02