您当前的位置:首页 > 互联网教程

java过滤sql关键字的正则替换掉

发布时间:2025-05-24 10:09:33    发布人:远客网络

java过滤sql关键字的正则替换掉

一、java过滤sql关键字的正则替换掉

1、java过滤sql关键字的正则替换掉方法如下:

2、可以在C#中这样做:Regexregex= newRegex(@"]*>[^");

3、stringcleanedHtml= regex.Replace(html,"");

4、可是我并不想再写个循环去遍历每条记录,然后保存每条记录,我想在数据库中一步到位,而sql只提供了简单的replace函数,这个函数明显不能达到咱的要求,那就去写一个自定义函数吧。

5、函数源代码如下:CREATE functiondbo.regexReplace

6、(@source ntext,--原字符串@regexp varchar(1000),--正则表达式@replace varchar(1000),--替换值@globalReplace bit=1,--是否是全局替换@ignoreCase bit=0--是否忽略大小写)returnS varchar(1000)AS

7、declare@result varchar(5000)exec@hr=sp_OACreate'VBScript.RegExp',@objRegExp OUTPUT

8、exec@hr=sp_OADestroy@objRegExp

9、exec@hr=sp_OASetProperty@objRegExp,'Pattern',@regexp

10、exec@hr=sp_OADestroy@objRegExp

11、exec@hr=sp_OASetProperty@objRegExp,'Global',@globalReplace

12、exec@hr=sp_OADestroy@objRegExp

13、exec@hr=sp_OASetProperty@objRegExp,'IgnoreCase',@ignoreCase

14、exec@hr=sp_OADestroy@objRegExp

15、exec@hr=sp_OAMethod@objRegExp,'Replace',@result OUTPUT,@source,@replace

16、exec@hr=sp_OADestroy@objRegExp

17、exec@hr=sp_OADestroy@objRegExp

18、需要注意的是,即使写好了这个函数,也并不能马上使用。执行这个函数时可能会出现以下的错误:Msg 15281, Level 16, State 1, Line 1

19、SQL Server blocked access to procedure'sys.sp_OACreate' of component'Ole Automation Procedures' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of'Ole Automation Procedures' by using sp_configure. For more information about enabling'Ole Automation Procedures', see"Surface Area Configuration" in SQL Server Books Online.

20、这是因为未开启Ole Automation Procedures选项,MSDN中的Ole Automation Procedures选项。执行下面的语句开启这个选项:sp_configure'show advanced options',1;GO

21、RECONFIGURE;GOsp_configure'Ole Automation Procedures',1;GO

22、所有的准备工作都已经做好,那就试验一下吧。

23、Example1:忽略大小写并替换selectdbo.regexReplace('123456',']*>[^','',1,1)

24、Also Available- Smith& Hogan: Criminal Law Cases& Materials 10th ed

25、There is, as ever, detailed analysis of the many recent case developments, in particular,

26、a revision of the chapter dealing with secondary liability and joint enterprise.

27、调用代码:selectdbo.regexReplace(html,']*>(.|\n)*?','',1,1)

