Tech Story/IoT

웹으로 DB에 데이터 저장하기

슈퍼맨짱 2018. 6. 18. 13:34

웹으로 DB에 데이터 저장하기

 

[ 입력창 - inisert.html ]

 

<!DOCTYPE html>

<html>

<head>

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

<title>Insert title here</title>

<style type="text/css">

   #regbox{ width : 300pxl }

   #regbox{ display : block; width: 100px; float: left; }

</style>

</head>

<body>

<form method="post" action="insert.jsp">

   <fieldset id="regbox">

      <legend>Insert Data...</legend>

      <label for="drone_id">drone id</label>

        <input type="text" name="drone_id"/><br/>

      <label for="dust_id">dust id</label>

        <input type="text" name="dust_id"/><br/>

      <label for="gps_id">gps id</label>

        <input type="text" name="gps_id"/><br/>

      <label for="chkpmValue">result</label>

        <input type="number" step=any name="chkpmValue"/><br/>

      <label for="pm25Value">std_result</label>

        <input type="number" step=any name="pm25Value"/><br/>

      <label for="pm10Value">std_result</label>

        <input type="number" step=any name="pm10Value"/><br/>

      <input type="submit" value="insert">

      <input type="reset" value="cancel"/>

   </fieldset>

</form>

</body>

</html>

 
화면

 

 
[ Form - insert.jsp ] 

<%@page import="java.text.DecimalFormat"%>

<%@page import="java.sql.*"%>

<%@page import="java.util.*"%>

<%@page import="org.json.JSONObject"%>

<html>

<head>

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

<title>Insert title here</title>

<head>

<body>

<%

    request.setCharacterEncoding("utf-8");

    String drone_id=request.getParameter("drone_id");

    String dust_id=request.getParameter("dust_id");

    String gps_id=request.getParameter("gps_id");

    String chkpmValue=request.getParameter("chkpmValue");

    String pm25Value=request.getParameter("pm25Value");

    String pm10Value=request.getParameter("pm10Value");

 

    //커넥션 선언

    Connection con = null;

    int n=0;

 

    try {

        //드라이버 호출, 커넥션 연결

        Class.forName("com.mysql.jdbc.Driver").newInstance();

        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql?autoReconnect=true&useSSL=false", "ID", "PASSWD");

        String query = "insert into dust(drone_id, dust_id, gps_id, chkpmValuepm25Valuepm10Value, datecreated) values(?,?,?,?,?,?,now())";

    PreparedStatement pstmt = con.prepareStatement(query);

 

        pstmt=con.prepareStatement(query);

pstmt.setString(1,drone_id);

pstmt.setString(2,dust_id);

pstmt.setString(3,gps_id);

pstmt.setString(4,chkpmValue);

pstmt.setString(5,pm25Value);

        pstmt.setString(5,pm10Value);

 
n=pstmt.executeUpdate();
 
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (con != null) {
            try {
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
 
    }
%>
 
<script type="text/javascript">
    if(<%=n%> > 0){
alert("success...");
    }else {
alert("fail ...");
    }
</script>
</body>
</html>
 
 

*직접 호출 방법 : http://localhost:8080/insert.jsp?drone_id=drone01&dust_id=dust01&gps_id=Eulgiro4ga&chkpmValue=18.444&pm25Value=22.473&pm10Value=27.987

 

 

[해당파일]

https://github.com/e3jake/DustProject