Randomly generate an eight-digit discount code and save it to the MySQL database

Randomly generate an eight-digit discount code and save it to the MySQL database

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:
  • Reasons and solutions for being unable to remotely connect to MySQL database under CentOS7
  • How to connect to MySQL database in Java and test whether the connection is successful
  • MyBatis implements Mysql database sub-library and sub-table operations and summary (recommended)
  • PHP database connection tool class based on MySQLI function encapsulation [definition and usage]

<<:  Detailed explanation of how to enable HSTS in nginx to force the browser to redirect to HTTPS access

>>:  How to Learn Algorithmic Complexity with JavaScript

Recommend

Detailed explanation of MySQL backup and recovery practice of mysqlbackup

1. Introduction to mysqlbackup mysqlbackup is the...

JavaScript to implement random roll call web page

JavaScript writes a random roll call webpage for ...

How to import txt into mysql in Linux

Preface When I was writing a small project yester...

Docker online and offline installation and common command operations

1. Test environment name Version centos 7.6 docke...

Which scenarios in JavaScript cannot use arrow functions

Table of contents 1. Define object methods 2. Def...

Implementation of Docker deployment of web projects

The previous article has installed the docker ser...

Detailed explanation of various HTTP return status codes

When a request is sent to your server to display ...

3 methods to restore table structure from frm file in mysql [recommended]

When mysql is running normally, it is not difficu...

mysql5.5.28 installation tutorial is super detailed!

mysql5.5.28 installation tutorial for your refere...

How to use positioning to center elements (web page layout tips)

How to center an element in the browser window He...

Some suggestions for Linux system optimization (kernel optimization)

Disable swap If the server is running a database ...

Three methods to modify the hostname of Centos7

Method 1: hostnamectl modification Step 1 Check t...

Research on the effect of page sidebar realized by JS

Table of contents Discover: Application of displa...