java基础类(二) 随机插入数字,并对用户的输入进行校验

 

 1 package com.duduli.li.db;
 2 
 3 
 4 import java.sql.ResultSet;
 5 import java.sql.SQLException;
 6 import java.util.Random;
 7 import java.util.Scanner;
 8 
 9 
10 public class CheckCode {
11     /**
12      * 插入验证码,以及对用户的输入进行验证
13      * @param args
14      */
15     public String nextWord(){
16         Random rd = new Random();
17         String str = "";
18         char [] ch = {'a','b','c','d'};
19         for(int i=0; i<4; i++){
20             str += ch[rd.nextInt(4)];
21         }
22         System.out.println(str);
23         return str;
24     }
25     
26     @SuppressWarnings("static-access")
27     public static void main(String[] args) throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
28     // TODO Auto-generated method stub
29 //  除去注释即可对插入验证码
30 //        CheckCode mt = new CheckCode();
31         Conn c = new Conn();
32 /*            for(int i=0; i<100; i++){
33                 String sql = "insert into userinsert(val) values (\"" + mt.nextWord()+"\")";
34                 System.out.println(sql);
35                 c.execute(sql);
36             }*/
37         Random rd = new Random();
38         String s = null;
39         String sql = "select val from userinsert where id = " + rd.nextInt(100);
40         ResultSet rs = c.query(sql);
41         while (rs.next()){
42             s = rs.getString(1);
43         }
44         c.colsed();
45         System.out.println(s);
46         Scanner scan = new Scanner(System.in);
47         String inputWord = "";
48         inputWord = scan.nextLine();
49         if(inputWord.equals(s)){
50             System.out.println("successful");
51         }else{
52             System.out.println("failure");
53         }
54     }
55 }

posted on 2010-05-20 10:35 duduli 阅读(1755) 评论(0)  编辑  收藏 所属分类: java


只有注册用户登录后才能发表评论。


网站导航:
 
<2010年5月>
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

导航

统计

公告

welcome to my place.

常用链接

留言簿(5)

我参与的团队

随笔分类

随笔档案

新闻分类

石头JAVA摆地摊儿

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜

@duduli