Generate a random eight-digit discount code and save it to the Mysql database.

Source: Internet
Author: User

Generate a random eight-digit discount code and save it to the Mysql database.

Currently, many sellers use discount codes to perform activities. Now we can easily implement eight-digit discount codes and store them in the database.

1. The randomly generated coupon code is as follows:

Import java. util. random;/*** function: randomly generate a discount Code * @ author iamwiam **/public class Activatedcode {public int ACTIVATEDCODENUM = 200; // Number of generated discount codes Random random = new Random (); String candicatedCode = "abcedefghijklmnopqrstuvwxyz"; // The discount Code contains the lower-case letter candicatedCode + = candicatedCode. toUpperCase (); // The coupon code contains the upper-case letter candicatedCode + = "1234567890"; // The coupon code contains Arabic numerals for (int I = 0; I <ACTIVATEDCODENUM; I ++) {String res = ""; for (int j = 0; j <8; j ++) {res + = candicatedCode. charAt (random. nextInt (candicatedCode. lenght ();} System. out. println (res); // generates 200 random 8-bit discount codes }}

2. Save the discount code in the database

Private static void insertToMySql (String res) {int n = 0; try {Class. forName ("com. mysql. jdbc. driver "); Connection connection = DriverMannager. getConnection ("jdbc: mysql: // 127.0.0.1/tb_act_code", "zy", "IamWiam"); String SQL = "insert into checkNum (value) values (?) "; PreparedStatement ps = connection. prepareStatement (SQL); ps. setObject (1, res); // The placeholder sequence starts from 1. The first parameter is the placeholder position, and the second parameter is the placeholder value n = ps.exe cuteUpdate ();} catch (ClassNotFoundException e) {e. printStackTrace ();} catch (SQLException e) {e. printStackTrace ();}}

3. Integration

Import java. SQL. connection; import java. SQL. driverManager; import java. SQL. preparedStatement; import java. SQL. SQLException; import java. util. random;/*** function: randomly generate a discount Code * @ author iamwiam **/public class Activatedcode {public static void main (String [] args) {final int ACTIVATEDCODENUM = 200; random random = new Random (); String candicatedCode = "abcdefghijklmnopqrstuvwxyz"; candicatedCode + = candicatedCode. ToUpperCase (); candicatedCode ++ = "1234567890"; for (int I = 0; I <ACTIVATEDCODENUM; I ++) {String res = ""; for (int j = 0; j <8; j ++) {res + = candicatedCode. charAt (random. nextInt (candicatedCode. length ();} // String pwd = Activatedcode. getMD5 (Activatedcode. getMD5 (res); insertToMysql (res) ;}} private static void insertToMysql (String res) {int n = 0; try {Class. forName ("com. mysql. jdbc. driver "); Connection connec Tion = DriverManager. getConnection ("jdbc: mysql: // 127.0.0.1/new2017", "zy", "IamWiam"); String SQL = "insert into checkNum (value) values (?) "; PreparedStatement ps = connection. prepareStatement (SQL); ps. setObject (1, res); n = ps.exe cuteUpdate ();} catch (ClassNotFoundException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (SQLException e) {// TODO Auto-generated catch block e. printStackTrace ();}}}

4. The result is as follows:

Summary

The above section describes how to randomly generate an eight-digit discount code and save it to the Mysql database. I hope it will help you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.