如何取数据四 servlet+JDBC模式

1、cunmysql.jsp

action 加入 jdbcdaomysql

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html data-embedded="">
<head>
  <meta charset="utf-8">
  <title>思学课程报名</title>
  <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
  <meta name="description" content= '为思学课程报名提交数据。&lt;p&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;n...'>
  <meta property="og:title" content='思学课程报名' >
<meta property="og:description" content='为思学课程报名提交数据。&lt;p&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;n...'>
<meta property="og:image" content='https://cdn.jinshuju.net/assets/weixin_thumbnail/wx-img-1-e99378d457b9d77ca8e12f4e33db820e30357ae184aa3e24306eda1f9b268cff.png' >

 <link rel="stylesheet" media="screen" href="jscss/1.css" debug="false" />
  
  <script>
  (function () {
    var js;
    if (typeof JSON !== 'undefined' && 'querySelector' in document && 'addEventListener' in window) {
      js = 'jscss/jquery1.js';
    } else {
      js = 'jscss/jquery2.js';
    }
    document.write('<script src="' + js + '"><\/script>');
  }());
  
 
</script>


  <style type="text/css">
    
  </style>

  <script src="jscss/application1.js" debug="false"></script>
  


  
  <meta name="csrf-param" content="authenticity_token" />
<meta name="csrf-token" content="e2aTwlp9/P7Nw74n9G6tLy560ap1WaoMdqbvQTIo6C1O9HjXR4shvCv5ZU0/f3r4ITF4bFcTwpaG9iG+FbXfTA==" />
</head>
<body class="entry-container">
  <div class="entry-container-inner">
    
