在实现程序的修改模块时,要在页面端检查用户是否修改过数据,以便提醒用户及时保存修改后的数据。
实现要求:
判断用户是否修改了输入的内容,要能判断input中的checkbox,text,radio等,不知道会多少个input,也不知道input的ID和name,要在客户端判断是否修改,不能产
生提交。还要同时满足IE和firefox两种环境。也要支持master和content页的使用。
解决方案;
处理思路:在页面加载时记录所有的input的值或状态,如果要求在用户进行修改操作时立即调整控件状态,则在数据区div上的onclick和onkeypress事件里调用检查数据
是否改变的函数,并设置相应控件的状态(用ha_setch())。如果是在提交或用户手动控制检查过程时,则在相应的操作事件中调用检查数据是否改变(例在button的onclick
()中使用ha_checkin()控制提交)。
以下是实现代码
var ha_last=new Array;//定义一个全局的空对象,存放所有的初始值。
function ha_get()//读取初始值
{ var ha_input = document.getElementsByTagName("input");
for (var i=0;i<ha_input.length;i++)
{
if (ha_input[i].type=="password"){ ha_last.push(ha_input[i].value);}//根本页面需要设置type的值和对象属性
if (ha_input[i].type=="radio") {ha_last.push(ha_input[i].checked);}//要保证检查的范围是否准确
}
}
window.onload=ha_get;//绑定读取初始值的函数
function ha_checkin()//检查新的输入值和初始值是否相等。返回判断结果。true为没有发生修改,false为有修改。
{ var ha_now=new Array;
var ha_input = document.getElementsByTagName("input");
for (var i=0;i<ha_input.length;i++)
{
if (ha_input[i].type=="password"){ ha_now.push(ha_input[i].value);}//根本页面需要设置type的值和对象属性。
if (ha_input[i].type=="radio") {ha_now.push(ha_input[i].checked);}//也要保证和ha_get()中检查的标签相一致
}
if (ha_now.toString()==ha_last.toString())//没修改
{return true;}
else//有修改
{return false;}
}
function ha_setch(){//设置相应的控件状态
if (ha_checkin())//没有变化
//改变控件的显示和功能状态
else//有变化
//改变控件的显示和功能状态
}
做了适当的修改。最后在button里面onclick直接调用
var ha_last=new Array;//定义一个全局的空对象,存放所有的初始值。
function ha_get()//读取初始值
{
var ha_input = document.getElementsByTagName("input");
ha_last.push($("#select").val());//采用jquery获取select 选择的值
for (var i=0;i<ha_input.length;i++)
{
if (ha_input[i].type=="text"){ ha_last.push(ha_input[i].value);}//根本页面需要设置type的值和对象属性
}
}
window.onload=ha_get;//绑定读取初始值的函数
function ha_checkin(action)//检查新的输入值和初始值是否相等。返回判断结果。true为没有发生修改,false为有修改。
{
if(action=="update"){
var ha_now=new Array;
var ha_input = document.getElementsByTagName("input");
ha_now.push($("#select").val());
for (var i=0;i<ha_input.length;i++)
{
if (ha_input[i].type=="text"){ ha_now.push(ha_input[i].value);}//根本页面需要设置type的值和对象属性。
}
if (ha_now.toString()==ha_last.toString())//没修改
{
alert("没有修改");
return false;
}
else//有修改
{
// return true;
}
}
}
来自:http://www.javaeye.com/topic/304818
jQuery 是我最喜欢的一个Javascript框架,下表中包含了240+可以为jQuery爱好者们使用的插件。我断定一定还有下表没有包括的,如果你还知道其他的好东西,欢迎与大家分享。
文件上传
Ajax File Upload
jQUploader
Multiple File Upload plugin
jQuery File Style
Styling an input type file
Progress Bar Plugin
表单验证
jQuery Validation
Auto Help
Simple jQuery form validation
jQuery XAV - form validations
jQuery AlphaNumeric
Masked Input
TypeWatch Plugin
Text limiter for form fields
Ajax Username Check with jQuery
Form - Select Box stuff
jQuery Combobox
jQuery controlled dependent (or Cascadign) Select List
Multiple Selects
Select box manipulation
Select Combo Plugin
jQuery - LinkedSelect
Auto-populate multiple select boxes
Choose Plugin (Select Replacement)
表单基本、输入与、复选框等
jQuery Form Plugin
jQuery-Form
jLook Nice Forms
jNice
Ping Plugin
Toggle Form Text
ToggleVal
jQuery Field Plugin
jQuery Form’n Field plugin
jQuery Checkbox manipulation
jTagging
jQuery labelcheck
Overlabel
3 state radio buttons
ShiftCheckbox jQuery Plugin
Watermark Input
jQuery Checkbox (checkboxes with imags)
jQuery SpinButton Control
jQuery Ajax Form Builder
jQuery Focus Fields
jQuery Time Entry
时间,日期和颜色选取器
jQuery UI Datepicker
jQuery date picker plugin
jQuery Time Picker
Time Picker
ClickPick
TimePicker
Farbtastic jQuery Color Picker Plugin
Color Picker by intelliance.fr
投票插件
jQuery Star Rating Plugin
jQuery Star Rater
Content rater with asp.net, ajax and jQuery
Half-Star Rating Plugin
搜索插件
Autocomplete Box
jQuery Suggest
jQuery Autocomplete
jQuery Autocomplete Mod
jQuery Autocomplete by AjaxDaddy
jQuery Autocomplete Plugin with HTML formatting
jQuery Autocompleter
AutoCompleter (Tutorial with PHP&MySQL)
quick Search jQuery Plugin
编辑器
jTagEditor
WYMeditor
jQuery jFrame
Jeditable - edit in place plugin for jQuery
jQuery editable
jQuery Disable Text Select Plugin
Edit in Place with Ajax using jQuery
jQuery Plugin - Another In-Place Editor
TableEditor
tEditable - in place table editing for jQuery
声音,视频,Flash, SVG等
jMedia - accessible multi-media embedding
JBEdit - Ajax online Video Editor
jQuery MP3 Plugin
jQuery Media Plugin
jQuery Flash Plugin
Embed QuickTime
SVG Integration
jQuery Multimedia Portfolio
jQuery YouTube Plugin
图片
ThickBox
jQuery lightBox plugin
jQuery FancyBox
jQuery Multimedia Portfolio
jQuery Image Strip
jQuery slideViewer
jQuery jqGalScroll 2.0
jQuery - jqGalViewII
jQuery - jqGalViewIII
jQuery Photo Slider
jQuery Thumbs - easily create thumbnails
jQuery jQIR Image Replacement
jCarousel Lite
jQPanView
jCarousel
Interface Imagebox
Image Gallery using jQuery, Interface & Reflactions
simple jQuery Gallery
jQuery Gallery Module
EO Gallery
jQuery ScrollShow
jQuery Cycle Plugin
jQuery Flickr
jQuery Lazy Load Images Plugin
Zoomi - Zoomable Thumbnails
jQuery Crop - crop any image on the fly
Image Reflection
Google Map
jQuery Plugin googlemaps
jMaps jQuery Maps Framework
jQmaps
jQuery & Google Maps
jQuery Maps Interface forr Google and Yahoo maps
jQuery J Maps - by Tane Piper
游戏
Tetris with jQuery
jQuery Chess
Mad Libs Word Game
jQuery Puzzle
jQuery Solar System (not a game but awesome jQuery Stuff)
jQuery Memory
表格
UI/Tablesorter
jQuery ingrid
jQuery Grid Plugin
Table Filter - awesome!
TableEditor
jQuery Tree Tables
Expandable “Detail” Table Rows
Sortable Table ColdFusion Costum Tag with jQuery UI
jQuery Bubble
TableSorter
Scrollable HTML Table
jQuery column Manager Plugin
jQuery tableHover Plugin
jQuery columnHover Plugin
jQuery Grid
TableSorter plugin for jQuery
tEditable - in place table editing for jQuery
jQuery charToTable Plugin
jQuery Grid Column Sizing
jQuery Grid Row Sizing
图表
Flot
jQuery Wizard Plugin
jQuery Chart Plugin
Bar Chart
Accessible Charts using Canvas and jQuery
边框,圆角,背景
jQuery Corner
jQuery Curvy Corner
Nifty jQuery Corner
Transparent Corners
jQuery Corner Gallery
Gradient Plugin
文本和链接
jQuery Spoiler plugin
Text Highlighting
Disable Text Select Plugin
jQuery Newsticker
Auto line-height Plugin
Textgrad - a text gradient plugin
LinkLook - a link thumbnail preview
pager jQuery Plugin
shortKeys jQuery Plugin
jQuery Biggerlink
jQuery Ajax Link Checker
Chili jQuery code highlighter plugin
jScroller
鼠标提示
jQuery Plugin - Tooltip
jTip - The jQuery Tool Tip
clueTip
BetterTip
Flash Tooltips using jQuery
ToolTip
菜单,导航栏
jQuery Tabs Plugin - awesome! [demo nested tabs ]
another jQuery nested Tab Set example (based on jQuery Tabs Plugin)
jQuery idTabs
jdMenu - Hierarchical Menu Plugin for jQuery
jQuery SuckerFish Style
jQuery Plugin Treeview
treeView Basic
FastFind Menu
Sliding Menu
Lava Lamp jQuery Menu
jQuery iconDock
jVariations Control Panel
ContextMenu plugin
clickMenu
CSS Dock Menu
jQuery Pop-up Menu Tutorial
Sliding Menu
手风琴,幻灯片等效果
jQuery Plugin Accordion
jQuery Accordion Plugin Horizontal Way
haccordion - a simple horizontal accordion plugin for jQuery
Horizontal Accordion by portalzine.de
HoverAccordion
Accordion Example from fmarcia.info
jQuery Accordion Example
jQuery Demo - Expandable Sidebar Menu
Sliding Panels for jQuery
jQuery ToggleElements
Coda Slider
jCarousel
Accesible News Slider Plugin
Showing and Hiding code Examples
jQuery Easing Plugin
jQuery Portlets
AutoScroll
Innerfade
CodaSlider
拖拽
UI/Draggables
EasyDrag jQuery Plugin
jQuery Portlets
jqDnR - drag, drop resize
Drag Demos
XML XSL JSON Feeds
XSLT Plugin
jQuery Ajax call and result XML parsing
xmlObjectifier - Converts XML DOM to JSON
jQuery XSL Transform
jQuery Taconite - multiple Dom updates
RSS/ATOM Feed Parser Plugin
jQuery Google Feed Plugin
浏览器
Wresize - IE Resize event Fix Plugin
jQuery ifixpng
jQuery pngFix
Link Scrubber - removes the dotted line onfocus from links
jQuery Perciformes - the entire suckerfish familly under one roof
Background Iframe
QinIE - for proper display of Q tags in IE
jQuery Accessibility Plugin
jQuery MouseWheel Plugin
对话框,确认窗口
jQuery Impromptu
jQuery Confirm Plugin
jqModal
SimpleModal
CSS
jQuery Style Switcher
JSS - Javascript StyleSheets
jQuery Rule - creation/manipulation of CSS Rules
jPrintArea
DOM, Ajax 和其他插件
FlyDOM
jQuery Dimenion Plugin
jQuery Loggin
Metadata - extract metadata from classes, attributes, elements
Super-tiny Client-Side Include Javascript jQuery Plugin
Undo Made Easy with Ajax
JHeartbeat - periodically poll the server
Lazy Load Plugin
Live Query
jQuery Timers
jQuery Share it - display social bookmarking icons
jQuery serverCookieJar
jQuery autoSave
jQuery Puffer
jQuery iFrame Plugin
Cookie Plugin for jQuery
jQuery Spy - awesome plugin
Effect Delay Trick
jQuick - a quick tag creator for jQuery
Metaobjects
elementReady
starterkit.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>jQuery Starterkit</title>
<link rel="stylesheet" type="text/css" media="screen" href="style.css" />
<script type="text/javascript" src="http://www.css88.com/jquery-1.3.2/jquery-1.3.2.min.js"></script>
<script src="jquery.js" type="text/javascript"></script>
<script src="custom.js" type="text/javascript"></script>
<script src="jquery.tablesorter.js" type="text/javascript"></script>
<script src="jquery-latest.js" type="text/javascript"></script>
<script src="jquery.tablesorter.min.js" type="text/javascript"></script>
<script src="jquery.metadata.js" type="text/javascript"></script>
</head>
<body>
<!-- tablecode taken from tablesorter examples -->
<table class="tablesorter " cellspacing=1>
<thead>
<tr>
<th class="{sorter:'text'}">Email</th>
<th>Id</th>
<th>Phone</th>
<th>Total</th>
<th>Ip</th>
<th class="{sorter:'procent'}">Url</th>
<th>Time</th>
<th>ISO Date</th>
<th>UK Date</th>
</tr>
</thead>
<tbody>
<tr>
</tr>
<tr>
<td>henry@mountdev.net</td>
<td>35889</td>
<td>941-964-9543</td>
<td>$2776.09</td>
<td>119.232.182.142</td>
<td>http://www.gmail.com</td>
<td>3:54</td>
<td>1974/1/19</td>
<td>19/1/1974</td>
</tr>
<tr>
<td>christian@reno.gov</td>
<td>60021</td>
<td>941-964-5617</td>
<td>$2743.41</td>
<td>167.209.64.181</td>
<td>http://www.dotnet.ca</td>
<td>10:58</td>
<td>2000/3/25</td>
<td>25/3/2000</td>
</tr>
<tr>
<td>muffins@donuts.com</td>
<td>17927</td>
<td>941-964-9511</td>
<td>$2998.18</td>
<td>210.214.231.182</td>
<td>http://google.se</td>
<td>21:22</td>
<td>1993/1/24</td>
<td>24/1/1993</td>
</tr>
<tr>
<td>muffins@reno.gov</td>
<td>76375</td>
<td>941-964-2757</td>
<td>$1836.09</td>
<td>220.222.93.171</td>
<td>http://www.samba.org</td>
<td>15:22</td>
<td>1988/4/4</td>
<td>4/4/1988</td>
</tr>
<tr>
<td>found@flexomat.com</td>
<td>20953</td>
<td>941-964-5544</td>
<td>$575.42</td>
<td>247.246.235.138</td>
<td>http://gmail.com</td>
<td>3:12</td>
<td>1978/6/10</td>
<td>10/6/1978</td>
</tr>
<tr>
<td>foo@reno.gov</td>
<td>03603</td>
<td>941-964-8942</td>
<td>$1677.07</td>
<td>176.96.162.192</td>
<td>http://www.flexomat.com</td>
<td>19:11</td>
<td>1980/7/31</td>
<td>31/7/1980</td>
</tr>
<tr>
<td>found@flexomat.com</td>
<td>84939</td>
<td>941-964-7004</td>
<td>$3916.83</td>
<td>240.145.198.78</td>
<td>http://www.fish.org</td>
<td>1:29</td>
<td>1980/8/26</td>
<td>26/8/1980</td>
</tr>
<tr>
<td>muffins@reno.gov</td>
<td>95926</td>
<td>941-964-147</td>
<td>$3897.91</td>
<td>105.85.121.209</td>
<td>http://www.flexomat.com</td>
<td>9:50</td>
<td>2003/4/1</td>
<td>1/4/2003</td>
</tr>
</tbody>
</table>
</body>
</html>
custom.js
jQuery(document).ready(function() {
$.tablesorter.defaults.widgets=['zebra'];
$.tablesorter.defaults.sortList=[[0,0]];
$("table").tablesorter();
});
starterkit.html
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>jQuery Starterkit</title>
<link rel="stylesheet" type="text/css" media="screen" href="screen.css" />
<script type="text/javascript" src="http://www.css88.com/jquery-1.3.2/jquery-1.3.2.min.js"></script>
<script src="jquery.js" type="text/javascript"></script>
<script src="custom.js" type="text/javascript"></script>
<script src="jquery.tablesorter.js" type="text/javascript"></script>
<script src="jquery-latest.js" type="text/javascript"></script>
<script src="jquery.tablesorter.min.js" type="text/javascript"></script>
<script src="jquery.metadata.js" type="text/javascript"></script>
</head>
<body>
<table id="large" cellspacing="0">
<thead>
<tr>
<th>Email</th>
<th>Id</th>
<th>Phone</th>
<th>Total</th>
<th>Ip</th>
<th>Url</th>
<th>Time</th>
<th>ISO Date</th>
<th>UK Date</th>
</tr>
</thead>
<tbody>
<tr>
<td>devo@flexomat.com</td>
<td>66672</td>
<td>941-964-8535</td>
<td>$2482.79</td>
<td>172.78.200.124</td>
<td>http://gmail.com</td>
<td>15:10</td>
<td>1988/12/14</td>
<td>14/12/1988</td>
</tr>
<tr>
<td>henry@mountdev.net</td>
<td>35889</td>
<td>941-964-9543</td>
<td>$2776.09</td>
<td>119.232.182.142</td>
<td>http://www.gmail.com</td>
<td>3:54</td>
<td>1974/1/19</td>
<td>19/1/1974</td>
</tr>
<tr>
<td>christian@reno.gov</td>
<td>60021</td>
<td>941-964-5617</td>
<td>$2743.41</td>
<td>167.209.64.181</td>
<td>http://www.dotnet.ca</td>
<td>10:58</td>
<td>2000/3/25</td>
<td>25/3/2000</td>
</tr>
<tr>
<td>muffins@donuts.com</td>
<td>17927</td>
<td>941-964-9511</td>
<td>$2998.18</td>
<td>210.214.231.182</td>
<td>http://google.se</td>
<td>21:22</td>
<td>1993/1/24</td>
<td>24/1/1993</td>
</tr>
<tr>
<td>muffins@reno.gov</td>
<td>76375</td>
<td>941-964-2757</td>
<td>$1836.09</td>
<td>220.222.93.171</td>
<td>http://www.samba.org</td>
<td>15:22</td>
<td>1988/4/4</td>
<td>4/4/1988</td>
</tr>
<tr>
<td>mendez@gmail.com</td>
<td>45834</td>
<td>941-964-2575</td>
<td>$2805.46</td>
<td>228.170.245.253</td>
<td>http://flexomat.com</td>
<td>11:31</td>
<td>1975/12/12</td>
<td>12/12/1975</td>
</tr>
<tr>
<td>dev@gmail.com</td>
<td>20022</td>
<td>941-964-4967</td>
<td>$3296.54</td>
<td>175.248.70.240</td>
<td>http://www.flexomat.com</td>
<td>4:27</td>
<td>2002/7/3</td>
<td>3/7/2002</td>
</tr>
<tr>
<td>foo@polyester.se</td>
<td>55977</td>
<td>941-964-745</td>
<td>$2953.73</td>
<td>222.114.227.156</td>
<td>http://www.donuts.com</td>
<td>23:49</td>
<td>1977/8/4</td>
<td>4/8/1977</td>
</tr>
<tr>
<td>adam@aftonbladet.se</td>
<td>38867</td>
<td>941-964-6302</td>
<td>$1949.27</td>
<td>116.241.143.196</td>
<td>http://flexomat.com</td>
<td>23:35</td>
<td>1995/7/27</td>
<td>27/7/1995</td>
</tr>
<tr>
<td>devo@donuts.com</td>
<td>51426</td>
<td>941-964-1234</td>
<td>$1067.00</td>
<td>88.96.149.82</td>
<td>http://www.polyester.se</td>
<td>15:17</td>
<td>1986/1/5</td>
<td>5/1/1986</td>
</tr>
<tr>
<td>henry@samba.org</td>
<td>40859</td>
<td>941-964-4856</td>
<td>$3401.19</td>
<td>68.152.250.74</td>
<td>http://www.flexomat.com</td>
<td>4:36</td>
<td>1990/3/7</td>
<td>7/3/1990</td>
</tr>
<tr>
<td>found@dotnet.ca</td>
<td>23986</td>
<td>941-964-2686</td>
<td>$1393.52</td>
<td>98.102.181.138</td>
<td>http://lostnfound.org</td>
<td>5:51</td>
<td>1993/7/22</td>
<td>22/7/1993</td>
</tr>
<tr>
<td>carl@fish.org</td>
<td>73392</td>
<td>941-964-5792</td>
<td>$3876.04</td>
<td>246.234.182.243</td>
<td>http://www.google.se</td>
<td>6:52</td>
<td>1984/7/14</td>
<td>14/7/1984</td>
</tr>
<tr>
<td>found@mountdev.net</td>
<td>03519</td>
<td>941-964-1599</td>
<td>$1176.48</td>
<td>104.212.122.177</td>
<td>http://donuts.com</td>
<td>18:52</td>
<td>2000/8/6</td>
<td>6/8/2000</td>
</tr>
<tr>
<td>lost@fish.org</td>
<td>36628</td>
<td>941-964-5975</td>
<td>$822.23</td>
<td>153.63.68.208</td>
<td>http://www.gmail.com</td>
<td>7:53</td>
<td>1994/7/14</td>
<td>14/7/1994</td>
</tr>
<tr>
<td>mendez@dotnet.ca</td>
<td>90442</td>
<td>941-964-1649</td>
<td>$1975.72</td>
<td>128.161.95.170</td>
<td>http://reno.gov</td>
<td>8:35</td>
<td>1997/10/20</td>
<td>20/10/1997</td>
</tr>
<tr>
<td>carl@donuts.com</td>
<td>00412</td>
<td>941-964-6432</td>
<td>$1834.77</td>
<td>141.231.126.192</td>
<td>http://gmail.com</td>
<td>16:15</td>
<td>1997/8/23</td>
<td>23/8/1997</td>
</tr>
<tr>
<td>foo@fish.org</td>
<td>80653</td>
<td>941-964-1022</td>
<td>$260.26</td>
<td>98.102.97.81</td>
<td>http://samba.org</td>
<td>8:27</td>
<td>1991/11/24</td>
<td>24/11/1991</td>
</tr>
<tr>
<td>found@fish.org</td>
<td>54635</td>
<td>941-964-6439</td>
<td>$1442.80</td>
<td>108.133.231.154</td>
<td>http://dotnet.ca</td>
<td>6:47</td>
<td>1977/7/12</td>
<td>12/7/1977</td>
</tr>
<tr>
<td>found@flexomat.com</td>
<td>20953</td>
<td>941-964-5544</td>
<td>$575.42</td>
<td>247.246.235.138</td>
<td>http://gmail.com</td>
<td>3:12</td>
<td>1978/6/10</td>
<td>10/6/1978</td>
</tr>
<tr>
<td>lost@aftonbladet.se</td>
<td>60810</td>
<td>941-964-8406</td>
<td>$1054.39</td>
<td>130.80.125.154</td>
<td>http://www.samba.org</td>
<td>15:50</td>
<td>1978/5/18</td>
<td>18/5/1978</td>
</tr>
<tr>
<td>carl@reno.gov</td>
<td>22666</td>
<td>941-964-104</td>
<td>$373.59</td>
<td>185.150.127.115</td>
<td>http://www.lostnfound.org</td>
<td>10:11</td>
<td>1971/11/5</td>
<td>5/11/1971</td>
</tr>
<tr>
<td>carl@donuts.com</td>
<td>82867</td>
<td>941-964-1031</td>
<td>$631.03</td>
<td>62.244.222.152</td>
<td>http://www.reno.gov</td>
<td>11:24</td>
<td>1985/1/22</td>
<td>22/1/1985</td>
</tr>
<tr>
<td>foo@donuts.com</td>
<td>11268</td>
<td>941-964-208</td>
<td>$3730.64</td>
<td>234.192.138.252</td>
<td>http://aftonbladet.se</td>
<td>22:45</td>
<td>1979/8/4</td>
<td>4/8/1979</td>
</tr>
<tr>
<td>devo@google.se</td>
<td>17059</td>
<td>941-964-2903</td>
<td>$1404.67</td>
<td>73.189.246.202</td>
<td>http://www.gmail.com</td>
<td>22:28</td>
<td>1983/4/28</td>
<td>28/4/1983</td>
</tr>
<tr>
<td>muffins@flexomat.com</td>
<td>90584</td>
<td>941-964-4640</td>
<td>$3706.01</td>
<td>243.162.73.115</td>
<td>http://www.fish.org</td>
<td>11:12</td>
<td>1997/4/8</td>
<td>8/4/1997</td>
</tr>
<tr>
<td>foo@samba.org</td>
<td>82966</td>
<td>941-964-7245</td>
<td>$1001.38</td>
<td>255.182.148.252</td>
<td>http://www.reno.gov</td>
<td>8:42</td>
<td>1995/11/1</td>
<td>1/11/1995</td>
</tr>
<tr>
<td>foo@reno.gov</td>
<td>03603</td>
<td>941-964-8942</td>
<td>$1677.07</td>
<td>176.96.162.192</td>
<td>http://www.flexomat.com</td>
<td>19:11</td>
<td>1980/7/31</td>
<td>31/7/1980</td>
</tr>
<tr>
<td>found@flexomat.com</td>
<td>84939</td>
<td>941-964-7004</td>
<td>$3916.83</td>
<td>240.145.198.78</td>
<td>http://www.fish.org</td>
<td>1:29</td>
<td>1980/8/26</td>
<td>26/8/1980</td>
</tr>
<tr>
<td>henry@lostnfound.org</td>
<td>76453</td>
<td>941-964-3084</td>
<td>$3875.88</td>
<td>235.225.168.168</td>
<td>http://www.fish.org</td>
<td>15:31</td>
<td>2000/10/22</td>
<td>22/10/2000</td>
</tr>
<tr>
<td>sam@aftonbladet.se</td>
<td>36274</td>
<td>941-964-7784</td>
<td>$73.43</td>
<td>114.190.194.148</td>
<td>http://www.dn.se</td>
<td>18:24</td>
<td>1994/11/11</td>
<td>11/11/1994</td>
</tr>
<tr>
<td>dev@polyester.se</td>
<td>15971</td>
<td>941-964-7527</td>
<td>$1855.18</td>
<td>179.121.143.189</td>
<td>http://www.flexomat.com</td>
<td>10:25</td>
<td>1984/8/16</td>
<td>16/8/1984</td>
</tr>
<tr>
<td>devo@fish.org</td>
<td>31734</td>
<td>941-964-9760</td>
<td>$3201.35</td>
<td>213.186.220.205</td>
<td>http://www.samba.org</td>
<td>19:37</td>
<td>1979/3/4</td>
<td>4/3/1979</td>
</tr>
<tr>
<td>devo@reno.gov</td>
<td>52074</td>
<td>941-964-2068</td>
<td>$3035.24</td>
<td>189.62.225.163</td>
<td>http://www.dn.se</td>
<td>21:18</td>
<td>1998/2/24</td>
<td>24/2/1998</td>
</tr>
<tr>
<td>mendez@fish.org</td>
<td>87800</td>
<td>941-964-7933</td>
<td>$618.59</td>
<td>254.119.153.91</td>
<td>http://www.dn.se</td>
<td>22:34</td>
<td>1979/5/28</td>
<td>28/5/1979</td>
</tr>
<tr>
<td>muffins@reno.gov</td>
<td>95926</td>
<td>941-964-147</td>
<td>$3897.91</td>
<td>105.85.121.209</td>
<td>http://www.flexomat.com</td>
<td>9:50</td>
<td>2003/4/1</td>
<td>1/4/2003</td>
</tr>
<tr>
<td>carl@donuts.com</td>
<td>74513</td>
<td>941-964-4507</td>
<td>$2150.68</td>
<td>106.175.123.125</td>
<td>http://www.fish.org</td>
<td>21:42</td>
<td>1977/4/8</td>
<td>8/4/1977</td>
</tr>
<tr>
<td>sam@fish.org</td>
<td>82420</td>
<td>941-964-2134</td>
<td>$3947.54</td>
<td>129.78.102.122</td>
<td>http://mountdev.net</td>
<td>11:33</td>
<td>1990/11/26</td>
<td>26/11/1990</td>
</tr>
<tr>
<td>devo@mountdev.net</td>
<td>81905</td>
<td>941-964-3535</td>
<td>$722.17</td>
<td>229.101.84.111</td>
<td>http://fish.org</td>
<td>7:29</td>
<td>1975/12/28</td>
<td>28/12/1975</td>
</tr>
<tr>
<td>john@lostnfound.org</td>
<td>93124</td>
<td>941-964-1508</td>
<td>$394.82</td>
<td>61.184.235.77</td>
<td>http://www.mountdev.net</td>
<td>14:38</td>
<td>1979/12/10</td>
<td>10/12/1979</td>
</tr>
</tbody>
</table>
</body>
custom.js
jQuery(document).ready(function() {
$("#large").tablesorter( {sortList: [[0,0], [1,0]]} );
});
1.在当前类路径下新建一个properties文件;
编写DBconfig.properties
driver=com.microsoft.jdbc.sqlserver.SQLServerDriver
url=jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=school
user=sa
password=sa
2.新建工具类
package com.itcast.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
public class ConnectionUtil {
public Connection openConnection() {
String driver = "";
String url = "";
String user = "";
String password = "";
Properties prop = new Properties();
Connection conn = null;
try {
//加载属性文件
prop.load(this.getClass().getClassLoader().getResourceAsStream(
"DBConfig.properties"));
driver = prop.getProperty("driver");
url = prop.getProperty("url");
user = prop.getProperty("user");
password = prop.getProperty("password");
//Class.forName加载驱动
Class.forName(driver);
//DriverManager获得连接
conn = DriverManager.getConnection(url, user, password);
return conn;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
3.业务实现类
package com.itcast.service.impl;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.itcast.db.ConnectionUtil;
import com.itcast.db.DBConnection;
import com.itcast.model.User;
import com.itcast.service.IUser;
/**
* 业务逻辑处理
*
* @author Administrator
*
*/
public class UserImpl implements IUser {
// DBConnection db = new DBConnection();
private PreparedStatement pstm;
/**
* 添加用户
*/
public void add_User(User user) {
try{
// Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
// conn= DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=school", "sa", "sa");
Connection conn = new ConnectionUtil().openConnection();
pstm=conn.prepareStatement("insert into userTable(user_name , user_password ,user_age,user_sex,user_address,user_telephone) values(?,?,?,?,?,?)");
pstm.setString(1, user.getUsername());
pstm.setString(2, user.getPassword());
pstm.setInt(3, user.getAge());
pstm.setString(4, user.getSex());
pstm.setString(5, user.getAddress());
pstm.setString(6, user.getTelephone());
pstm.executeUpdate();
}catch(SQLException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
// String sql="insert into userTable(user_name , user_password ,user_age,user_sex,user_address,user_telephone) values(?,?,?,?,?,?)";
//
// db.GetConnection();
}
/**
* 删除用户
*/
public boolean del_User(int id) {
boolean result=false;
try{
Connection conn = new ConnectionUtil().openConnection();
pstm=conn.prepareStatement("delete from userTable where user_id="+id);
pstm.executeUpdate();
result=true;
}catch(SQLException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
return result;
}
/**
* 查找用户
*/
public List<User> find_User() {
Connection conn = new ConnectionUtil().openConnection();
List<User> list = new ArrayList();
User user = new User();
String sql = "select * from userTable ";
try {
pstm=conn.prepareStatement(sql);
// pstm.setInt(1, id);
ResultSet rs=pstm.executeQuery();
while (rs.next()) {
user.setId(rs.getInt(1));
user.setUsername(rs.getString(2));
user.setPassword(rs.getString(3));
user.setAge(rs.getInt(4));
user.setSex(rs.getString(5));
user.setAddress(rs.getString(6));
user.setTelephone(rs.getString(7));
user.setTime(rs.getDate(8));
// System.out.println(rs.getString(7));
list.add(user);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
/**
* 按照id查询用户信息
*/
public List find_Users(int id) {
Connection conn = new ConnectionUtil().openConnection();
List list = new ArrayList();
User user = new User();
String sql = "select * from userTable where user_id=?";
try {
pstm=conn.prepareStatement(sql);
pstm.setInt(1, id);
ResultSet rs=pstm.executeQuery();
while (rs.next()) {
user.setId(rs.getInt(1));
user.setUsername(rs.getString(2));
user.setPassword(rs.getString(3));
user.setAge(rs.getInt(4));
user.setSex(rs.getString(5));
user.setAddress(rs.getString(6));
user.setTelephone(rs.getString(7));
user.setTime(rs.getDate(8));
// System.out.println(rs.getString(7));
list.add(user);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
/**
* 修改用户信息
*/
public IUser modify_User() {
// TODO Auto-generated method stub
return null;
}
}
摘要: JSTL 入门: 探讨 core
JSTL 入门: 探讨 core
使用定制标记进行流控制和 URL 管理
...
阅读全文
由于采用sql server2000作为数据库。用java进行jdbc连接时
出现Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
发现原来是少了sp4补丁。
先下载补丁文件
SQL2000-KB884525-SP4-x86-CHS.EXE
之后安装该文件,默认安装在c:/下
进入该文件夹,点击set up.bat 进行更新。
最后重新开启服务。就ok啦!
以前用myeclipse连接sql server 2005时,直接用的一个jdbcdriver.jar就ok了。但是目前为了需要
必须用sql server 2000,所以我才在网上找了找连接驱动。
SQLServer2000的JDBC驱动
转自 http://blog.csdn.net/javalf00/archive/2009/06/09/4254478.aspx
一、下载SQL SERVER2000的jdbc驱动程序。在微软站点就有这个驱动程序: Window操作系统
http://download.microsoft.com/download/3/0/f/30ff65d3-a84b-4b8a-a570-27366b2271d8/setup.exe Unix操作系统
Mssqlserver.tar http://download.microsoft.com/download/3/0/f/30ff65d3-a84b-4b8a-a570-27366b2271d8/mssqlserver.tar
二、安装JDBC 执行ms_jdbc_setup.exe可执行文件,一切只需要点击下一
步,至到出现finish按钮,完成安装。注: (1) ms_JDBC_setup默认安装路
径为:c:\Program Files\Microsoft SQL Server 2000 Driver for JDBC (2)此版
(Version 2.2.0022)本仅支持Microsoft SQL Server 2000 Driver for JDBC (3)安
装目录\lib\下的三个jar文件即是我们要的JDBC驱动核心 msbase.jar
mssqlserver.jar msutil.jar
三、将以上(3)中指的三个jar文件加入到环境变量中去 classpath:
d:\webserver\lib\msbase.jar;d:\webserver\lib\mssqlserver.jar;d:\webserver\lib\msutil.jar
注:也可以省事的将以上三个文件拷贝至你的jvm机所在的
JAVA_HOME\jre\lib\ext目录下,不过不建议这么做。 对于web项目,可
直接将以上三个jar包拷贝到WEB-INF下的lib文件夹下即可。
四、测试 jsp文件:
您的第一个字段内容为:
您的第二个字段内容为:
五、注意事项 1. 要保证你的Sql服务是启动状态 2. 在Sql 的管理器中能够
使用sa或其它用户正常登录;(有些人在安装sql时用了nt用户管理模式,
这里可能会有一定问题) 3. 第一次配置好环境变量最好能重启一下电脑
4. 注意JVM和DataBase的启动顺序:先启动DataBase,再启动JVM机。
注:在停止Web服务器后,再重启动,中间最好能有10秒以上的间隔。
5. 注意操作系统的网络连通性 A.启动了Tcp/IP服务 B.配置了相关IP地址,
有些人机器可能用的是自动分配IP或有配置IP时,但网络不通可能也找不
到IP 6. 有些机器在调试明最好能将(四)中的连接地址localhost改成数据
库服务器的IP,这也是要注意5的原因。
package zmsjdbc;
import java.sql.*;
public class sqlserver1 {
private String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=catv"; //catv是数据库!loacalhost可以用IP和主机
private String user="sa";
private String pwd="";
public Connection getcon() {
Connection con;
try { //加载驱动程序
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); //创建连接 con=DriverManager.getConnection(url,user,pwd);
return con;
} catch(ClassNotFoundException e) {
System.out.println("加载驱动程序出错");
} catch(SQLException e) {
System.out.println(e.getMessage());
} catch(Exception e) {
System.out.println("出现了好大的错误");
}
return null;
}
public ResultSet executeSql(String sqlstr) {
Connection conn; Statement stmt;
try { conn=getcon();
stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(sqlstr);
return rs;
} catch(SQLException e) {
System.out.print("获取数据集时出现错误");
}
return null;
}
public static void main(String[] args) {
ResultSet rs1;
sqlserver1 sql1=new sqlserver1();
rs1=sql1.executeSql("select ui_id,ui_name from userinfo");
try {
while(rs1.next()) {
System.out.println(rs1.getString("ui_id")+" 姓名:"+rs1.getString("ui_name"));
}
} catch(Exception e)
{
System.out.println(e.getMessage());
}
} } /*
1、对于SQLSERVER 必须下载 SQLSERVER驱动 http://download.microsoft.com/download/3/0/f/30ff65d3-a84b-4b8a-a570-27366b2271d8/setup.exe 然后把 msbase.jar mssqlserver.jar msutil.jar 复制到 D:\JBuilder9\jdk1.4\jre\lib\ext,或者放在web项目的WEB-INF/lib下。
2:如果出现错误: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket. 说明 SQLERVER 端口号 不对 》》》更改端口号:SQLSERVER->属性-常规-网络配置-常规-TCP/IP-属性--更改端口号有个命令可以用: cmd-> netstat -a 例如:String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=zmstv"; 说明你使用的是1433 在CMD 下 输入 netstat -a -n 应该看不到:TCP 127.0.0.1:1433 0.0.0.0:0 LISTENING 如果安装 sqlserver 2000 出现 报错 "以前的某个程序安装已在安装计算机上创建挂起的文件操作。。。。" 打开注册表编辑器,在 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager中找到PendingFileRenameOperations项目,并删除它。这样就可以清除安装暂挂项目。
在J2ME程序开发过程中,为了一定的需要,经常需要来获得用户的手机号码,但是这个功能却在标准的J2ME类库中没有提供。
在使用中国移动的CMWAP方式连接网络时,中国移动会将用户的手机号码放在一个名称为x-up-calling-line-id的头信息中,可以通过读取该头信息,获得用户的手机号码,具体代码如下:
String usermphone = http.getHeader("x-up-calling-line-id");
伪手机号,用户在移动梦网的ID:
String usermphone = http.getHeader("MISC_MID");
其中http是HttpConnction类型的对象。
[color=Green]注意:必须要是CNWAP联网才行。 [/color]
原来获取用户手机号码很简单,关键是通信营运商给不给你送。如果给你送的话,手机号码是在Http请求头里获取,通常在x-up-calling-line-id项里,如果通过此项得不到的话。可采用循环读出所有的Http头里的信息,号码在哪一个头里就一目了然了。
String userPhone = request.getHeader("x-up-calling-line-id");一般通过这句就能获取到手机号码。
如果号码不在上面这条请求头里,采用下面方法读取看号码在哪一条请求头里。
Enumeration headers = request.getHeaderNames() ;
while(headers.hasMoreElements())
{
String head = (String)headers.nextElement();
out.println(head+":"+request.getHeader(head));
}
这样就可以将所有的信息打印出来,看到号码的位置了。注意获取号码的前提是跟营运商有合作或者跟他们申请。同意给你送,否则程序实现了也没有用。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/jhjinhua/archive/2008/12/12/3505675.aspx
转载:http://www.host01.com/article/Wap/wap001/05922162843330.htm
JSP下获取手机号码
//取手机号
String mobile = "" ;
String MO="";
String temvit = "";
String version = "";
Enumeration headerNames = request.getHeaderNames();
while(headerNames.hasMoreElements()) {
String headerName = (String)headerNames.nextElement();
if (headerName.equals("x-up-calling-line-id")) {
temvit=request.getHeader(headerName);
if (temvit.substring(0,3).trim().equals("861")) {
mobile=temvit.substring(2,13);
}
if (temvit.substring(0,2).trim().equals("13")) {
mobile=temvit;
}
}
if (headerName.equals("user-agent")) {
<--ubbcodetab--> <--/ubbcodetab-->MO=request.getHeader(headerName);
}
if (headerName.equals("x-up-calling-line-id")) {
temvit=request.getHeader(headerName);
if (temvit.substring(0,2).trim().equals("13")) {
mobile=temvit;
}
}
}
转载:
http://hi.baidu.com/yashengwh/blog/item/27130edf7ec447156327980b.html
手机号码:Request.ServerVariables("HTTP_X_UP_CALLING_LINE_ID")
手机型号:request.ServerVariables("HTTP_User-Agent")
补充:
手机号码,要看当地的运营商了
有三种方法获得(联通的)
1.加密的手机号码:被加密的手机号码,与手机号码一一对应。
中国联通WAP平台向CP Server(主域或IP地址)传送加密手机号码,CP Server获取该加密手机号码的方法为:在每次用户发送的请求http header中取“deviceid”。
2.公开的手机号码:中国联通WAP平台向CP Server(主域或IP地址)传送公开的手机号码,CP Server获取该公开手机号码的方法为:在每次用户发送的请求http header中取“x-up-calling-line-id”。
以上要和联通进行申请
3、你可以试这样的方法获得手机号码: Mobile = request.ServerVariables("HTTP_X_UP_subno")
Mobile =mid(FromMobile,3,11) ??
asp?lp=27&id=1782582>http://www.blueidea.com/bbs/NewsDetail.asp?lp=27&id=1782582
声明:第三种方法不保险
头文件参考:
答7:
POST /default.asp HTTP/1.0
Host: 211.94.121.3:81
content-type: text/plain
accept-language: zh
accept-charset: ISO-8859-1, UTF-8; Q=0.8, ISO-10646-UCS-2; Q=0.6
profile: http://nds.nokia.com/uaprof/N7210r100.xml
user-agent: Nokia7210/1.0 (3.09) Profile/MIDP-1.0 Configuration/CLDC-1.0
x-wap.tod-coded: Thu, 01 Jan 1970 00:00:00 GMT
accept: */*
content-length: 1
Cookie: ASPSESSIONIDGGGQGAPU=KFHHMHPCHJFPKPEPBEDFHCJL
via: WTP/1.1 wapgw2 (Nokia WAP Gateway 3.1/CD1/3.1.43), HTTP/1.1 httpproxy2[0A0000C3] (Traffic-Server/4.0.9 [uSc ])
X-Network-info: GPRS,10.15.96.127,13810027XXX,211.139.172.70,unsecured
X-Forwarded-For: 10.15.96.127
X-Up-Calling-Line-ID: 13810027XXX
X-Source-ID: 211.139.172.70
X-Nokia-CONNECTION_MODE: CLESS
X-Nokia-BEARER: GPRS
X-Nokia-gateway-id: NAWG/3.1/Build43
Client-ip: 192.168.0.6
Connection: keep-alive
package cn.itcast.bean;
public class Person {
private Integer id;
private String name;
public Person(){
}
public Person(String name) {
this.name=name;
}
getter&&setter方法
}
Person.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.itcast.bean">
<class name="Person" table="person">
<id name="id" type="integer">
<generator class="native"></generator>
</id>
<property name="name" length="10" not-null="true">
</property>
</class>
</hibernate-mapping>
定义业务接口
package cn.itcast.service;
import java.util.List;
import cn.itcast.bean.Person;
public interface IPersonService {
/**
* 保存人员信息
* @param person
*/
public abstract void save(Person person);
/**
* 更新信息
* @param person
*/
public abstract void update(Person person);
/**
* 获取人员
* @param personId
* @return
*/
public abstract Person getPerson(Integer personId);
/**
* 删除人员信息
* @param personId
*/
public abstract void delete(Integer personId);
/**
* 获取人员列表
* @return
*/
public abstract List<Person> getPersons();
}
业务实现类
package cn.itcast.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import cn.itcast.bean.Person;
import cn.itcast.service.IPersonService;
/**
* 业务层,采用注解声明事务
*
* @author Administrator
*
*/
@Transactional
public class PersonServiceBean implements IPersonService {
@Resource
private SessionFactory sessionFactory;
public void save(Person person) {
// 从spring 容器中得到正在管理的sessionFactory,persist方法用于保存实体
sessionFactory.getCurrentSession().persist(person);
}
public void update(Person person) {
sessionFactory.getCurrentSession().merge(person);
}
@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
public Person getPerson(Integer personId) {
return (Person) sessionFactory.getCurrentSession().get(Person.class,
personId);
}
public void delete(Integer personId) {
sessionFactory.getCurrentSession()
.delete(
sessionFactory.getCurrentSession().load(Person.class,
personId));
}
@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
@SuppressWarnings("unchecked")
public List<Person> getPersons() {
return sessionFactory.getCurrentSession().createQuery("from Person")
.list();
}
}
hibernate && spring的配置文件
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
- Application context definition for JPetStore's business layer.
- Contains bean references to the transaction manager and to the DAOs in
- dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
<!-- 配置数据源 -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName"
value="org.gjt.mm.mysql.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&characterEncoding=UTF-8" />
<property name="username" value="root" />
<property name="password" value="" />
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="1" />
<!-- 连接池的最大值 -->
<property name="maxActive" value="500" />
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="2" />
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="1" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" /><!-- 将datasource注入到sessionFactory -->
<property name="mappingResources">
<list>
<value>cn/itcast/bean/Person.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.hbm2ddl.auto=update hibernate.show_sql=false
hibernate.format_sql=false
</value>
</property>
</bean>
<!-- 通过事务管理 管理sessionFactory -->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />
<bean id="personServiceBean"
class="cn.itcast.service.impl.PersonServiceBean">
</bean>
</beans>
/**
测试类**/
package junit;
import java.util.List;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.itcast.bean.Person;
import cn.itcast.service.IPersonService;
public class IPersonServiceTest {
private static IPersonService ipersonservice;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
try {
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
ipersonservice=(IPersonService)ctx.getBean("personServiceBean");
} catch (RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void TestSave(){
ipersonservice.save(new Person("小张"));
System.out.println("保存成功");
}
@Test public void testGetPerson(){
Person person=ipersonservice.getPerson(1);
System.out.println(person.getName());
}
@Test public void testUpdate(){
Person person=ipersonservice.getPerson(1);
person.setName("小丽");
ipersonservice.update(person);
}
@Test public void testGetPersons(){
List<Person> persons=ipersonservice.getPersons();
for(Person person : persons){
System.out.println(person.getId()+" :" +person.getName());
}
}
@Test public void testDelete(){
ipersonservice.delete(1);
}
}
table :person
id int
name varchar
接口
package cn.itcast.service;
import java.util.List;
import cn.itcast.bean.Person;
public interface IPersonService {
public void save(Person person);
public void update(Person person);
public void delete(int personId);
public Person getPerson(int personId);
public List<Person> getPersons();
}
实现类:
package cn.itcast.service.impl;
import java.util.List;
import javax.annotation.Resource;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.itcast.bean.Person;
import cn.itcast.service.IPersonService;
@Transactional
public class PersonServiceImpl implements IPersonService {
private JdbcTemplate jdbcTemplete;
// private DataSource datasource;
@Resource
public void setDatasource(DataSource datasource) {
this.jdbcTemplete = new JdbcTemplate(datasource);
}
public void delete(int personId) {
this.jdbcTemplete
.update("delete from person where id=?",
new Object[] { personId },
new int[] { java.sql.Types.INTEGER });
}
public Person getPerson(int personId) {
return (Person) this.jdbcTemplete.queryForObject(
"select * from person where id=?", new Object[] { personId },
new int[] { java.sql.Types.INTEGER }, new PersonRowMapper());
}
@SuppressWarnings("unchecked")
public List<Person> getPersons() {
return (List<Person>) this.jdbcTemplete.query("select * from person",
new PersonRowMapper());
}
public void save(Person person) {
System.out.println(person.getName());
this.jdbcTemplete.update("insert into person(name) values(?)",
new Object[] { person.getName() },
new int[] { java.sql.Types.VARCHAR });
}
public void update(Person person) {
this.jdbcTemplete.update("update person set name=? where id=?",
new Object[] { person.getName(), person.getId() }, new int[] {
java.sql.Types.VARCHAR, java.sql.Types.INTEGER });
}
}
实体类:
package cn.itcast.bean;
public class Person {
private int id;
private String name;
public Person() {
}
public Person(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package cn.itcast.service.impl;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
import cn.itcast.bean.Person;
public class PersonRowMapper implements RowMapper {
public Object mapRow(ResultSet rs, int index) throws SQLException {
cn.itcast.bean.Person person=new Person(rs.getString("name"));
person.setId(rs.getInt("id"));
return person;
}
}
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!--
- Application context definition for JPetStore's business layer.
- Contains bean references to the transaction manager and to the DAOs in
- dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<aop:aspectj-autoproxy proxy-target-class="true"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value=""/>
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="1"/>
<!-- 连接池的最大值 -->
<property name="maxActive" value="500"/>
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="2"/>
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="1"/>
</bean>
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />
<bean id="personServiceImpl"
class="cn.itcast.service.impl.PersonServiceImpl">
<property name="datasource" ref="dataSource" />
</bean>
</beans>
测试类:
package junit.test;
import java.util.List;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.itcast.bean.Person;
import cn.itcast.service.IPersonService;
public class TestSpringAndJdbc {
public static IPersonService iPersonService;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
iPersonService=(IPersonService)ctx.getBean("personServiceImpl");
}
@Test
public void TestSave(){
for(int i=1;i<5;i++){
iPersonService.save(new Person("传智博客"+i));
}
}
@Test
public void TestFindPerson(){
iPersonService.getPerson(1);
System.out.println(iPersonService.getPerson(1).getName());
}
@Test
public void TestUpdate(){
Person person=iPersonService.getPerson(1);//通过id取得person对象
person.setName("张三");//设置名字
iPersonService.update(person);//更新
}
@Test
public void TestDelete(){
Person person=iPersonService.getPerson(2);
iPersonService.delete(person.getId());
}
@Test
public void testFindAllPeron(){
List<Person> list=iPersonService.getPersons();
for(Person person:list){
System.out.println(person.getId()+" "+person.getName());
}
}
}