html에서 부서코드를 입력받아 Mysql 실행

2016. 9. 6. 17:47프로그래밍/jsp

반응형

<!DOCTYPE html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

<h2> 부서코드 입력</h2>

<form action="MyDivisionSelectDept.jsp">

부서코드 : <input type="number" name="dno" required="required"><p>

<input type="submit" value="확인">

</form>



</body>

</html>


html문을 작성


<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8" 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>Insert title here</title></head><body>

<% String dno = request.getParameter("dno");

String driver = "com.mysql.jdbc.Driver";

String url = "jdbc:mysql://127.0.0.1:3306/test?useSSL=false";

String sql = "select * from division where dno="+dno;

Connection conn=null; Statement stmt=null; ResultSet rs=null;

try { Class.forName(driver);

conn=DriverManager.getConnection(url,"root","mysql");

stmt = conn.createStatement();

rs = stmt.executeQuery(sql);

if (rs.next()) {

out.println("부서코드 : "+dno+"<p>");

out.println("부서명 : "+rs.getString(2)+"<p>");

out.println("전화번호 : "+rs.getString(3)+"<p>");

out.println("근무지 : "+rs.getString(4));

} else out.println("없는 부선데");

}catch(Exception e) { System.out.println(e.getMessage());

}finally {

if (rs != null) rs.close();

if (stmt != null) stmt.close();

if (conn != null) conn.close();

}

%>

</body>

</html>


반응형

'프로그래밍 > jsp' 카테고리의 다른 글

작성중인 문서입니다!  (0) 2016.09.07
html를 이용한 Mysql DB추가  (0) 2016.09.07
mysql 연동하기  (0) 2016.09.06
피보나치 수열 합산!  (0) 2016.09.01
doWhile과 table을 이용해 구구단 출력  (0) 2016.09.01