<!-- 
      <header class="clearfix">
        <div class="center user-info">
          <a class="avatar-link" href="https://jinshuju.net/">
            <span class="user-name">欢迎</span>
          </a>
          <a class="logout-link" href="/logout.json">登录</a>
        </div>
      </header> -->

    <style type="text/css">
  .entry-container {
    background-color: #000000;
background-image: image/1920.jpg;
background-position: top center;
background-attachment: fixed;
background-size: cover;
background-repeat: no-repeat;
    
  }

  @media screen and (min-width: 769px) and (max-width: 1440px) {
    .entry-container {
      background-color: #000000;
background-image: url(https://pub-files.jinshuju.net/bgi/20160221174317_2a10a7?imageView2/2/w/1440);
background-position: top center;
background-attachment: fixed;
background-size: cover;
background-repeat: no-repeat;
      
    }
  }

  @media screen and (min-width: 481px) and (max-width: 768px) {
    .entry-container {
      background-color: #000000;
background-image: url(https://pub-files.jinshuju.net/bgi/20160221174317_2a10a7?imageView2/2/w/768);
background-position: top center;
background-attachment: fixed;
background-size: cover;
background-repeat: no-repeat;
      
    }
  }

  @media screen and (max-width: 480px) {
    .entry-container {
      background-color: #000000;
background-image: url(https://pub-files.jinshuju.net/bgi/20160221174317_2a10a7?imageView2/2/w/480);
background-position: top center;
background-attachment: fixed;
background-size: cover;
background-repeat: no-repeat;
    }
  }
</style>

<style type="text/css">
  .entry-container > .entry-container-inner .center {
  max-width: 700px;
  }

  .entry-container > .entry-container-inner > form {
  background-color: rgb(250, 255, 255);
max-width: 700px;
border-width: 0;
border-color: rgb(36, 121, 115);
  }

  .entry-container > .entry-container-inner > form > .banner {
  background-color: rgb(36, 121, 115);
font-size: 1.333em;
font-weight: normal;
color: rgb(255, 255, 255);
text-align: left;
  }

  .entry-container > .entry-container-inner > form > .form-header .form-title {
  font-size: 1.667em;
font-weight: normal;
color: rgb(0, 0, 0);
text-align: center;
  }

  .entry-container > .entry-container-inner > form .fields .field {
  padding-top: 15px;
padding-bottom: 15px;
  }

  .entry-container > .entry-container-inner > form .fields .field .field-label {
  font-size: 1.167em;
font-weight: bold;
color: rgb(0, 0, 0);
  }

  .entry-container > .entry-container-inner > form .fields .field .choices label,
  .entry-container > .entry-container-inner > form .fields .field .likert thead th,
  .entry-container > .entry-container-inner > form .fields .field .matrix thead th,
  .entry-container > .entry-container-inner > form .fields .field .goods-items .name {
  font-size: 1.000em;
font-weight: normal;
color: rgb(0, 0, 0);
  }

  .entry-container > .entry-container-inner > form .submit-field {
  text-align: left;
  }

  form .submit-field .gd-btn {
  background-color: rgb(68, 74, 74);
padding: 0 55px;
font-size: 1.167em;
font-weight: normal;
color: #FFF;
border-width: 1px;
border-color: transparent;
  }

</style>



<form class="center with-shadow"  id="new_entry" action="jdbcdaomysql" accept-charset="UTF-8" method="post">
<input type="hidden" name="utf8" value="✓" />
      <div class="banner font-family-heiti">
          <div class="banner-img">
     <img data-img-url="https://pub-files.jinshuju.net/hi/20160221174333_4cda5c" data-img-prefix="hi" class="header-image need-adapt-retina" src="" /> 
           <!--   <img  src="image/shangfutu.jpg" /> -->
          </div>
    </div>
  <div class="form-header container-fluid">
    <div class="row">
      <h1 class="form-title col-md-12 font-family-heiti">
        思学课程报名
      </h1>
      <div class="form-description col-md-12"><p>               思学课程,帮助每一位孩子学会充分利用课堂时间,快乐学习,高效记忆!</p></div>
    </div>
  </div>
  <div class="form-content container-fluid">
    <div class="row">
      
        <div class="fields clearfix">
              <div class="field field-section-break col-sm-12" data-api-code="field_6" data-type="SectionBreak" data-label="null" data-validations="[]">
                  <hr/>
<div class="form-group" >
      <div class="field-label-container" οnclick="">
        <label class="field-label font-family-heiti" for="entry_field_6">
</label>        
</div>
  <div class="field-content">  

  </div>
</div>


</div>              <div class="field field-name-field col-sm-12" data-api-code="field_9" data-type="NameField" data-label="孩子姓名" data-validations="[]">
                

<div class="form-group" >
      <div class="field-label-container" οnclick="">
        <label class="field-label font-family-heiti" for="entry_field_9">
            孩子姓名
</label>        
</div>
  <div class="field-content">

    <input class="input-with-icon" data-icon="gd-icon-name" type="text" name="entry[field_9]" id="entry_field_9" />

  </div>
</div>


</div>            
  <div class="field field-name-field col-sm-12" data-api-code="field_17" data-type="NameField" data-label="家长姓名" data-validations="[]">
                

<div class="form-group" >
      <div class="field-label-container" οnclick="">
        <label class="field-label font-family-heiti" for="entry_field_17">
            家长姓名
</label>        
</div>
  <div class="field-content">

    <input class="input-with-icon" data-icon="gd-icon-name" type="text" name="entry[field_17]" id="entry_field_17" />

  </div>
</div>


</div>             
 <div class="field field-name-field col-sm-12" data-api-code="field_18" data-type="NameField" data-label="年龄" data-validations="[]">            

   <div class="form-group" >
      <div class="field-label-container" οnclick="">
        <label class="field-label font-family-heiti" for="entry_field_18">
                 年龄
        </label>        
      </div>
       <div class="field-content">
          <input class="input-with-icon" data-icon="gd-icon-name" type="text" name="entry[field_18]" id="entry_field_18" />
      </div>
   </div>
</div> 
          
 <div class="field field-drop-down col-sm-12 required" data-api-code="field_7" data-type="DropDown" data-label="年级" data-validations="["Presence"]">
          

<div class="form-group" >
      <div class="field-label-container" οnclick="">
        <label class="field-label font-family-heiti" for="entry_field_7">
            年级
</label>        
</div>
  <div class="field-content">

    <select name="entry[field_7]" id="entry_field_7" class="needsclick" data-has-error="false"><option value="">请选择</option><option value="一年级">一年级</option>
<option value="二年级">二年级</option>
<option value="三年级">三年级</option>
<option value="四年级">四年级</option>
<option value="五年级">五年级</option>
<option value="六年级">六年级</option>
<option value="小升初">小升初</option></select>
  </div>
</div>
</div>       


   <div class="field field-mobile-field col-sm-12 required" data-api-code="field_16" data-type="MobileField" data-label="家长手机" data-validations="["Presence","Format","VerificationTokenPresence"]">
              

<div class="form-group" >
      <div class="field-label-container" οnclick="">
        <label class="field-label font-family-heiti" for="entry_field_16">
            家长手机
</label>        
</div>
  <div class="field-content">
    <div data-role='verification_sender'>
  <input class="mobile-input input-with-icon" data-icon="gd-icon-mobile" type="tel" name="entry[field_16]" id="entry_field_16" />
    <input value="true" type="hidden" name="entry[field_16_skip_verification]" id="entry_field_16_skip_verification" />
   </div>
    </div>
    </div>
   </div>  
 </div>
 
  <input type="submit" name="commit" value="提交" οnclick="severCheck();" >
    </div>
  </div>
</form>
    <script type="text/javascript">
    function severCheck()    
 {    
     //取出id为“one”表单中所有的属性的数据    
     //alert("ok");
    var form = document.getElementById("one2");    
    /*  var loginname = $("#userName").val();   
     alert (loginname);  */  
       
   var yonghuming = document.getElementById("entry_field_9").value;   
     alert (yonghuming);   
    //one表单中第一个元素属性值    
/*     var content = form.elements(0).value;     
    alert(content); //user的值     */
        
    //也可以直接通过元素的属性Id来直接获取    
    var jzxm = document.getElementById("entry_field_17").value;    
    alert(jzxm);
 }
  </script>
 
    
</body>
</html>

2、web.xml 配置  

同时记得拷贝 mysqlconnet的jar包

<servlet>
		<servlet-name>jdbcdaomysqldemo</servlet-name>
		<servlet-class>cn.jieshoucanshu.jdbcdaomysql</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>jdbcdaomysqldemo</servlet-name>
		<url-pattern>/jdbcdaomysql</url-pattern>
	</servlet-mapping>


3、jdbcdaomysql.java

package cn.jieshoucanshu;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class jdbcdaomysql extends HttpServlet {
	private static final long serialVersionUID = 1789481329876401944L;
	// ��ݿ�����Connection
	private Connection conn;

	// ��ʼ������
	public void init() throws ServletException {
		super.init();
		try {
			// ������
			System.out.println("laizhel");
			Class.forName("com.mysql.jdbc.Driver");
			// ��ݿ�����url
			String url = "jdbc:mysql://localhost:3306/tcxb";
			// ��ȡ��ݿ�����
			conn = DriverManager.getConnection(url, "root", "admin");
		
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// doPost()��������ע������
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// ����request��response�ı���
		System.out.println("laizhe");
		response.setContentType("text/html");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
			String xsxm = request.getParameter("entry[field_9]");
		String jzxm = request.getParameter("entry[field_17]");
		String old = request.getParameter("entry[field_18]");
		String nianji = request.getParameter("entry[field_7]");
		String iphone = request.getParameter("entry[field_16]");
		// �ж���ݿ��з����ӳɹ�
		if (conn != null) {
			try {
				// 插入数据库
				String sql = "insert into baoming(xsxm,jzxm,old,nianji,iphone) "
						+ "values(?,?,?,?,?)";
				// ����PreparedStatement����
				PreparedStatement ps = conn.prepareStatement(sql);
				// ��SQL����еIJ���̬��ֵ
				ps.setString(1, xsxm);
				ps.setString(2, jzxm);
				ps.setString(3, old);
				ps.setString(4, nianji);
				ps.setString(5, iphone);
				
				// 执行语句
				ps.executeUpdate();
				// ��ȡPrintWriter����
				PrintWriter out = response.getWriter();
				// ���ע������Ϣ
				out.print("<h1 aling='center'>");
				out.print(xsxm + "导入数据库成功");
				out.print("</h1>");
				out.flush();
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			// ������ݿ����Ӵ�����ʾ��Ϣ
			response.sendError(500, "500服务器报错");
		}
	}
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值