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

Prototype and prototype chain prototype and proto details

Table of contents 1. Prototype 2. Prototype chain...

JavaScript implements constellation query function with detailed code

Table of contents 1. Title 2. Code 3. Results IV....

WePY cloud development practice in Linux command query applet

Hello everyone, today I will share with you the W...

25 Ways and Tips to Increase Web Page Loading Speed

Introduction <br />Not everyone has access t...

HTML table tag tutorial (12): border style attribute FRAME

Use the FRAME property to control the style type ...

Application of Hadoop counters and data cleaning

Data cleaning (ETL) Before running the core busin...

Docker-compose one-click deployment of gitlab Chinese version method steps

1. Introduction to gitlab Gitlab official address...

Detailed tutorial on building Gitlab server on CentOS8.1

There is no need to say much about the difference...

A brief discussion on whether CSS will block page rendering

Maybe everyone knows that js execution will block...

How to use cc.follow for camera tracking in CocosCreator

Cocos Creator version: 2.3.4 Demo download: https...

Summary of Form Design Techniques in Web Design

“Inputs should be divided into logical groups so ...

Mybatis paging plug-in pageHelper detailed explanation and simple example

Mybatis paging plug-in pageHelper detailed explan...

Detailed steps for running springboot project in Linux Docker

Introduction: The configuration of Docker running...