Home > Database > Mysql Tutorial > body text

Example sharing: Randomly generate an eight-digit discount code and save it to the Mysql database

小云云
Release: 2018-02-11 10:34:20
Original
2048 people have browsed it

Currently, many merchants use discount codes to carry out activities. Now we simply implement eight-digit discount codes and save them in the database. This article mainly introduces you to the relevant information of randomly generating eight-digit discount codes and saving them to the Mysql database. It is very good and has reference value. Friends who need it can refer to it. I hope it can help everyone.

1. Randomly generate the discount code code as follows:

import java.util.Random;
/**
*功能:随机生成优惠码
*@author iamwiam
*
*/
public class Activatedcode {
 public int ACTIVATEDCODENUM = 200; //生成的优惠码数量
 Random random = new Random();
 String candicatedCode = "abcedefghijklmnopqrstuvwxyz";//优惠码包含小写字母
 candicatedCode+=candicatedCode.toUpperCase();//优惠码包含大写字母
 candicatedCode+="1234567890";//优惠码包含阿拉伯数字
 for(int i=0; i<p>2. Save the discount code in the database </p><pre class="brush:php;toolbar:false">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); //占位符顺序从1开始,第一个参数是占位符的位置,第二个参数是占位符的值 
n = ps.executeUpdate(); 
}catch(ClassNotFoundException e){ 
e.printStackTrace(); 
}catch(SQLException e){ 
e.printStackTrace(); 
} 
}
Copy after login

3. Integrate

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Random;
/**
 * 功能:随机生成优惠码
 * @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><p>4. The results are as follows</p>
<p><img title="" alt="Example sharing: Randomly generate an eight-digit discount code and save it to the Mysql database" src="https://img.php.cn/upload/article/000/054/025/206775316b4935c9e4a9ca5472741680-0.jpg"></p>
<p>Related recommendations:</p>
<p><a href="http://www.php.cn/php-weizijiaocheng-79891.html" target="_self">About php receiving the expression sent from the ios client and saving it to the mysql database</a></p></activatedcodenum>
Copy after login

The above is the detailed content of Example sharing: Randomly generate an eight-digit discount code and save it to the Mysql database. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template