28、Example3:去除html标签selectdbo.regexReplace('

29、Example4:数据库字段值替换updateBooks。

二、Java网络爬虫怎么实现

网络爬虫是一个自动提取网页的程序,它为搜索引擎从万维网上下载网页,是搜索引擎的重要组成。

传统爬虫从一个或若干初始网页的URL开始,获得初始网页上的URL,在抓取网页的过程中,不断从当前页面上抽取新的URL放入队列,直到满足系统的一定停止条件。对于垂直搜索来说,聚焦爬虫,即有针对性地爬取特定主题网页的爬虫,更为适合。

以下是一个使用java实现的简单爬虫核心代码:

public void crawl() throws Throwable{

CrawlerUrl url= getNextUrl();//获取待爬取队列中的下一个URL

String content= getContent(url);//获取URL的文本信息

//聚焦爬虫只爬取与主题内容相关的网页,这里采用正则匹配简单处理

if(isContentRelevant(content, this.regexpSearchPattern)){

saveContent(url, content);//保存网页至本地

//获取网页内容中的链接,并放入待爬取队列中

Collection urlStrings= extractUrls(content, url);

addUrlsToUrlQueue(url, urlStrings);

System.out.println(url+" is not relevant ignoring...");

Thread.sleep(this.delayBetweenUrls);

private CrawlerUrl getNextUrl() throws Throwable{

while((nextUrl== null)&&(!urlQueue.isEmpty())){

CrawlerUrl crawlerUrl= this.urlQueue.remove();

//doWeHavePermissionToVisit:是否有权限访问该URL,友好的爬虫会根据网站提供的"Robot.txt"中配置的规则进行爬取

//isUrlAlreadyVisited:URL是否访问过,大型的搜索引擎往往采用BloomFilter进行排重,这里简单使用HashMap

//isDepthAcceptable:是否达到指定的深度上限。爬虫一般采取广度优先的方式。一些网站会构建爬虫陷阱(自动生成一些无效链接使爬虫陷入死循环),采用深度限制加以避免

if(doWeHavePermissionToVisit(crawlerUrl)

&&(!isUrlAlreadyVisited(crawlerUrl))

&& isDepthAcceptable(crawlerUrl)){

// System.out.println("Next url to be visited is"+ nextUrl);

private String getContent(CrawlerUrl url) throws Throwable{

//HttpClient4.1的调用与之前的方式不同

HttpClient client= new DefaultHttpClient();

HttpGet httpGet= new HttpGet(url.getUrlString());

StringBuffer strBuf= new StringBuffer();

HttpResponse response= client.execute(httpGet);

if(HttpStatus.SC_OK== response.getStatusLine().getStatusCode()){

HttpEntity entity= response.getEntity();

BufferedReader reader= new BufferedReader(

new InputStreamReader(entity.getContent(),"UTF-8"));

if(entity.getContentLength()> 0){

strBuf= new StringBuffer((int) entity.getContentLength());

while((line= reader.readLine())!= null){

public static boolean isContentRelevant(String content,

Matcher m= regexpPattern.matcher(content.toLowerCase());

public List extractUrls(String text, CrawlerUrl crawlerUrl){

extractHttpUrls(urlMap, text);

extractRelativeUrls(urlMap, text, crawlerUrl);

return new ArrayList(urlMap.keySet());

private void extractHttpUrls(Map urlMap, String text){

String[] terms= url.split("a href=\"");

// System.out.println("Term="+ term);

int index= term.indexOf("\"");

term= term.substring(0, index);

System.out.println("Hyperlink:"+ term);

private void extractRelativeUrls(Map urlMap, String text,

Matcher m= relativeRegexp.matcher(text);

URL textURL= crawlerUrl.getURL();

String host= textURL.getHost();

String[] terms= url.split("a href=\"");

int index= term.indexOf("\"");

term= term.substring(0, index);

System.out.println("Relative url:"+ s);

public static void main(String[] args){

Queue urlQueue= new LinkedList();

urlQueue.add(new CrawlerUrl(url, 0));

NaiveCrawler crawler= new NaiveCrawler(urlQueue, 100, 5, 1000L,

// boolean allowCrawl= crawler.areWeAllowedToVisit(url);

// System.out.println("Allowed to crawl:"+ url+""+

System.out.println(t.toString());

三、求Java万年历源代码!!!

1、你可以把他改下我是没时间帮你该哈!!!

2、function dateSelector()//构造dateSelector对象,用来实现一个日历形式的日期输入框。

3、 this.year=myDate.getFullYear();//定义year属性,年份,默认值为当前系统年份。

4、 this.month=myDate.getMonth()+1;//定义month属性,月份,默认值为当前系统月份。

5、 this.date=myDate.getDate();//定义date属性,日,默认值为当前系统的日。

6、 this.inputName='';//定义inputName属性,即输入框的name,默认值为空。注意:在同一页中出现多个日期输入框,不能有重复的name!

7、 this.display=display;//定义display方法,用来显示日期输入框。

8、function display()//定义dateSelector的display方法,它将实现一个日历形式的日期选择框。

9、 var week=new Array('日','一','二','三','四','五','六');

10、 document.write("<style type=text/css>");

11、 document.write(".ds_font td,span{ font: normal 12px宋体; color:#000000;}");

12、 document.write(".ds_border{ border: 1px solid#000000; cursor: hand; background-color:#DDDDDD}");

13、 document.write(".ds_border2{ border: 1px solid#000000; cursor: hand; background-color:#DDDDDD}");

14、 document.write("</style>");

15、 var M=new String(this.month);

16、 if(M.length==1&&d.length==1){

17、 document.write("<input style='text-align:center;' id='DS_"+this.inputName+"' name='"+this.inputName+"' value='"+this.year+"-0"+this.month+"-0"+this.date+"' title=双击可进行编缉 ondblclick='this.readOnly=false;this.focus()' onblur='this.readOnly=true' readonly>");}

18、 else if(M.length==1&&d.length==2){

19、 document.write("<input style='text-align:center;' id='DS_"+this.inputName+"' name='"+this.inputName+"' value='"+this.year+"-0"+this.month+"-"+this.date+"' title=双击可进行编缉 ondblclick='this.readOnly=false;this.focus()' onblur='this.readOnly=true' readonly>");}

20、 else if(M.length==2&&d.length==1){

21、 document.write("<input style='text-align:center;' id='DS_"+this.inputName+"' name='"+this.inputName+"' value='"+this.year+"-"+this.month+"-0"+this.date+"' title=双击可进行编缉 ondblclick='this.readOnly=false;this.focus()' onblur='this.readOnly=true' readonly>");}

22、 else if(M.length==2&&d.length==2){

23、 document.write("<input style='text-align:center;' id='DS_"+this.inputName+"' name='"+this.inputName+"' value='"+this.year+"-"+this.month+"-"+this.date+"' title=双击可进行编缉 ondblclick='this.readOnly=false;this.focus()' onblur='this.readOnly=true' readonly>");}

24、document.write("<button style='width:60px;height:18px;font-size:12px;margin:1px;border:1px solid#A4B3C8;background-color:#DFE7EF;' type=button onclick=this.nextSibling.style.display='block' onfocus=this.blur()>日期</button>");

25、 document.write("<div style='position:absolute;display:none;text-align:center;width:0px;height:0px;overflow:visible' onselectstart='return false;'>");

26、 document.write("<div style='position:absolute;left:-60px;top:20px;width:142px;height:165px;background-color:#F6F6F6;border:1px solid#245B7D;' class=ds_font>");

27、 document.write("<table cellpadding=0 cellspacing=1 width=140 height=20 bgcolor=#CEDAE7 onmousedown='DS_x=event.x-parentNode.style.pixelLeft;DS_y=event.y-parentNode.style.pixelTop;setCapture();' onmouseup='releaseCapture();' onmousemove='dsMove(this.parentNode)' style='cursor:move;'>");

28、 document.write("<tr align=center>");

29、 document.write("<td width=12% onmouseover=this.className='ds_border' onmouseout=this.className='' onclick=subYear(this) title='减小年份'><<</td>");

30、 document.write("<td width=12% onmouseover=this.className='ds_border' onmouseout=this.className='' onclick=subMonth(this) title='减小月份'><</td>");

31、 document.write("<td width=52%><b>"+this.year+"</b><b>年</b><b>"+this.month+"</b><b>月</b></td>");

32、 document.write("<td width=12% onmouseover=this.className='ds_border' onmouseout=this.className='' onclick=addMonth(this) title='增加月份'>></td>");

33、 document.write("<td width=12% onmouseover=this.className='ds_border' onmouseout=this.className='' onclick=addYear(this) title='增加年份'>>></td>");

34、 document.write("</tr>");

35、 document.write("</table>");

36、 document.write("<table cellpadding=0 cellspacing=0 width=140 height=20 onmousedown='DS_x=event.x-parentNode.style.pixelLeft;DS_y=event.y-parentNode.style.pixelTop;setCapture();' onmouseup='releaseCapture();' onmousemove='dsMove(this.parentNode)' style='cursor:move;'>");

37、 document.write("<tr align=center>");

38、 document.write("<td>"+week[i]+"</td>");

39、 document.write("</tr>");

40、 document.write("</table>");

41、 document.write("<table cellpadding=0 cellspacing=2 width=140 bgcolor=#EEEEEE>");

42、 document.write("<tr align=center>");

43、 document.write("<td width=10% height=16 onmouseover=if(this.innerText!=''&&this.className!='ds_border2')this.className='ds_border' onmouseout=if(this.className!='ds_border2')this.className='' onclick=getValue(this,document.all('DS_"+this.inputName+"'))></td>");

44、 document.write("</tr>");

45、 document.write("</table>");

46、 document.write("<span style=cursor:hand onclick=this.parentNode.parentNode.style.display='none'>【关闭】</span>");

47、 document.write("</div>");

48、 document.write("</div>");

49、 dateShow(document.all("DS_"+this.inputName).nextSibling.nextSibling.childNodes[0].childNodes[2],this.year,this.month)

50、function subYear(obj)//减小年份

51、 var myObj=obj.parentNode.parentNode.parentNode.cells[2].childNodes;

52、 myObj[0].innerHTML=eval(myObj[0].innerHTML)-1;

53、 dateShow(obj.parentNode.parentNode.parentNode.nextSibling.nextSibling,eval(myObj[0].innerHTML),eval(myObj[2].innerHTML))

54、function addYear(obj)//增加年份

55、 var myObj=obj.parentNode.parentNode.parentNode.cells[2].childNodes;

56、 myObj[0].innerHTML=eval(myObj[0].innerHTML)+1;

57、 dateShow(obj.parentNode.parentNode.parentNode.nextSibling.nextSibling,eval(myObj[0].innerHTML),eval(myObj[2].innerHTML))

58、function subMonth(obj)//减小月份

59、 var myObj=obj.parentNode.parentNode.parentNode.cells[2].childNodes;

60、 var month=eval(myObj[2].innerHTML)-1;

61、 dateShow(obj.parentNode.parentNode.parentNode.nextSibling.nextSibling,eval(myObj[0].innerHTML),eval(myObj[2].innerHTML))

62、function addMonth(obj)//增加月份

63、 var myObj=obj.parentNode.parentNode.parentNode.cells[2].childNodes;

64、 var month=eval(myObj[2].innerHTML)+1;

65、 dateShow(obj.parentNode.parentNode.parentNode.nextSibling.nextSibling,eval(myObj[0].innerHTML),eval(myObj[2].innerHTML))

66、function dateShow(obj,year,month)//显示各月份的日

67、 var myDate=new Date(year,month-1,1);

68、 var selectDate=obj.parentNode.parentNode.previousSibling.previousSibling.value.split('-');

69、 if((year%4==0)&&(year%100!=0)||(year%400==0))

70、 for(i=0;i<obj.cells.length;i++)

71、 obj.cells[i].innerHTML='';

72、 obj.cells[i].style.color='';

73、 obj.cells[i].className='';

74、 obj.cells[i+day].innerHTML=(i+1);

75、 if(year==today.getFullYear()&&(month-1)==today.getMonth()&&(i+1)==today.getDate())

76、 obj.cells[i+day].style.color='red';

77、 if(year==eval(selectDate[0])&&month==eval(selectDate[1])&&(i+1)==eval(selectDate[2]))

78、 obj.cells[i+day].className='ds_border2';

79、function getValue(obj,inputObj)//把选择的日期传给输入框

80、 var myObj=inputObj.nextSibling.nextSibling.childNodes[0].childNodes[0].cells[2].childNodes;

81、 if(obj.innerHTML.length==1&&myObj[2].innerHTML.length==1)

82、 inputObj.value=myObj[0].innerHTML+"-0"+myObj[2].innerHTML+"-0"+obj.innerHTML;

83、 else if(obj.innerHTML.length==1&&myObj[2].innerHTML.length==2)

84、 inputObj.value=myObj[0].innerHTML+"-"+myObj[2].innerHTML+"-0"+obj.innerHTML;

85、 else if(obj.innerHTML.length==2&&myObj[2].innerHTML.length==1)

86、 inputObj.value=myObj[0].innerHTML+"-0"+myObj[2].innerHTML+"-"+obj.innerHTML;

87、 else if(obj.innerHTML.length==2&&myObj[2].innerHTML.length==2)

88、 inputObj.value=myObj[0].innerHTML+"-"+myObj[2].innerHTML+"-"+obj.innerHTML;

89、 inputObj.nextSibling.nextSibling.style.display='none';

90、 for(i=0;i<obj.parentNode.parentNode.parentNode.cells.length;i++)

91、 obj.parentNode.parentNode.parentNode.cells[i].className='';

92、 obj.className='ds_border2'

93、function dsMove(obj)//实现层的拖移

94、 obj.style.pixelLeft=X+(event.x-DS_x);

95、 obj.style.pixelTop=Y+(event.y-DS_y);

96、<script language=javascript>

97、 var myDate=new dateSelector();

98、 myDate.inputName='date';//