⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 server.java

📁 用户需要注册
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.util.Date;
import java.sql.*;
//服务器
public class Server extends JFrame{
	private static final long serialVersionUID = 1L;
	/**
	 * @param args
	 */
	private MessagePanel messagePanel = new MessagePanel("购物中心");
	private JTextArea jta = new JTextArea();
	private int result = 1;
	private int result1 = 1;
	private int result2 = 1;
	private int result3 = 1;
	private int result4 = 1;
	private int result5 = 1;
	private String flag = new String("");
	
	public Server()
	{
		setTitle("服务器");
		setSize(500, 300);
		
		jta.setEditable(false);
		jta.setLineWrap(true);
		jta.setWrapStyleWord(true);
		messagePanel.setCentered(true);
		messagePanel.setFont(new Font("Serif",Font.BOLD+Font.CENTER_BASELINE,50));
		messagePanel.setBackground(Color.blue);
		getContentPane().add(new JScrollPane(jta), BorderLayout.CENTER);
		getContentPane().add(messagePanel,BorderLayout.NORTH);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		try
		{
			ServerSocket serverSocket = new ServerSocket(2000);
			while(true)
			{
				Socket socket = serverSocket.accept();
				jta.append(new Date() + ": A client at " + 
						socket.getInetAddress().getHostAddress() + " connected\n");
				new RegistrationThread(socket).start();
			}
		}
		catch(IOException ex)
		{
			jta.append(new Date() + ": " + ex);
		}
	}
	class RegistrationThread extends Thread
	{
		private Socket socket;
		private BufferedReader in;
		public RegistrationThread(Socket socket)
		{
			this.socket = socket;
			jta.append(new Date() + ": Thread started\n");
			try
			{
				in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			}
			catch(IOException ex)
			{
				jta.append(new Date() + ": " + ex);
			}
		}
		public void run()
		{
			String name;
			String password;
			String id;
			String account;
			String question;
			String answer;
			String shopname="";
			try
			{
				flag = new String(in.readLine());
				int flag1=Integer.parseInt(flag);
				System.out.println(flag1);
				switch(flag1)
				{
				case 0://普通用户登录
				{
					name = new String(in.readLine());
					password = new String(in.readLine());
					if(Query.select(name))//判断用户名是否存在
					{
						Connection conn = null;
						Statement stmt = null;
						ResultSet rs=null;
						String s = new String();
						String host="localhost";
						String username="root";
						String password1="111111";
						String database="shoppingmall"; 
						String url = "jdbc:mysql://localhost:3306/shoppingmall";
						try{  
						   //-------------加载驱动-------------------------   
							Class.forName("org.gjt.mm.mysql.Driver").newInstance();
						  	}catch(Exception e){
						  		System.out.println("Error loading Mysql Driver!");
						  	}
						  	try{   
						  		conn = DriverManager.getConnection(url,username,password1);
						  		System.out.println("Success connect Mysql Database!1"); 
						  		stmt = conn.createStatement();  //Statement对象用于将sql语句发送到数据库中 
						  		rs = stmt.executeQuery("Select password FROM user where name = '"+name +"'");
						  		while(rs.next())
						  		{
						  			s = rs.getString("password").trim();
						  		}
						  		if(!s.equals(password))
						  			result2 = -1;
						  		else 
						  			jta.append("用户"+name+"登录成功");
						  		
						  		conn.close();
						  	}catch(SQLException e){
						  		System.out.println(e.getMessage());
						  	}
					}
					else 
						result2 = 0;
					try//返回结果
					{
						Socket isToClient2 = new Socket("localhost", 2003);
						DataOutputStream toClient2 = new DataOutputStream(isToClient2.getOutputStream());
						toClient2.writeInt(result2);
						result2=1;
					}
					catch(IOException ex)
					{
						System.err.println(ex);
					}
				}
				case 1:           //店主登录
				{
					name = new String(in.readLine());
					password = new String(in.readLine());
					if(Query.select(name))
					{
						Connection conn = null;
						Statement stmt = null;
						ResultSet rs=null;
						ResultSet rv=null;
						String s = new String();
						String v=new String();
						String host="localhost";
						String username="root";
						String password1="111111";
						String database="shoppingmall"; 
						String url = "jdbc:mysql://localhost:3306/shoppingmall";
						try{  
						   //-------------加载驱动-------------------------   
							Class.forName("org.gjt.mm.mysql.Driver").newInstance();
						    System.out.println("Success loading Mysql Driver!2");
						  	}catch(Exception e){
						  		System.out.println("Error loading Mysql Driver!");
						  	}
						  	try{   
						  		conn = DriverManager.getConnection(url,username,password1);
						  		//System.out.println("Success connect Mysql Database!"); 
						  		stmt = conn.createStatement();  //Statement对象用于将sql语句发送到数据库中 
						  		rv = stmt.executeQuery("Select shopname FROM user where name = '"+name +"'");
						  		while(rv.next())
						  		{
						  			//v=null;
						  			v=rv.getString("shopname");
						  		}
						  		if(v!=null)//未注册店铺,则报错
						  		{
						  			rs = stmt.executeQuery("Select password FROM user where name = '"+name +"'");
						  			while(rs.next())
						  			{
						  				s = rs.getString("password").trim();
						  			}
						  			if(!s.equals(password))
						  				result2 = -1;
						  		}
						  		else 
						  			result2=-2;
						  		conn.close();
						  	}catch(SQLException e){
						  		System.out.println(e.getMessage());
						  	}
					}
					else 
						result2 = 0;
					try
					{//返回结果
						Socket isToClient2 = new Socket("localhost", 2004);
						DataOutputStream toClient2 = new DataOutputStream(isToClient2.getOutputStream());
						toClient2.writeInt(result2);
						result2=1;
						toClient2.close();
						isToClient2.close();
					}
					catch(IOException ex)
					{
						System.err.println(ex);
					}
					
				}
				case 2:            //查询余额
				{
					name = new String(in.readLine());
					String sum4 = new String("");
					PrintWriter toClient4 = null;
					if(Query.select(name))
					{
						Connection conn = null;
						Statement stmt = null;
						ResultSet rs=null;
						ResultSet rv=null;
						String s = new String();
						String v=new String();
						String host="localhost";
						String username="root";
						String password1="111111";
						String database="shoppingmall"; 
						String url = "jdbc:mysql://localhost:3306/shoppingmall";
						try
						{  
							//-------------加载驱动-------------------------   
							Class.forName("org.gjt.mm.mysql.Driver").newInstance();
						}
						catch(Exception e)
						{
							System.out.println("Error loading Mysql Driver!");
						}
						try
						{   
							conn = DriverManager.getConnection(url,username,password1);
							stmt = conn.createStatement();  //Statement对象用于将sql语句发送到数据库中 
							rs = stmt.executeQuery("Select sum FROM user where name = '"+name +"'");
							while(rs.next())
							{
								s = rs.getString("sum").trim();
							}
							//sum1 = (Double.parseDouble(s));	
							sum4 = s;
						}
						catch(SQLException e)
						{
							System.out.println(e.getMessage());
						}
					}
					try
					{					
						Socket isToClient4 = new Socket("localhost", 2006);
						toClient4 = new PrintWriter(isToClient4.getOutputStream(),true);
						toClient4.println(sum4);
						jta.append("用户"+name+"查询余额成功");
					}
					catch(IOException ex)
					{
						System.err.println(ex);
					}
					toClient4.flush();
				}
				case 3:           //申请开店
				{
					name = new String(in.readLine());
					String shop = new String(in.readLine());
					DataOutputStream toClientz = null;
					Change.updateUser(name, shop,0);//user表中加入店铺名称
					try
					{	//返回结果				
						Socket isToClientz = new Socket("localhost", 2030);
						toClientz = new DataOutputStream(isToClientz.getOutputStream());
						toClientz.writeInt(1);
						jta.append("用户"+name+"开店成功");
					}
					catch(IOException ex)
					{
						System.err.println(ex);
					}
					toClientz.flush();
				}
				case 4:         //初始化大厅
				{
					int i = 0;
					int j = 0;
					String[] s = new String[20];
					String[] s1 = new String[20];
					PrintWriter toClient6 = null;
					Connection conn = null;
					Statement stmt = null;
					ResultSet rs=null;
					ResultSet rv=null;
					//String s = new String();
					String v=new String();
					String host="localhost";
					String username="root";
					String password1="111111";
					String database="shoppingmall"; 
					String url = "jdbc:mysql://localhost:3306/shoppingmall";
					try
					{  
							//-------------加载驱动-------------------------   
						Class.forName("org.gjt.mm.mysql.Driver").newInstance();
					}
					catch(Exception e)
					{
						System.out.println("Error loading Mysql Driver!");
					}
					try
					{   
						conn = DriverManager.getConnection(url,username,password1);
							//System.out.println("Success connect Mysql Database!"); 
						stmt = conn.createStatement();  //Statement对象用于将sql语句发送到数据库中 
						rs = stmt.executeQuery("Select shopname FROM user where state = '1' and shopname <>'null'");
						while(rs.next())
						{
							s[i] = rs.getString("shopname");
							i ++;
						}
						rs = stmt.executeQuery("Select name FROM user where state = '1' and shopname <>'null'");
						while(rs.next())
						{
							s1[j] = rs.getString("name");
							j ++;
						}
					}
					catch(SQLException e)
					{
						System.out.println(e.getMessage());
					}
					
					try
					{	//返回结果				
						Socket isToClient6 = new Socket("localhost", 2009);
						toClient6 = new PrintWriter(isToClient6.getOutputStream(),true);
						int x = i;
						toClient6.println(String.valueOf(i));
						i = 0;
						j = 0;
						
						for(;i < x;)
						{
							toClient6.println(s[i]);
							toClient6.println(s1[j]);
							i ++;
							j ++;
		
							
						}
					}
					catch(IOException ex)
					{
						System.err.println(ex);
					}
					toClient6.flush();
				}
				case 5:              //充值
				{
					name = new String(in.readLine());
					String sum4 = new String(in.readLine());
					double sum1 = Double.parseDouble(sum4);
					double sum2 = 0;
					DataOutputStream toClient5 = null;
						Connection conn = null;
						Statement stmt = null;
						ResultSet rs=null;
						ResultSet rv=null;
						String s = new String();
						String v=new String();
						String host="localhost";
						String username="root";
						String password1="111111";
						String database="shoppingmall"; 
						String url = "jdbc:mysql://localhost:3306/shoppingmall";
						try
						{  
							//-------------加载驱动-------------------------   
							Class.forName("org.gjt.mm.mysql.Driver").newInstance();
						}
						catch(Exception e)
						{
							System.out.println("Error loading Mysql Driver!");
						}
						try
						{   
							conn = DriverManager.getConnection(url,username,password1);
							stmt = conn.createStatement();  //Statement对象用于将sql语句发送到数据库中 
							rs = stmt.executeQuery("Select sum FROM user where name = '"+name +"'");
							while(rs.next())
							{
								s = rs.getString("sum").trim();
							}
							//sum1 = (Double.parseDouble(s));	
							sum2 = Double.parseDouble(s);
						}
						catch(SQLException e)
						{
							System.out.println(e.getMessage());
						}
					sum1 = sum1 + sum2;
					String sum3 = new String("");
					sum3 = String.valueOf(sum1);
					Change.updateUser(name, sum1);
					jta.append("用户"+name+"充值成功");
					try
					{	//返回结果				
						Socket isToClient5 = new Socket("localhost", 2008);
						toClient5 = new DataOutputStream(isToClient5.getOutputStream());
						toClient5.writeInt(1);
					}
					catch(IOException ex)
					{
						System.err.println(ex);
					}
					toClient5.flush();
				}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -