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!