代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
PHP
】
查询所输入的手机号的基本信息
作者:
路人甲123
/ 发布于
2015/8/3
/
669
1.实体类 package com.bw.model; import javax.persistence.Id; import javax.validation.constraints.Size; import org.hibernate.validator.constraints.NotEmpty; public class Mobile { private String number; //手机号码 private String local; //所属地区 private String type; //号码类型 private String description; //区号 private String code; //邮编 public Mobile(){}; public Mobile(String number ,String local,String type, String description,String code){ super(); this.number=number; this.local =local; this.type=type; this.description=description; this.code=code; } @Id @NotEmpty(message="不能为空") @Size(min=11,max=11,message="手机号码应为11位") public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public String getLocal() { return local; } public void setLocal(String local) { this.local = local; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } } 2控制器 package com.bw.controller; import java.io.UnsupportedEncodingException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.HashMap; import java.util.Map; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.bw.model.Mobile; @Controller @RequestMapping("/number") public class MobileController{ private Map<String,Mobile> m=new HashMap<String,Mobile>(); @RequestMapping(value="/search",method=RequestMethod.GET) public String search1(Model model){ System.out.println("跳转到查询首页"); model.addAttribute(new Mobile()); return "search"; } @RequestMapping(value="/search",method=RequestMethod.POST) public String search(Mobile mobile,ModelMap model) throws Exception { //得到要查询的手机号码 m.put(mobile.getNumber(),mobile); //连接数据库并查找该手机号码是否存在 Connection conn; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/databa?useUnicode=true&characterEncoding=UTF-8","root",""); String strSql = "select number from mobile where number=?" ; PreparedStatement ps = conn.prepareStatement(strSql); ps.setString(1, mobile.getNumber()); ResultSet result = ps.executeQuery(); //若存在,则将该手机号码下的所有信息进行查询,并显示到页面上 if(result.next()){ String strSql1 = "select * from mobile where number=?" ; PreparedStatement ps1 = conn.prepareStatement(strSql1); ps1.setString(1, mobile.getNumber()); ResultSet re = ps1.executeQuery(); while(re.next()){ System.out.println(re.getString("number")); model.addAttribute("aa",re.getString("number")); model.addAttribute("bb",re.getString("local")); model.addAttribute("cc",re.getString("type")); model.addAttribute("dd",re.getString("description")); model.addAttribute("ee",re.getString("code")); System.out.println(re.getString("code")); } } //若不存在,则先将该手机号码发到查询地址处,然后把主要信息读取、显示出来,并把信息保存在数据库中 else{ Document doc = Jsoup.connect("http://www.ip138.com:8080/search.asp?action=mobile&mobile="+mobile.getNumber()).get(); Element body = doc.body(); Document doc2 = Jsoup.parse(body.toString()); @SuppressWarnings("unused") Elements links = doc2.select("a[href]").remove(); @SuppressWarnings("unused") Elements text2 = doc2.select("tr").select("td"); String result1 = doc2.select("tr").select("td").get(4).text(); String result2 = doc2.select("tr").select("td").get(6).text(); String result3 = doc2.select("tr").select("td").get(8).text(); String result4 = doc2.select("tr").select("td").get(10).text(); String result5 = doc2.select("tr").select("td").get(12).text(); String local=new String(result2.getBytes(),"utf-8"); String type=new String(result3.getBytes(),"utf-8"); model.addAttribute("aa", result1).addAttribute("bb", result2).addAttribute("cc", result3). addAttribute("dd", result4).addAttribute("ee", result5); MobileController mc=new MobileController(); mc.save(result1, local, type, result4, result5); } conn.close(); } catch (SQLException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return "shuchu"; } //对第一次查询号码的保存 public void save(String number, String local, String type, String description, String code) throws ClassNotFoundException, UnsupportedEncodingException { Connection conn; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/databa","root",""); String strSql = "insert into mobile(number,local,type,description,code)" + "values(?,?,?,?,?)"; PreparedStatement ps = conn.prepareStatement(strSql); ps.setString(1, number); ps.setString(2, local); ps.setString(3, type); ps.setString(4, description); ps.setString(5, code); @SuppressWarnings("unused") int result = ps.executeUpdate(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } 3两个JSP页面 search.jsp: <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@taglib prefix="sf" uri="http://www.springframework.org/tags/form" %> <html> <head> </head> <body> <sf:form method="post" modelAttribute="mobile"> 输入手机号码:<sf:input path="number"/> <input type="submit" value="查询"> <br/> </sf:form> </body> </html> shuchu.jsp <%@ page language="java" import="java.sql.*,java.io.*,java.util.*"%> <%@ page contentType="text/html;charset=utf-8"%> <%@page import="com.bw.model.Mobile" %> <%@page import="com.bw.controller.*" %> <%@ page isELIgnored="false" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <html> <head> </head> <body> <table> <tr> <td>查询的手机号码:</td> <td>${aa}</td> </tr> <tr> <td>号码的归属地:</td> <td>${bb}</td> </tr> <tr> <td>电话卡的种类:</td> <td>${cc}</td> </tr> <tr> <td>所属地区区号:</td> <td>${dd}</td> </tr> <tr> <td>所属地区邮编:</td> <td>${ee}</td> </tr> </table> </body> </html> 4主要的代码程序上传完毕,其余的配置什么的都是springmvc的一些基本配置
试试其它关键字
同语言下
.
用net匹配并替换iOS标准的emoji表情符号
.
处理带Emoji表情的的字符串
.
获取微信昵称时 过滤特殊字符
.
通过判断上传文件的头字符来判断文件的类型
.
模拟百度URL加密解密算法
.
以太坊检查地址是否合法
.
实现crontab解析类
.
获取每个月的开始和结束时间
.
图片上传工具类
.
APP手机应用信息采集
可能有用的
.
用net匹配并替换iOS标准的emoji表情符号
.
处理带Emoji表情的的字符串
.
获取微信昵称时 过滤特殊字符
.
通过判断上传文件的头字符来判断文件的类型
.
模拟百度URL加密解密算法
.
以太坊检查地址是否合法
.
实现crontab解析类
.
获取每个月的开始和结束时间
.
图片上传工具类
.
APP手机应用信息采集
路人甲123
贡献的其它代码
(
3
)
.
查询所输入的手机号的基本信息
.
获取本周第一天,本周最后一天,一天的开始,一天的结
.
给图片加文字水印
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3