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

📄 simpleserversocket.java

📁 一个基于Java servelet的hotel broker 程序 实现access数据库访问
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
   public void actionPerformed(ActionEvent e)
            {
                    
                  ////////////////////
              if(e.getActionCommand().equals("Send"))
           {
           	  //如果想只给某一个客户机发消息,则i>0
           	  int i=-10;
           	   String client1=list.getSelectedItem();
           	     StringTokenizer stc = new StringTokenizer(client1,":");
                String user = stc.nextToken();
           	    StringBuffer msg=null;
           	   if(i>0)
           	   {
           	   	 msg = new StringBuffer("MSG:"+"BROAD:");
           	     msg.append("Server:"+tf.getText());
           	   	  sendToClients(msg);
           	   }          	   
              else
              {
              	  msg = new StringBuffer("MSG:"+stc+":");
              	    msg.append("Server:"+tf.getText());
              	  sendToClient(msg,user);
              }
              
             
               tf.setText("");
           }
          
          
          
          
             ///////////////////////
            }     
    ///增加一个主方法    
 public static void main(String[] args)
    {
        simpleserversocket app = new simpleserversocket();
    }	


	
	 //通知客户端刷新用户列表
    public void refreshClientList()
    {
        StringBuffer sb = new StringBuffer("PEOPLE");
        for(int i=0;i<array.size();i++)
        {
            Client c =(Client)array.get(i);
            sb.append(":"+c.name);
        }
        sendToClients(sb);
    }
    
  /* public List<String> getCity()  //connecting to the oracle database for retriving data
    {
    	final String sql = "SECECT NAME FROM CITY";
    	final List<String> result = new ArrayList<String>();
    	
    	Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        
        String url="jdbc:oracle:thin:@localhost:1521:orcl"; //orcl为数据库的SID 
        String user="test"; 
        String password="test";
        
        try
        {
        	connection= DriverManager.getConnection(url,user,password); 
            statement = connection.createStatement();
                            
            resultSet = statement.executeQuery(sql);
            
            while (resultSet.next())
            {                
                result.add(resultSet.getString("NAME"));
            }
        }
        catch(Exception ex)
        {
         	ex.printStackTrace();
        }
        finally
        {
            if (connection != null)
            {
                try
                {
                    connection.close();
                }
                catch (Exception e)
                {
                    // do nothing
                }
            }
            
            if (statement != null)
            {
                try
                {
                    statement.close();
                }
                catch (Exception e)
                {
                    // do nothing
                }
            }
            
            if (resultSet != null)
            {
                try
                {
                    resultSet.close();
                }
                catch (Exception e)
                {
                    // do nothing
                }
            }
        }
        return result;
    }*/
    
  public ArrayList<String> getCity()
    {
    	final String sql = "SELECT NAME FROM CITY";
    	final ArrayList<String> result = new ArrayList();
    	
    	Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
    	
    	try
        {
    	  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
          String dburl ="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=broker.mdb";//此为NO-DSN方式
          //String dburl ="jdbc:odbc:odbcName";//此为ODBC连接方式
          connection=DriverManager.getConnection(dburl);
          statement=connection.createStatement();
          resultSet=statement.executeQuery(sql);
          while(resultSet.next())
          {
           result.add(resultSet.getString("NAME"));
          }	
    	}
    	catch(Exception ex)
    	{
    	   ex.printStackTrace();	
    	}
    	finally
        {
            if (connection != null)
            {
                try
                {
                    connection.close();
                }
                catch (Exception e)
                {
                    // do nothing
                }
            }
            
            if (statement != null)
            {
                try
                {
                    statement.close();
                }
                catch (Exception e)
                {
                    // do nothing
                }
            }
            
            if (resultSet != null)
            {
                try
                {
                    resultSet.close();
                }
                catch (Exception e)
                {
                    // do nothing
                }
            }
        
    }	
    return result;
  }
  
  public ArrayList<String> getHotel(String city)
    {
    	//String selectedCity = city;
    	final String sql = "SELECT "+
    		               "HOTEL "+
    		               "FROM HOTEL "+ 
    		               "WHERE CITY=?";
    	final ArrayList<String> result = new ArrayList();
    	
    	Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
    	
    	try
        {
    	  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
          String dburl ="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=broker.mdb";//此为NO-DSN方式
          //String dburl ="jdbc:odbc:odbcName";//此为ODBC连接方式
          connection=DriverManager.getConnection(dburl);
          //statement=connection.createStatement();
          //resultSet=statement.executeQuery(sql);
          
          statement = connection.prepareStatement(sql);
          statement.setString(1, city);
            
          resultSet = statement.executeQuery();
          
          while(resultSet.next())
          {
           result.add(resultSet.getString("HOTEL"));
          }	
    	}
    	catch(Exception ex)
    	{
    	   ex.printStackTrace();	
    	}
    	finally
        {
            if (connection != null)
            {
                try
                {
                    connection.close();
                }
                catch (Exception e)
                {
                    // do nothing
                }
            }
            
            if (statement != null)
            {
                try
                {
                    statement.close();
                }
                catch (Exception e)
                {
                    // do nothing
                }
            }
            
            if (resultSet != null)
            {
                try
                {
                    resultSet.close();
                }
                catch (Exception e)
                {
                    // do nothing
                }
            }
        
    }	
    return result;
  }
  
  public int getPortNumber(String city,String hotel)
  {
  	int result = 0;
  	
  	Hashtable<String, Integer> ht_sydney = new Hashtable<String, Integer>();
	ht_sydney.put("Hilton",6000);
	ht_sydney.put("hyatt",6001);
	ht_sydney.put("daysinn",6002);
	
	Hashtable<String, Integer> ht_melbourne = new Hashtable<String, Integer>();
    ht_melbourne.put("Hilton",5000);
    ht_melbourne.put("Peninsula",5001);
    ht_melbourne.put("Starwood",5002);
    
    if(city.equals("Sydney"))
    {
     result = (int)ht_sydney.get(hotel);
    }
    if(city.equals("Melbourne"))
    {
     result = (int)ht_melbourne.get(hotel);
    }
    
    return result;
  }
  
}

⌨️ 快捷键说明

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