Java_socket程序学习05-彩票销售-第一阶段

建立mysql数据库

建立表user    表属性包括name + pass


server端初步设计

package db;


import java.io.*;
import java.net.*;
import java.sql.*;


import javax.swing.JOptionPane;
import java.sql.*;
public class server {
static int portNO = 8886;

public static void main(String[] args) {
ServerSocket server = null;
Socket socket = null;
try{
server = new ServerSocket(portNO);
System.out.println("success");
} catch(Exception e){
System.out.println("Error" +e);
 }

while(true){
try{
socket = server.accept();
} catch(Exception e1){
System.out.println("Error" +e1);
 }
String str_c = null;
try{
BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter os=new PrintWriter(socket.getOutputStream());


str_c = is.readLine();


System.out.println(str_c);
String box[]=str_c.split("\\|");
//System.out.println(box[0]+"   "+box[1]+"   "+box[2]);
int marker = Integer.parseInt(box[0]);
switch(marker)
{
case 1001:{
Connection con;
Statement sql;
ResultSet rs;
String name = box[1];
String pass = box[2];
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e3) {
e3.printStackTrace();
}
try {
String url = "jdbc:mysql://localhost:3306/dbtest";
con = DriverManager.getConnection(url, "root","123456");
sql = con.createStatement();
rs = sql.executeQuery("Select * From user");
while (rs.next()){
String name1 = rs.getString("name");
String pass1 = rs.getString("pass");
System.out.println(name1);
if((name1.equals(name))&&(pass1.equals(pass))){
os.println("1001|123");                                                  
os.flush();
con.close();
}
}
   os.println("1001|false");
os.flush();
System.out.println("22222222222");

} catch (SQLException e2) {
}
break;        //如果不用会直接去执行case1002
}//case

case 3001:{

}

}//switch
} catch(Exception e2){
System.out.println("Error" +e2);
 }
}//while


}//main
}//server

注意:两点注意,我调了一下午哈哈

1.if((name1.equals(name))&&(pass1.equals(pass))) 括号不能少

2.os.println("asdfsadf") 这里是println而不是print

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
客户端设计

客户端暂时包括三部分 login登录,base,sale销售


package cl;


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import javax.swing.*;
import java.net.*;


public class Login {

JFrame jf;
JTextField jt_user = new JTextField(14);
JTextField jt_pass = new JTextField(14);




String readtext;
public static void main(String[] args) {
new Login();
}

public  Login(){
createGUI();
}

public void createGUI(){
jf = new JFrame("用户登录");
jf.setLayout(new GridLayout(6, 2));
JPanel jpa = new JPanel();  //为了界面美观占位
jf.add(jpa);                //为了界面美观占位
JPanel jpa1 = new JPanel();  //为了界面美观占位
jf.add(jpa1);                //为了界面美观占位
JPanel jpa2 = new JPanel();  //为了界面美观占位
jf.add(jpa2);                //为了界面美观占位
JPanel jpa3 = new JPanel();  //为了界面美观占位
jf.add(jpa3);                //为了界面美观占位
JPanel jp1 = new JPanel();
jf.add(jp1);
JLabel jl1 = new JLabel("用户名:");
jp1.add(jl1);
jp1.add(jt_user);
JPanel jp3 = new JPanel();
jf.add(jp3);
JButton jb3 = new JButton ("重置");
jb3.addActionListener(new enter());
jp3.add(jb3);
JPanel jp2 = new JPanel();
jf.add(jp2);
JLabel jl2 = new JLabel("  密码:");
jp2.add(jl2);
jp2.add(jt_pass);
JPanel jp4 = new JPanel();
jf.add(jp4);
JButton jb4 = new JButton("登录");
jb4.addActionListener(new enter());
jp4.add(jb4);

jf.setBounds(400,200,400,400);
jf.setSize(500, 300);
jf.setResizable(false);  //设置固定窗体大小,不让改变
jf.setVisible(true);
}
class enter  implements ActionListener {
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();

if("登录".equals(str)){
new enter().check();                  //调用方法check密码检查
}else if("重置".equals(str)){
//System.out.println("yyyy");
new enter().reset();                  //调用reset方法
}
}
public void check(){
//StringBuffer read = new StringBuffer(); //因为要把两个字符串连起来,最方便的方法就是用的stringbuffer里的append()方法
//read=read.append(1001).append("|").append(user).append("|").append(pass);
//readl=read.toString();

String user = jt_user.getText();
String pass = jt_pass.getText();
if(user.equals("")){
jt_user.setText("用户名不能空");
}
else if (pass.equals("")){
jt_pass.setText("密码不能为空");
}
System.out.println(jt_user.getText());
readtext="1001"+"|"+user+"|"+pass;
System.out.print(readtext);
try{
  Socket socket=new Socket("127.0.0.1",8886);
  PrintWriter os=new PrintWriter(socket.getOutputStream());
  BufferedReader is=new BufferedReader (new InputStreamReader(socket.getInputStream()));
  os.println(readtext);//向服务器输出
  os.flush();          //缓存刷新
  
  String  readser=is.readLine();//等待接收服务器的信息
  System.out.println(readser);
  System.out.println("通过");
  String box[]=readser.split("\\|");
  String Marker=box[1];
  
  if(Marker.equals("false"))   
       JOptionPane.showMessageDialog(null, "用户名或密码错误");  ///在屏幕上弹出窗体,显示引号“中的内容”
  else{  
  new Base().createBaseGUI(); 
  jf.setVisible(false);             //隐藏主窗体
      }
  os.close();
          is.close();
          socket.close();
}catch(Exception e1)
  {
 System.out.println("Error"+e1) ;
  }
};

public void reset(){                        //密码检查方法
jt_user.setText("");                    //文本框清零
jt_pass.setText("");                    //文本框清零
jt_user.requestFocus();                 //把输入焦点放在调用这个方法的控件上
};

}



}


/*如果要二次输入密码时的,两次密码和对
public void passck(){                        //密码检查方法
String pass = jtf2.getText();
String passr = jtf3.getText();
if(!pass.equalsIgnoreCase(passr)){
jtf2.setText("密码不一致");
}else if(pass.equals("")||passr.equals("")){
jtf2.setText("密码不能为空");
}
};
*/

-------------------------------------------------------------------------------------------------------------------------


package cl;


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import javax.swing.*;
import java.net.*;


import cl.Login.enter;


public class Base  implements ActionListener{
JFrame jfb;

/*public static void main(String args[]){
new Base().createBaseGUI();
}
*/

public void createBaseGUI() {
jfb = new JFrame("");
jfb.setLayout(new GridLayout(3, 2));
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JPanel jp3 = new JPanel();
JPanel jp4 = new JPanel();
JPanel jp5 = new JPanel();
JPanel jp6 = new JPanel();
jfb.add(jp1);
jfb.add(jp2);
jfb.add(jp3);
jfb.add(jp4);
jfb.add(jp5);
jfb.add(jp6);
JButton jb_sale = new JButton ("销售");
jb_sale.setPreferredSize(new Dimension(200,100));
jp3.add(jb_sale);
jb_sale.setBounds(200,300,50,60);
JButton jb_dj = new JButton ("兑奖");
jb_dj.setPreferredSize(new Dimension(200,100));
jp4.add(jb_dj);
JButton jb_cx = new JButton ("查询");
jb_cx.setPreferredSize(new Dimension(200,100));
jp5.add(jb_cx);
JButton jb_mes = new JButton ("通知");
jb_mes.setPreferredSize(new Dimension(200,100));
jp6.add(jb_mes);
jb_dj.addActionListener(this);        //为四个按钮添加侦听事件
jb_cx.addActionListener(this);
jb_sale.addActionListener(this);
jb_mes.addActionListener(this);

jfb.setBounds(250,100,100,100);
jfb.setResizable(false); 
jfb.setSize(800, 600);
jfb.setVisible(true);

public void actionPerformed(ActionEvent e){
String str1 = e.getActionCommand();
if(str1.equals("销售")){
new Sale().createSaleGUI();
jfb.setVisible(false);
}
else if(str1.equals("兑奖")){
new Sale().createSaleGUI();
jfb.setVisible(false);
}
else if(str1.equals("查询")){
new Sale().createSaleGUI();
jfb.setVisible(false);
}
else if(str1.equals("通知")){
new Sale().createSaleGUI();
jfb.setVisible(false);
}
}

}

-------------------------------------------------------------------------------------------------------------------------------------


package cl;


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


import org.omg.CORBA.PUBLIC_MEMBER;


import cl.Login.enter;
public class Sale implements ActionListener{

JFrame jfs;
//定义输入框,原本放在了createGUI()方法里,无法让d3Sring方法等其他方法调用此变量,所以放在了最外边
JTextField nbd1 = new JTextField(2);
JTextField nbd2 = new JTextField(2);
JTextField nbd3 = new JTextField(2);
JTextField nbl1 = new JTextField(2);
JTextField nbl2 = new JTextField(2);
JTextField nbl3 = new JTextField(2);
JTextField nbl4 = new JTextField(2);
JTextField nbl5 = new JTextField(2);
JTextField nbl6 = new JTextField(2);
JTextField nbl7 = new JTextField(2);




//定义主窗体
public void createSaleGUI() {
jfs = new JFrame("彩票销售");
jfs.setBounds(250,100,100,100);
jfs.setLayout(new GridLayout(2, 2));
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JPanel jp3 = new JPanel();
JPanel jp4 = new JPanel();
jfs.add(jp1);
jfs.add(jp2);
jfs.add(jp3);
jfs.add(jp4);
JButton jb_3d = new JButton ("3D销售");
JButton jb_lot = new JButton ("LOT销售");
jb_3d.setPreferredSize(new Dimension(200,100));     //设置按钮的大小
jb_lot.setPreferredSize(new Dimension(200,100));
jp1.setLayout(null);         //设置jp1容器的布局
jp3.setLayout(null);
jp2.setLayout(null);
jp4.setLayout(null);


jp1.add(jb_3d);
jp3.add(jb_lot);
jb_3d.setBounds(100, 100, 100, 80);
jb_lot.setBounds(100, 100, 100, 80);


nbd1.setFont(new Font("黑体",Font.BOLD,30));         //改变字体和大小
nbd2.setFont(new Font("黑体",Font.BOLD,30));
nbd3.setFont(new Font("黑体",Font.BOLD,30));
nbl1.setFont(new Font("黑体",Font.BOLD,30));         //改变字体和大小
nbl2.setFont(new Font("黑体",Font.BOLD,30));
nbl3.setFont(new Font("黑体",Font.BOLD,30));
nbl4.setFont(new Font("黑体",Font.BOLD,30));         //改变字体和大小
nbl5.setFont(new Font("黑体",Font.BOLD,30));
nbl6.setFont(new Font("黑体",Font.BOLD,30));
nbl7.setFont(new Font("黑体",Font.BOLD,30));
               
jp2.add(nbd1);
nbd1.setBounds(0,100,60,80);
jp2.add(nbd2);
nbd2.setBounds(100,100,60,80);
jp2.add(nbd3);
nbd3.setBounds(200,100,60,80);
jp4.add(nbl1);
nbl1.setBounds(0,100,40,60);
jp4.add(nbl2);
nbl2.setBounds(44,100,40,60);
jp4.add(nbl3);
nbl3.setBounds(88,100,40,60);
jp4.add(nbl4);
nbl4.setBounds(132,100,40,60);
jp4.add(nbl5);
nbl5.setBounds(176,100,40,60);
jp4.add(nbl6);
nbl6.setBounds(220,100,40,60);
jp4.add(nbl7);
nbl7.setBounds(264,100,40,60);

jfs.setResizable(false); 
jfs.setSize(800, 600);
jfs.setVisible(true);

jb_3d.addActionListener(this);
jb_lot.addActionListener(this);


//获得3D投注号码,作为放回值
public String d3Str(){
//System.out.print("132431214231432");
String nb_1 = nbd1.getText();
String nb_2 = nbd2.getText();
String nb_3 = nbd3.getText();
String d3string = 3001+"|"+nb_1+"|"+nb_2+"|"+nb_3+"|"+"end";
System.out.println(d3string);
return(d3string);
}

//获得lot投注号码,作为返回值
public String lotStr(){
String nbl_1 = nbl1.getText();
String nbl_2 = nbl2.getText();
String nbl_3 = nbl3.getText();
String nbl_4 = nbl4.getText();
String nbl_5 = nbl5.getText();
String nbl_6 = nbl6.getText();
String nbl_7 = nbl7.getText();
String lotstring = 4001+"|"+nbl_1+"|"+nbl_2+"|"+nbl_3+"|"+nbl_4+"|"+nbl_5+"|"+nbl_6+"|"+nbl_7+"|"+"end";
return(lotstring);
}

//连接socket服务器,将字符窜发送给服务器,等待服务器确认,来判断是否成功
public void connectSocket(String a){
String tzh = a;
try{
Socket socket=new Socket("127.0.0.1",8886);
PrintWriter os=new PrintWriter(socket.getOutputStream());
BufferedReader is=new BufferedReader (new InputStreamReader(socket.getInputStream()));
os.print(tzh);
os.flush(); 

String reads=is.readLine();
String box[] = reads.split("\\|");
String Marker=box[1];
  
  if(Marker.equals("true"))   
       JOptionPane.showMessageDialog(null, "销售成功");  ///在屏幕上弹出窗体,显示引号“中的内容”
  else{  
  JOptionPane.showMessageDialog(null, "销售失败");
      }
    os.close();
           is.close();
           socket.close();
}catch(Exception e1)
  {
 System.out.println("Error"+e1) ;
  }
   };



public void actionPerformed(ActionEvent e){
String str1 = e.getActionCommand();
if(str1.equals("3D销售")){
//获得投注号码
//String nb_77777 = nbd1.getText();
//System.out.print(nb_77777);
String ss = this.d3Str();
   System.out.println(ss+"houhou");
//向服务器传数,将ss传入方法connectSocket.
new Sale().connectSocket(ss);
}else if(str1.equals("LOT销售")){
//获得投注号码
String ss = new Sale().lotStr();
//向服务器传数,将ss传入方法connectSocket.
new Sale().connectSocket(ss);
}
}

public static void main(String args[]){
new Sale().createSaleGUI();
}


}








        










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值