十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
用redis也不是不可以,但效率可能有点低,建议使用乐观锁解决这个问题。
创新互联公司是专业的西山网站建设公司,西山接单;提供网站建设、网站制作,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行西山网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
举个例子:
假设order表里有个version字段,该字段只能单向自增(一般就是+1),SELECT的时候把version也查出来:
SELECT ..., version FROM order WHERE ...;UPDATE orderSET ...,version = version+1WHERE version = 上一个SELECT语句带出来的version值
假设用户A和用户B在某时间段内先后或同时查出来order_id=1, version=1的订单,UPDATE的时候由于mysql行锁的存在,只会有一个用户UPDATE成功(1 rows affected),另一个用户则UPDATE失败(0 rows affected),然后可以根据UPDATE后返回的话行数判断用户是否抢单成功。
你是青鸟的吧 这我写过 有源码 这里怎么上传压缩包啊
package ghhh;
import java.util.Scanner;
public class DvD {
public static void main(String[] args) {
int state[]=new int[6];
String name[]=new String[6];
int date[]=new int[6];
int count[]=new int [6];
name[0]="权利的游戏";
name[1]="命运之夜";
name[2]="傲慢与偏见";
state[0]=1;
state[1]=0;
state[2]=1;
date[0]=13;
date[1]=0;
date[2]=9;
count[0]=23;
count[1]=23;
count[2]=23;
int n;
// boolean n=false;
do{
System.out.println("欢迎使用迷你DVD管理器");
System.out.println("1.新增DVD");
System.out.println("2.查看DVD");
System.out.println("3.删除DVD");
System.out.println("4.借出DVD");
System.out.println("5.归还DVD");
System.out.println("6.退出DVD");
Scanner input =new Scanner(System.in);
System.out.println("请选择:");
n=input.nextInt();
switch(n){
case 1:
System.out.println("请输入要增加DVD的名称:");
String name1=input.next();
boolean flag=false;
for(int i=0;iname.length;i++){
if(name[i]==null){
name[i]=name1;
flag=true;
break;
}
}
if(flag){
System.out.println("新增DVD"+name1+"成功");
}else{
System.out.println("货架已满!增加失败!");
}
System.out.println("请输入0返回!");
n=input.nextInt();
break;
case 2:
System.out.println("序号\t"+"状态\t"+"名称\t\t"+"借出日期\t"+"借出次数");
for(int i=0;iname.length;i++){
if(name[i]!=null){
String state1 =((state[i]==0)?"可借":"已借");
String date1=((date[i]==0)?"":date[i]+"日");
String count1=count[i]+"次";
System.out.println((i+1)+"\t"+state1+"\t"+name[i]+"\t"+date1+"\t\t"+count1);
}
}
System.out.println("请输入0返回!");
n=input.nextInt();
break;
case 3:
System.out.println("请输入要删除的DVD名称:");
String name2=input.next();
int index=-1;
boolean a=false;
boolean flag1=false;
for(int i=0;iname.length;i++){
if(name2.equals(name[i])state[i]==1){
System.out.println("此DVD已经借出,无法删除");
a=true;
break;
}else if(name2.equals(name[i])state[i]==0){
a=true;
index=i;
flag1=true;
System.out.println("删除成功!");
break;
}
}
if(a==false){
System.out.println("没有找到相同名称的DVD!");
}
if(flag1){
for (int i=index;iname.length;i++){
if(i!=name.length-1){
name[i]=name[i+1];
state[i]=state[i+1];
date[i]=date[i+1];
count[i]=count[i+1];
}
name[name.length-1]=null;
state[name.length-1]=0;
date[name.length-1]=0;
count[name.length-1]=0;
}
}
System.out.println("请输入0返回!");
n=input.nextInt();
break;
case 4:
System.out.println("请输入要借出的DVD:");
String name3=input.next();
boolean a3=false;
boolean b3=false;
for(int i=0;iname.length;i++){
if(name3.equals(name[i]) state[i]==1){
System.out.println("该DVD已经借出");
a3=true;
}else if(name3.equals(name[i]) state[i]==0){
do{
System.out.println("请输入借出的日期:");
int m=input.nextInt();
if(m31||m1){
System.out.println("请重新输入日期:");
b3=true;
}else{
date[i]=m;
state[i]=1;
count[i]+=1;
}
}while(b3==true);
System.out.println("借出成功!");
a3=true;
}
}
if(a3==false){
System.out.println("没有该DVD");
}
System.out.println("请输入0返回!");
n=input.nextInt();
break;
case 5:
System.out.println("请输入要归还的DVD:");
String name5=input.next();
boolean b5=false;
boolean m5=false;
for(int i=0;iname.length;i++){
if(name5.equals(name[i]) state[i]==1){
b5=true;
do{
System.out.println("请输入要归还DVD的日期:(归还日期请输入当月日期 1~31)");
int a5=input.nextInt();
if(a531){
System.out.println("请重新输入日期:");
m5=true;
}else if(a5date[i]){
System.out.println("借出日期是"+date[i]+"日\t输入的日期不能小于借出的日期,请重新输入日期:");
m5=true;
}else{
state[i]=0;
System.out.println("归还成功");
System.out.println("借出日期是:"+date[i]+"归还日期是:"+a5+"日\t租金一天一元:共"+(a5-date[i])+"元");
date[i]=0;
m5=false;
}
}while(m5==true);
}else if (name5.equals(name[i]) state[i]==0){
System.out.println("该DVD未借出,不可归还!");
b5=true;
}
}
if(b5==false){
System.out.println("没有该名称的DVDV");
}
System.out.println("请输入0返回!");
n=input.nextInt();
break;
case 6:
n=1;
System.out.println("程序退出!");
break;
default:
if(n==0){
}else{
System.out.println("输入错误!请重新输入!");
n=0;
}
break;
}
}while(n==0);
System.out.println("谢谢使用!");
}
}
看看有没有问题 好久之前的了
class Stack_Float
{
float nums[];
int top;
Stack_Float()
{
nums = new float[50];
top = -1;
}
boolean IsEmpty()
{
if(top == -1)
return true;
else
return false;
}
float Pop_Stack()
{
if(top==-1)
{
return 0;
}
top--;
return nums[top + 1];
}
float GetTop()
{
return nums[top];
}
void Push_Stack(float num)
{
if(top == 49)
return;
top++;
nums[top] = num;
}
}
/*****************************************************************************/
class Stack_Char
{
char str[];
int top;
Stack_Char()
{
str = new char[50];
top = -1;
}
boolean IsEmpty()
{
if(top==-1)
return true;
else
return false;
}
void Push_Stack(char ch)
{
if(top == 49)
return;
top++;
str[top] = ch;
}
char Pop_Stack()
{
if(top == -1)
return '\0';
top--;
return str[top + 1];
}
char GetTop()
{
if(top == -1)
{
System.out.print("error");
System.exit(0);
}
return str[top];
}
}
/*****************************************************************************/
public class jisuanqi extends javax.swing.JFrame
{
String show = "";
public jisuanqi()
{
initComponents();
}
char[] TranSmit(char str[])
{
char houzhui[] = new char[50]; //存放后缀表达式的字符串
int i = 0,j = 0;
char c = str[i];
Stack_Char s = new Stack_Char(); //存放运算符的栈
while(c != '=') //对算术表达式扫描未结束时
{
if(c = '0' c = '9')
{
while(c = '0' c = '9')//数字直接入栈
{
houzhui[j]=c;
j++;
i++;
c=str[i];
}
houzhui[j]='#';//用#隔开数字
j++;
}
switch(c) //扫描到运算符时
{
case '+':
case '-':
case '*':
case '/':
if(s.IsEmpty() == true) //栈空,直接入栈
{
s.Push_Stack(c);
i++;
c=str[i];
break;
}
if(ComPare(s.GetTop(),c) == -1)
{
s.Push_Stack(c); //入栈
i++;
c=str[i];
break;
}
if(ComPare(s.GetTop(),c) == 1)
{
houzhui[j]=s.Pop_Stack();//出栈元素存入后缀表达式
j++;
break;
}
}
}
while(s.IsEmpty() != true)//把剩余的运算符直接出栈
{
houzhui[j]=s.Pop_Stack();
j++;
}
houzhui[j] = '=';//后缀表达式后面加 =
j++;
houzhui[j] = '\0';
j++;
return houzhui;
}
float Count(char str[])
{
Stack_Float s = new Stack_Float();//定义存放数字的栈
char c = str[0];
int i = 0,j = 0;
float result = 0,temp,left,right;
while(c != '=') //未扫描到 = 时
{
if(c = '0' c = '9')//扫描到数字
{
temp = 0;
while(c != '#')//未读到分隔符时
{
temp = temp * 10 + c - '0';
i++;
c = str[i];
}
s.Push_Stack(temp);//进栈
}
switch(c)//扫描到运算符时
{
case '+':
{
result = s.Pop_Stack() + s.Pop_Stack();//2个数字出栈相加
s.Push_Stack(result);//最后得数进栈
break;
}
case '-':
{
right = s.Pop_Stack();//右操作数出栈
left = s.Pop_Stack();//左操作数出栈
result = left - right;
s.Push_Stack(result);
break;
}
case '*':
{
result = s.Pop_Stack() * s.Pop_Stack();//2个数字出栈相乘
s.Push_Stack(result);
break;
}
case '/':
{
right = s.Pop_Stack();//右操作数出栈
left = s.Pop_Stack();//左操作数出栈
result = left / right;
s.Push_Stack(result);
break;
}
}
i++;
c = str[i];
}
return result;
}
int ComPare(char a,char b) //判断运算符的优先级函数
{
int s[][] =
{// 栈顶元素高于算术表达式中的元素时, 返回 1,否则返回 -1
{1,1,-1,-1},
{1,1,-1,-1},
{1,1,1,1},
{1,1,1,1},
};
char x1[]={'+','-','*','/'};//栈顶元素
char x2[]={'+','-','*','/'};//算术表达式中的元素
int k=0,m,n = 0;
for(m=0;m4;m++) //查找2个进行比较的运算符在表中的位置,并返回比较结果
{
for(n=0;n4;n++)
{
if(x1[m]==ax2[n]==b)
{
k=1;break; //找到比较结果后,跳出循环
}
}
if(k==1)
break;
}
return s[m][n];//返回比较结果
}
/*****************************************************************************/
@SuppressWarnings("unchecked")
// editor-fold defaultstate="collapsed" desc="Generated Code"//GEN-BEGIN:initComponents
private void initComponents() {
text = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
jButton9 = new javax.swing.JButton();
jButton10 = new javax.swing.JButton();
jButton11 = new javax.swing.JButton();
jButton12 = new javax.swing.JButton();
jButton13 = new javax.swing.JButton();
jButton14 = new javax.swing.JButton();
jButton21 = new javax.swing.JButton();
jButton22 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
getContentPane().add(text);
text.setBounds(10, 10, 270, 30);
jButton1.setText("1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1);
jButton1.setBounds(10, 50, 60, 25);
jButton2.setText("2");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
getContentPane().add(jButton2);
jButton2.setBounds(80, 50, 60, 25);
jButton3.setText("3");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
getContentPane().add(jButton3);
jButton3.setBounds(150, 50, 60, 25);
jButton4.setText("4");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
getContentPane().add(jButton4);
jButton4.setBounds(220, 50, 60, 25);
jButton5.setText("5");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
getContentPane().add(jButton5);
jButton5.setBounds(10, 80, 60, 25);
jButton6.setText("6");
jButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton6ActionPerformed(evt);
}
});
getContentPane().add(jButton6);
jButton6.setBounds(80, 80, 60, 25);
jButton7.setText("7");
jButton7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton7ActionPerformed(evt);
}
});
getContentPane().add(jButton7);
jButton7.setBounds(150, 80, 60, 25);
jButton8.setText("8");
jButton8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton8ActionPerformed(evt);
}
});
getContentPane().add(jButton8);
jButton8.setBounds(220, 80, 60, 25);
jButton9.setText("9");
jButton9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton9ActionPerformed(evt);
}
});
getContentPane().add(jButton9);
jButton9.setBounds(10, 110, 60, 25);
jButton10.setText("0");
jButton10.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton10ActionPerformed(evt);
}
});
getContentPane().add(jButton10);
jButton10.setBounds(80, 110, 60, 25);
jButton11.setText("+");
jButton11.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton11ActionPerformed(evt);
}
});
getContentPane().add(jButton11);
jButton11.setBounds(150, 110, 60, 25);
jButton12.setText("-");
jButton12.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton12ActionPerformed(evt);
}
});
getContentPane().add(jButton12);
jButton12.setBounds(220, 110, 60, 25);
jButton13.setText("*");
jButton13.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton13ActionPerformed(evt);
}
});
getContentPane().add(jButton13);
jButton13.setBounds(10, 140, 60, 25);
jButton14.setText("/");
jButton14.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton14ActionPerformed(evt);
}
});
getContentPane().add(jButton14);
jButton14.setBounds(80, 140, 60, 25);
jButton21.setText("CE");
jButton21.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton21ActionPerformed(evt);
}
});
getContentPane().add(jButton21);
jButton21.setBounds(150, 140, 60, 25);
jButton22.setText("=");
jButton22.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton22ActionPerformed(evt);
}
});
getContentPane().add(jButton22);
jButton22.setBounds(220, 140, 60, 25);
pack();
}// /editor-fold//GEN-END:initComponents
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
show += "1";
text.setText(show);
}//GEN-LAST:event_jButton1ActionPerformed
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
show += "2";
text.setText(show);
}//GEN-LAST:event_jButton2ActionPerformed
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
show += "3";
text.setText(show);
}//GEN-LAST:event_jButton3ActionPerformed
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
show += "4";
text.setText(show);
}//GEN-LAST:event_jButton4ActionPerformed
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
show += "5";
text.setText(show);
}//GEN-LAST:event_jButton5ActionPerformed
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
show += "6";
text.setText(show);
}//GEN-LAST:event_jButton6ActionPerformed
private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
show += "7";
text.setText(show);
}//GEN-LAST:event_jButton7ActionPerformed
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
show += "8";
text.setText(show);
}//GEN-LAST:event_jButton8ActionPerformed
private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
show += "9";
text.setText(show);
}//GEN-LAST:event_jButton9ActionPerformed
private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed
show += "0";
text.setText(show);
}//GEN-LAST:event_jButton10ActionPerformed
private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton13ActionPerformed
show += "*";
text.setText(show);
}//GEN-LAST:event_jButton13ActionPerformed
private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton14ActionPerformed
show += "/";
text.setText(show);
}//GEN-LAST:event_jButton14ActionPerformed
private void jButton21ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton21ActionPerformed
show = "";
text.setText("");
}//GEN-LAST:event_jButton21ActionPerformed
private void jButton22ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton22ActionPerformed
show += "=";
text.setText(show);
char str1[] = new char[50];
char str2[] = new char[50];
float result = 0;
str1 = show.toCharArray();
str2 = TranSmit(str1);
result = Count(str2);
text.setText("" + result);
show = "";
}//GEN-LAST:event_jButton22ActionPerformed
private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton12ActionPerformed
show += "-";
text.setText(show);
}//GEN-LAST:event_jButton12ActionPerformed
private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton11ActionPerformed
show += "+";
text.setText(show);
}//GEN-LAST:event_jButton11ActionPerformed
public static void main(String args[])
{
jisuanqi j = new jisuanqi();
j.setBounds(300, 300, 300, 195);
j.setVisible(true);
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton10;
private javax.swing.JButton jButton11;
private javax.swing.JButton jButton12;
private javax.swing.JButton jButton13;
private javax.swing.JButton jButton14;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton21;
private javax.swing.JButton jButton22;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
private javax.swing.JTextField text;
// End of variables declaration//GEN-END:variables
}