기록해! 정리해!

데이터베이스와 JSP 연동 본문

JAVA/JSP

데이터베이스와 JSP 연동

zsuling 2022. 8. 4. 17:32

1. 이클립스의 웹프로젝트에 드라이버 복사 .

 

2. JAVA 의 lib에 붙여넣기

3. 톰캣에 복사

 

 


1. 생성

2. 오토셋에 있는 데이터베이스 이름으로 사용

database와 url 이름 같게.

 

3. 저장하기

 

 

º 쿼리문 작성하기

--테이블생성

Create table member(
 id varchar(50) not null primary key,
 passwd varchar(16) not null,
 name varchar(10) not null,
 reg_date datetime not null
);


Create table test(
 num_id int not null primary key auto_increment,
 title varchar(50) not null,
 content text not null
);

--인서트하기

insert into member(id, passwd, name, reg_date)
values('kk0063@naver.com','1234','라즈베리',now());

insert into member(id, passwd, name, reg_date)
values('sky@naver.com','1212','블랙베리',now());

insert into member(id, passwd, name, reg_date)
values('ppk@naver.com','3333','김베리',now());

select * from member;

º DBConnection.jsp 데이터베이스 연동하기

- select

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<%@ page import="java.sql.*" %>  <!-- (0) 패키지 , import  -->

<%

   String url = "jdbc:mysql://localhost:3306/ppk400";
   String id ="root";
   String pass ="autoset"; 
   
   Connection conn =null;
   PreparedStatement pstmt =null;
   ResultSet rs =null;
   
   
   try{
       Class.forName("com.mysql.jdbc.Driver"); // (1)
       conn = DriverManager.getConnection(url, id, pass); // (2)
       
       String sql ="select  *  from member";
       
       pstmt=conn.prepareStatement(sql);  // (3)
       rs = pstmt.executeQuery(); // (4) 
       
       while(rs.next()){   // (5) 
       %>	
    	 <%=rs.getString("id") %>
    	 <%=rs.getString("passwd") %>
    	 <%=rs.getString("name") %>
    	 <%=rs.getString("reg_date") %><br>
    	 
       <%	   
       }
   } catch(Exception e){
	   e.printStackTrace();
   } finally {     // (6) 객체 닫기 
	   rs.close();
	   pstmt.close();
	   conn.close();
   }

%>
    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

</body>
</html>

-insert

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<%@ page import="java.sql.*" %>  <!-- (0) 패키지 , import  -->

<%

   String url = "jdbc:mysql://localhost:3306/ppk400";
   String id ="root";
   String pass ="autoset"; 
   
   Connection conn =null;
   PreparedStatement pstmt =null;
   ResultSet rs =null;
   
   
   try{
       Class.forName("com.mysql.jdbc.Driver"); // (1)
       conn = DriverManager.getConnection(url, id, pass); // (2)
       
       String sql ="insert  into member(id, passwd, name, reg_date) values(?,?,?,now())";
       
       pstmt=conn.prepareStatement(sql);  // (3)
       pstmt.setString(1,"ppk200");
       pstmt.setString(2,"5678");
       pstmt.setString(3,"너구리2");
       int k=pstmt.executeUpdate(); // (4) 
       if (k==1){
    	   out.println("저장성공");
       }else{
    	   out.println("저장실패!!");
       }       
       
   } catch(Exception e){
	   out.println("예외발생");
	   e.printStackTrace();
   } finally {     // (6) 객체 닫기 
	   pstmt.close();
	   conn.close();
   }

%>
    

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

</body>
</html>

 

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

JSP - 답변형 게시판  (0) 2022.08.19
JSP - 자료실  (0) 2022.08.08
JSP 교재(3) - 액션태그 (include / forward ), MVC  (0) 2022.08.04
JSP 교재 (2)  (0) 2022.08.03
JSP 교재  (0) 2022.08.02
Comments