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
1. Introduction to mysqlbackup mysqlbackup is the...
JavaScript writes a random roll call webpage for ...
Preface When I was writing a small project yester...
1. Test environment name Version centos 7.6 docke...
Table of contents 1. Define object methods 2. Def...
The previous article has installed the docker ser...
When a request is sent to your server to display ...
When mysql is running normally, it is not difficu...
mysql5.5.28 installation tutorial for your refere...
How to center an element in the browser window He...
Disable swap If the server is running a database ...
Method 1: hostnamectl modification Step 1 Check t...
1. Permanent modification, valid for all users # ...
Linux virtual machine: VMware + Ubuntu 16.04.4 Wi...
Table of contents Discover: Application of displa...