Ajax dynamically gets the values in the database

Source: Internet
Author: User
Tags stmt

Feature Description: Enter content in an input box when content length >1 back to send Ajax from the database to get information about the text box displayed in the foreground, the foreground design some click events, and the application of jquery

1 using the requestmapping annotation in the SPRINGMVC, attach the configuration file first

Web. XML configuration:

<servlet><servlet-name>springDispatcherServlet</servlet-name><servlet-class> Org.springframework.web.servlet.dispatcherservlet</servlet-class><init-param><param-name> contextconfiglocation</param-name><param-value>classpath:springmvc.xml</param-value></ init-param><load-on-startup>1</load-on-startup></servlet><!--Map all requests to the Dispatcherservlet for handling--><servlet-mapping><servlet-name>springdispatcherservlet</ Servlet-name><url-pattern>/</url-pattern></servlet-mapping>

 

The Springmvc.xml configuration is as follows:
<!--Configuring a custom Scan package--><context:component-scan base-package= "Com.rl.util" ></context:component-scan> <!--configuration View resolver: How to resolve the handler method return value to the actual physical view--><bean class= " Org.springframework.web.servlet.view.InternalResourceViewResolver "><property name=" prefix "value="/web-inf /views/"></property><property name=" suffix "value=". JSP "></property></bean>

  

The front page is as follows:

<%@ 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" >

The code for the

Accept request in the background is as follows:

Package Com.rl.util;import Java.sql.connection;import Java.sql.drivermanager;import java.sql.resultset;import Java.sql.sqlexception;import Java.sql.statement;import Java.util.arraylist;import Java.util.List;import Javax.servlet.http.httpservletrequest;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.responsebody;import Com.google.gson.gson;import Com.google.gson.gsonbuilder;import Net.sf.json.JSONArray; @Controllerpublic class Control {@RequestMapping ("/ad") @  Responsebodypublic String getList (httpservletrequest request) {//httpservletrequest request=  Servletactioncontext.getrequest (); List<string> list=new arraylist<> (); Gson gson2 = new Gsonbuilder (). Enablecomplexmapkeyserialization (). Create (); try {class.forname ("com.mysql.jdbc.Driver");//import drive (Jar package)} catch (ClassNotFoundException e) {e.printstacktrace ();} String name = ""; Connection conn = null;try {conn = (ConnectION) Drivermanager.getconnection ("Jdbc:mysql://localhost:3306/xxff", "root", "123456");//Establish connection} catch (SQLException E ) {e.printstacktrace ();}  String sql = "Select Name,email,dob from Student";//Establish SQL statement System.out.println (request.getparameter ("PostData"));    String postdata = Request.getparameter ("PostData");  if (postdata==null) {System.out.println ("null" + "PostData");  } else{PostData = Postdata.trim (); System.out.println ("Trubleshooting.java Timestap =" +postdata); String sql = "Select login_name from Tab_account WHERE login_name like '%" + "" +postdata+ "% '";//where login_name like '%p ostdata% '/*list<map<string, string>> List = new arraylist<> (); map<string, string> map = new hashmap<> (); */try {Statement stmt = (Statement) conn.createstatement (); ResultSet rs = stmt.executequery (sql);//Cursorwhile (Rs.next ()) {name = rs.getstring (1);//list.add (name);/* * String EMA Il = rs.getstring (2); String DOB = rs.getstring (3); *///SYSTEM.OUT.PRINTLN (name/* + "," +email+ "," +dob */); SYSTEM.OUT.PRINTLN (name);//map.put ("name", name); List.add (name);}} catch (SQLException e) {e.printstacktrace ();} finally {if (conn! = null) try {conn.close ();//Close connection} catch (SQLException e)  {E.printstacktrace ();}} } String Json=jsonarray.fromobject (list). ToString (); Jsonarray Jsonarray = Jsonarray.fromobject (JSON); List<string> List2 = (List) jsonarray.tocollection (Jsonarray); return json;}}

  

Ajax dynamically gets the values in the database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.