I need help with my Java application Database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • crystal2005
    New Member
    • Apr 2007
    • 44

    I need help with my Java application Database

    Hello guys, I'm a beginner in Java application programming. I started to write a Java application in which link to MS Access database.

    I encountered a problem in deletion function. E.g. I would like to delete one record in database, it always shows "record not found" in my program, even if the data has been deleted.

    I tried to used function for each choices. But the compiler showed that we can't used function in static void. Is there anyway to use function in public static?

    One more thing, as a beginner in Java programming, i need a comment about my program. Thank you very much guys.

    Code:
    import java.io.*;
    import java.sql.*;
    
    public class DBMS
    {
    	public static void main(String args[]) throws Exception
    	{
    		int select;
    
    		BufferedReader choice1 = new BufferedReader (new InputStreamReader(System.in));
    		BufferedReader choice2 = new BufferedReader (new InputStreamReader(System.in));
    		BufferedReader entry1 = new BufferedReader (new InputStreamReader(System.in));
    		BufferedReader entry2 = new BufferedReader (new InputStreamReader(System.in));
    		BufferedReader entry3 = new BufferedReader (new InputStreamReader(System.in));
    		BufferedReader entry4 = new BufferedReader (new InputStreamReader(System.in));
    
    		String URL, Query1, Query2;
    		Connection connection;
    		PreparedStatement preparedStatement;
    
    		URL = "jdbc:odbc:Staff";
    		Query1 = "INSERT INTO Emp VALUES(?,?,?)";
    		Query2 = "DELETE FROM Emp WHERE EmpNo = ?";
    
    		do
    		{
    			System.out.println("---------------------------------");
    			System.out.println("|      * * * M E N U * * *      |");
    			System.out.println("---------------------------------");
    			System.out.println("| 1 | New Employee Registration |");
    			System.out.println("| 2 | Employee Resignation      |");
    			System.out.println("| 3 | Quit Program              |");
    			System.out.println("---------------------------------");
    
    			System.out.print("Please Input the choice = ");
    			select = Integer.parseInt(choice1.readLine());
    
    			if(select==1)
    			{
    				System.out.println("Please fill up the particular data as the following");
    
    				System.out.print("Employee No: ");
    				int entryA = Integer.parseInt(entry1.readLine());
    
    				System.out.print("Employee Name: ");
    				String entryB = entry2.readLine();
    
    				System.out.print("Employee Salary: ");
    				double entryC = Double.valueOf(entry3.readLine()).doubleValue();
    
    				try
    				{
    					Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    					connection = DriverManager.getConnection(URL,"","");
    
    					preparedStatement = connection.prepareStatement(Query1);
    					preparedStatement.setInt(1,entryA);
    					preparedStatement.setString(2,entryB);
    					preparedStatement.setDouble(3,entryC);
    
    					preparedStatement.executeUpdate();
    
    					System.out.println("RECORD INSERTED SUCCESSFULLY");
    
    					preparedStatement.close();
    					connection.close();
    
    				}catch(Exception e) { System.out.println("Error Occured"); }
    			}
    
    			if(select==2)
    			{
    				int delete=0,loop;
    
    				System.out.println("Employee Resignation");
    				System.out.print("Employee No: ");
    				int entryD = Integer.parseInt(entry4.readLine());
    
    				loop=1;
    				while(loop==1)
    				{
    					System.out.println("Are you sure want to delete " +entryD+ "? (1) to continue | (2) to cancel");
    					System.out.print("Choice = ");
    					int choicedel = Integer.parseInt(choice2.readLine());
    
    					if(choicedel==1)
    					{
    
    						try
    						{
    							Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    							connection = DriverManager.getConnection(URL,"","");
    
    							preparedStatement = connection.prepareStatement(Query2);
    							preparedStatement.setInt(1,entryD);
    
    							preparedStatement.executeUpdate();
    
    							if(delete!=0) // Problem in this line
    							{
    								System.out.println("Employee with EmpNo " +entryD+ " has been deleted");
    							}
    							else
    							{
    								System.out.println("Record not found");
    							}
    
    							preparedStatement.close();
    							connection.close();
    						}catch(Exception e) { System.out.println("Error Occured"); }
    						break;
    					}
    
    					if(choicedel==2)
    					{
    						System.out.println("Resignation has been canceled");
    						break;
    					}
    
    					else
    					{
    						System.out.println("Invalid, please refer to the selection");
    						loop=1;
    					}
    				}
    			}
    		}while(select!=3);
    	}
    }
    http://upload2.net/page/download/LVZBO5gb7kMJdY5/Staff.mdb.html
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by crystal2005
    Hello guys, I'm a beginner in Java application programming. I started to write a Java application in which link to MS Access database.

    I encountered a problem in deletion function. E.g. I would like to delete one record in database, it always shows "record not found" in my program, even if the data has been deleted.

    I tried to used function for each choices. But the compiler showed that we can't used function in static void. Is there anyway to use function in public static?

    One more thing, as a beginner in Java programming, i need a comment about my program. Thank you very much guys.

    Code:
    import java.io.*;
    import java.sql.*;
     
    public class DBMS
    {
    	public static void main(String args[]) throws Exception
    	{
    		int select;
     
    		BufferedReader choice1 = new BufferedReader (new InputStreamReader(System.in));
    		BufferedReader choice2 = new BufferedReader (new InputStreamReader(System.in));
    		BufferedReader entry1 = new BufferedReader (new InputStreamReader(System.in));
    		BufferedReader entry2 = new BufferedReader (new InputStreamReader(System.in));
    		BufferedReader entry3 = new BufferedReader (new InputStreamReader(System.in));
    		BufferedReader entry4 = new BufferedReader (new InputStreamReader(System.in));
     
    		String URL, Query1, Query2;
    		Connection connection;
    		PreparedStatement preparedStatement;
     
    		URL = "jdbc:odbc:Staff";
    		Query1 = "INSERT INTO Emp VALUES(?,?,?)";
    		Query2 = "DELETE FROM Emp WHERE EmpNo = ?";
     
    		do
    		{
    			System.out.println("---------------------------------");
    			System.out.println("| * * * M E N U * * * |");
    			System.out.println("---------------------------------");
    			System.out.println("| 1 | New Employee Registration |");
    			System.out.println("| 2 | Employee Resignation |");
    			System.out.println("| 3 | Quit Program |");
    			System.out.println("---------------------------------");
     
    			System.out.print("Please Input the choice = ");
    			select = Integer.parseInt(choice1.readLine());
     
    			if(select==1)
    			{
    				System.out.println("Please fill up the particular data as the following");
     
    				System.out.print("Employee No: ");
    				int entryA = Integer.parseInt(entry1.readLine());
     
    				System.out.print("Employee Name: ");
    				String entryB = entry2.readLine();
     
    				System.out.print("Employee Salary: ");
    				double entryC = Double.valueOf(entry3.readLine()).doubleValue();
     
    				try
    				{
    					Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    					connection = DriverManager.getConnection(URL,"","");
     
    					preparedStatement = connection.prepareStatement(Query1);
    					preparedStatement.setInt(1,entryA);
    					preparedStatement.setString(2,entryB);
    					preparedStatement.setDouble(3,entryC);
     
    					preparedStatement.executeUpdate();
     
    					System.out.println("RECORD INSERTED SUCCESSFULLY");
     
    					preparedStatement.close();
    					connection.close();
     
    				}catch(Exception e) { System.out.println("Error Occured"); }
    			}
     
    			if(select==2)
    			{
    				int delete=0,loop;
     
    				System.out.println("Employee Resignation");
    				System.out.print("Employee No: ");
    				int entryD = Integer.parseInt(entry4.readLine());
     
    				loop=1;
    				while(loop==1)
    				{
    					System.out.println("Are you sure want to delete " +entryD+ "? (1) to continue | (2) to cancel");
    					System.out.print("Choice = ");
    					int choicedel = Integer.parseInt(choice2.readLine());
     
    					if(choicedel==1)
    					{
     
    						try
    						{
    							Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    							connection = DriverManager.getConnection(URL,"","");
     
    							preparedStatement = connection.prepareStatement(Query2);
    							preparedStatement.setInt(1,entryD);
     
    							preparedStatement.executeUpdate();
     
    							if(delete!=0) // Problem in this line
    							{
    								System.out.println("Employee with EmpNo " +entryD+ " has been deleted");
    							}
    							else
    							{
    								System.out.println("Record not found");
    							}
     
    							preparedStatement.close();
    							connection.close();
    						}catch(Exception e) { System.out.println("Error Occured"); }
    						break;
    					}
     
    					if(choicedel==2)
    					{
    						System.out.println("Resignation has been canceled");
    						break;
    					}
     
    					else
    					{
    						System.out.println("Invalid, please refer to the selection");
    						loop=1;
    					}
    				}
    			}
    		}while(select!=3);
    	}
    }
    http://upload2.net/page/download/LVZ...Staff.mdb.html

    For starters, you can use only one BufferedReader object to read your input. And for the delete message you have

    if(select==2)
    {
    int delete=0,loop;

    .
    .
    .nowhere here is the value of delete changed
    .
    .
    if(delete!=0) // so at this point delete = 0
    {
    System.out.prin tln("Employee with EmpNo " +entryD+ " has been deleted");
    }
    else
    {
    System.out.prin tln("Record not found");
    }

    Comment

    • crystal2005
      New Member
      • Apr 2007
      • 44

      #3
      How do i use only one BufferedReader object to read the input ??

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Just declare one buffered reader and call it something like input, then all the time you need to get input just call readLine on that same reader object.

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          make one object of ...............

          BufferedReader input = new BuufferedReader (new InputStreamRead er(System.in));

          whenever u want to read a line ... then call input.readLine( )

          best of luck.

          Comment

          • dmjpro
            Top Contributor
            • Jan 2007
            • 2476

            #6
            one co-incidence happened here.......

            when i was answering this question .... then no answer was of the second question.

            but when i finished answering then i saw r035198x already answered the second question.

            and the both answer is same ...... goood co-incidence.....

            i m enjoying this site much more than before .......

            best of luck crystal2005 again.

            Comment

            • crystal2005
              New Member
              • Apr 2007
              • 44

              #7
              Thank you guys for your help. Those are very useful :D

              Comment

              Working...