package connection;
import java.sql.*;
public class ConnectionProvider {
static Connection conn;
public static Connection getConnection(){
//2. 연결
try{
// test는 database 이름 입니다
// mysql안에 database 만드는 명령 :
// c:/> mysqladmin -u root create test -p
String url="jdbc:mysql://localhost:3306/test";
String user="root"; // 계정 이름
String password="1234"; //계정 비밀번호
conn=
DriverManager.getConnection(url, user, password);
System.out.print("연결성공 :"+ conn.toString());
}catch(Exception e){
System.out.print("연결 실패-"+e.getMessage());
}
return conn;
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="bean.Item" %>
<%@ page import="java.util.ArrayList" %>
<!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>db에서 jsp2테이블 값을 다 가져오기</title>
<link href="myStyle.css"
rel="stylesheet" type="text/css" />
</head>
<body>
<%
//1 . 드라이버 로딩
Class.forName("com.mysql.jdbc.Driver");
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
//2. 연결
try{
// test는 database 이름 입니다
// mysql안에 database 만드는 명령 :
// c:/> mysqladmin -u root create test -p
String url="jdbc:mysql://localhost:3306/test";
String user="root"; // 계정 이름
String password="1234"; //계정 비밀번호
conn=
DriverManager.getConnection(url, user, password);
out.print("연결성공 :"+ conn.toString());
}catch(Exception e){
out.print("연결 실패-"+e.getMessage());
}
String query=
"select * from jsp2;";
stmt=conn.createStatement();
//db에 명령을 날리면 결과가 옵니다
rs=stmt.executeQuery(query);
/* out.print("<table>");
while( rs.next()){ //한칸 아래로 이동
out.print("<tr><td> no </td><td>");
out.print(rs.getString("no")+"</td></tr>");
out.print("<tr><td id='title'> title </td><td class='title'>");
out.print(rs.getString("title")+"</td></tr>");
out.print("<tr><td> context </td><td>");
out.print(rs.getString("context")+"</td></tr>");
}
out.print("</table>"); */
ArrayList<Item> arrayList=new ArrayList<Item>();
while(rs.next()){
// String -> int 로 cast
int no=Integer.parseInt( rs.getString("no"));
String title=rs.getString("title");
String context=rs.getString("context");
Item item=new Item(no, title, context);
arrayList.add(item);
%>
<table>
<tr>
<td colspan="3"> 예제 테이블명 : jsp2 </td>
</tr>
<tr>
<th>no</th>
<td>title</td>
<td>context</td>
</tr>
<tr>
<td><%=no %></td>
<td>
<a href="day04_modify.jsp?no=<%=no%>">
<%=title%>
</a>
</td>
<td><%=context %></td>
</tr>
</table>
<%
}// while문 닫기
rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>게시판 입력 폼</title>
<style>
table{ border:1px thin red ;
border-style: solid ;
padding: 10px 10px 10px 10px ;
background-color: silver;
}
</style>
</head>
<body>
<form action="day04_insert.jsp">
<table>
<tr>
<td>글제목</td>
<td>
<input type="text" name="title" size="20"
required="required">
</td>
</tr>
<tr>
<td>글내용 </td>
<td>
<textarea rows="5" name="context"></textarea>
</td>
</tr>
<tr>
<td>
<input type="submit" value="저장">
</td>
</tr>
<tr>
</tr>
</table>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!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>수정/삭제 페이지 </title>
<link rel="stylesheet"
href="myStyle.css" type="text/css" >
<script>
function openWindow(url, name){
/* 다이얼로그 띄우기 */
window.open(url, name,
"scrollbars=yes, status=no, resizable=no,"
+ "width=500 , height=500");
}
</script>
</head>
<body>
<% String no=request.getParameter("no");
// no에 해당하는 데이터를 db에서 불러와서 화면에 보여주자
// 커넥션을 맺고(web.xml에서 기본으로 한다)
// no에 해당하는 데이터를 select문으로 불러와서
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
//2. 연결
try{
// test는 database 이름 입니다
// mysql안에 database 만드는 명령 :
// c:/> mysqladmin -u root create test -p
String url="jdbc:mysql://localhost:3306/test";
String user="root"; // 계정 이름
String password="1234"; //계정 비밀번호
conn=
DriverManager.getConnection(url, user, password);
out.print("연결성공 :"+ conn.toString());
}catch(Exception e){
out.print("연결 실패-"+e.getMessage());
%><%
}
String query=
"select * from jsp2 where no="+no ;
stmt=conn.createStatement();
//db에 명령을 날리면 결과가 옵니다
rs=stmt.executeQuery(query);
// resultSet을 읽어서 화면에 출력하기
String title="nothing";
String context="nothing";
if( rs.next() ){
title = rs.getString("title");
context = rs.getString("context");
}
%>
<form action="day04_update.jsp">
<input type="hidden" name="no"
value="<%=no %>">
<table>
<tr>
<td>글번호</td>
<td> <%=no %></td>
</tr>
<tr>
<td>글제목</td>
<td>
<input type="text" name="title" size="20"
value="<%=title %>"
required="required">
</td>
</tr>
<tr>
<td>글내용 </td>
<td>
<textarea rows="5" name="context">
<%=context %>
</textarea>
</td>
</tr>
<tr>
<td>
<input type="submit" value="수정">
</td>
<td>
<input type="button" value="삭제"
onClick="openWindow('day04_delete.jsp?no=<%=no%>', 'db에서 <%=no %>번 글 지우기')" >
</td>
</tr>
<tr>
</tr>
</table>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import ="java.sql.*" %>
<!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>db에 인서트 하기</title>
</head>
<body>
<%
//1 . 드라이버 로딩
Class.forName("com.mysql.jdbc.Driver");
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
//2. 연결
try{
//test는 database 이름 입니다
//mysql안에 database 만드는 명령:
//c:/>mysqladmin -u root creat test -p
String url="jdbc:mysql://localhost:3306/test";
String user="root"; // 계정 이름
String password="1234"; //계정 비밀번호
conn = DriverManager.getConnection(url, user, password);
out.print("연결성공 :"+ conn.toString());
}catch(Exception e){
out.print("연결 실패-"+e.getMessage());
}
/* create table jsp2(no integer primary key auto_increment,
title varchar(20),context varchar(100)); */
String sql="insert into jsp2 (title,context) values(?,?);";
PreparedStatement preState = conn.prepareStatement(sql);
String title=request.getParameter("title");//첫번째?
String context=request.getParameter("context");//두번째?
preState.setString(1,title);//첫번째 ?값을 셋팅하기
preState.setString(2,context);//두번째 ?값을 셋팅하기
int outcome=-1;
outcome=preState.executeUpdate();//insert 문 실행
if(outcome>0){
out.print("insert 성공 <br/>");
out.print("결과"+outcome);
}
%>
db에 insert하고 dayo4_list.jsp로 이동하게 만듭니다.
<jsp:forward page="day04_list.jsp"></jsp:forward>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="connection.ConnectionProvider" %>
<!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>파라미터를 받아서 db업데이트 하기</title>
</head>
<body>
<%
Connection conn=
ConnectionProvider.getConnection() ;
// 파라미터 3개 받아서 update명령을 날리기
String no=request.getParameter("no");
String title=request.getParameter("title");
String context=request.getParameter("context");
String sql="update jsp2 set title=? , context= ? where no=? ";
PreparedStatement preState= conn.prepareStatement(sql);
preState.setString(1, title); //1th ?
preState.setString(2, context); //2th ?
preState.setString(3, no); //3th ?
int outcome=preState.executeUpdate();
out.print("update 리턴값 : "+ outcome +" <br/>");
// 업데이트 되었습니다. list로 돌아가겠습니까 바로가기 만들기
%>
a태그가 싫을 때 간단하게 넘어가는 방법입니다 <br />
&sp; button onClick:= " javascript:location.href=' x.jsp ' ">
<br />
<button onClick="javascript:location.href='day04_list.jsp' ">
list로 되돌아가기
</button>
insert/ delete / update/ select /select All
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!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>request 파라미터 no값에 해당하는 row지우기</title>
</head>
<body>
<%
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
//2. 연결
try{
// test는 database 이름 입니다
// mysql안에 database 만드는 명령 :
// c:/> mysqladmin -u root create test -p
String url="jdbc:mysql://localhost:3306/test";
String user="root"; // 계정 이름
String password="1234"; //계정 비밀번호
conn=
DriverManager.getConnection(url, user, password);
out.print("연결성공 :"+ conn.toString());
}catch(Exception e){
out.print("연결 실패-"+e.getMessage());
}
String sql="delete from jsp2 where no=?";
PreparedStatement preState=
conn.prepareStatement(sql);
String no=request.getParameter("no");
preState.setString(1, no);
int outcome = preState.executeUpdate();
out.print("outcome="+outcome+"<br />");
out.print(no+"에 해당하는 데이터를 지웠습니다 <br/>");
%>
<br/>
<a href="day04_list.jsp"> list보기로 되돌아가기 </a>
</body>
</html>
ConnectionProvider.java
day04_delete.jsp
day04_insert.jsp
day04_list.jsp
day04_modify.jsp
day04_update.jsp
day04_writeForm.jsp