Currently, many businesses are conducting promotions through discount codes. Now we simply implement eight-digit discount codes and save them in the database. 1. Randomly generate discount codes as follows: import java.util.Random; /** *Function: Randomly generate discount codes*@author iamwiam * */ public class ActivatedCode { public int ACTIVATEDCODENUM = 200; //Number of coupon codes generated Random random = new Random(); String candicatedCode = "abcedefghijklmnopqrstuvwxyz"; //The discount code contains lowercase letters candicatedCode+=candicatedCode.toUpperCase(); //The discount code contains uppercase letters candicatedCode+="1234567890"; //The discount 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); //Randomly generate 200 8-digit 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 order of placeholders starts from 1, the first parameter is the position of the placeholder, and the second parameter is the value of the placeholder n = ps.executeUpdate(); }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 discount codes * @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 connection = 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.executeUpdate(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 4. The results are as follows Summarize The above is what I introduced to you about randomly generating eight-digit discount codes and saving them to the Mysql database. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website! You may also be interested in:
|
>>: How to Learn Algorithmic Complexity with JavaScript
Table of contents 1. Prototype 2. Prototype chain...
Table of contents 1. Title 2. Code 3. Results IV....
Hello everyone, today I will share with you the W...
var() Introduction and Usage Details (MDN) IE is ...
Introduction <br />Not everyone has access t...
Use the FRAME property to control the style type ...
Data cleaning (ETL) Before running the core busin...
1. Introduction to gitlab Gitlab official address...
Unzip the file into a directory This is the direc...
There is no need to say much about the difference...
Maybe everyone knows that js execution will block...
Cocos Creator version: 2.3.4 Demo download: https...
“Inputs should be divided into logical groups so ...
Mybatis paging plug-in pageHelper detailed explan...
Introduction: The configuration of Docker running...