乐趣区

JAVA插入随机验证码并进行校验

1 package com.duduli.li.db;
4 import java.sql.ResultSet;
5 import java.sql.SQLException;
6 import java.util.Random;
7 import java.util.Scanner;

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 }

退出移动版