Bulletin Board – Implementation

Bulletin Board – Implementation

     Introduction:

                  Bulletin Board application provides group communication between different group members. Here we are maintaining different groups, and each group is having two or more members. With in a particular group any member can post the message which can be read by all the group members and can read the message posted by other group members. Here we are maintaining separate message table for every group.

Implementation:

    à Create the database and tables as shown below.
    à Establish the connection to the database using JDBC from the program.
    à Insert al the messages posted by particular group members into its corresponding table.
    à If any group member want to read the message, extract all messages from its group table and display   
         them.
Create a database called “BULLETIN” and then create the following tables in it:




bulletin.java


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

public class bulletin   {
   static Connection con;
   static Statement st;
   static ResultSet rs;

   public static void main(String[] args)throws IOException   {
      try   {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con=DriverManager.getConnection("jdbc:odbc:BULLETIN");
            st=con.createStatement();
      }
      catch(ClassNotFoundException ex)  {
            ex.printStackTrace();
      }
      catch(SQLException ex)  {
            ex.printStackTrace();
      }
      if(con!=null)  {
            System.out.println("Connected to server...\n");
            System.out.println("Please Enter your group name: \n");
            try  {
               rs=st.executeQuery("Select * from groups");
               while(rs.next())  {
                        System.out.println(rs.getString("Group"));
               }
            }
            catch(SQLException ex)  {
               ex.printStackTrace();
            }
            String grpname;
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            grpname=br.readLine();
           
            System.out.println("Enter username: \t");
            String usr=br.readLine();
            System.out.println("\nEnter password: \t");
            String pass=br.readLine();
            try  {
               String query="select * from "+grpname.trim()+"Mem where Member='"+usr+"'";
               System.out.println(query);
               rs=st.executeQuery(query);
               while(rs.next())  {
                  if(rs.getString("passwd").equals(pass))  {
                        System.out.println("Login Successful!!!HAVE A NICE DAY!!!");
                        while(true)  {
                           System.out.println("Enter your choice: \n");
                           System.out.println("1. Read messages \n");
                           System.out.println("2. Write messages \n");
                           System.out.println("3. Logout \n");

                           int option=Integer.parseInt(br.readLine());
                           switch(option)  {
                            case 1:
                                    query="select * from "+grpname+ "Msg";
                                    System.out.println(query);
                                    rs=st.executeQuery(query);
                                    while(rs.next())
                                       System.out.println(rs.getString("user")+":"+rs.getString("Message")+"\n\n");
                                    break;
                           case 2:
                                    System.out.println("Enter your message: \n");
                                    String msg=br.readLine();
                                    query="insert into "+grpname+"Msg"+"values('"+msg+"','"+usr+"')";
                                    System.out.println(query);
                                    st.executeUpdate(query);
                                    break;
                           case 3:
                                    System.exit(1);
                           default:
                                    System.out.println("Choose a valid option!!! \n");
                                    break;
                           }
                        }
                  }
                  else  {
                        System.out.println("Please login again!!!\n");
                        System.exit(1);
                  }
               }
            }
            catch(SQLException ex)
            {  ex.printStackTrace();   }
      }
   }
}






Bulletin Board: OUTPUT
C:\Documents and Settings\cse4y>e:
E:\>cd Lab
E:\Lab>cd bulletin
E:\Lab\bulletin>javac *.java
E:\Lab\bulletin>java bulletin
Connected to server...
Please Enter your group name:
Group1
Group2
Group1
Enter username:
abc
Enter password:
abc123
select * from Group1Mem where Member='abc'
Login successful!!!HAVE A NICE DAY!!!
Enter your choice:
1. Read mesages
2. Write message
3. Logout
1
select * from Group1Msg
abc:Hello
xyz:Hi all!