469,646 Members | 1,766 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,646 developers. It's quick & easy.

I need help with my Java application Database

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.

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.sql.*;
  3.  
  4. public class DBMS
  5. {
  6.     public static void main(String args[]) throws Exception
  7.     {
  8.         int select;
  9.  
  10.         BufferedReader choice1 = new BufferedReader (new InputStreamReader(System.in));
  11.         BufferedReader choice2 = new BufferedReader (new InputStreamReader(System.in));
  12.         BufferedReader entry1 = new BufferedReader (new InputStreamReader(System.in));
  13.         BufferedReader entry2 = new BufferedReader (new InputStreamReader(System.in));
  14.         BufferedReader entry3 = new BufferedReader (new InputStreamReader(System.in));
  15.         BufferedReader entry4 = new BufferedReader (new InputStreamReader(System.in));
  16.  
  17.         String URL, Query1, Query2;
  18.         Connection connection;
  19.         PreparedStatement preparedStatement;
  20.  
  21.         URL = "jdbc:odbc:Staff";
  22.         Query1 = "INSERT INTO Emp VALUES(?,?,?)";
  23.         Query2 = "DELETE FROM Emp WHERE EmpNo = ?";
  24.  
  25.         do
  26.         {
  27.             System.out.println("---------------------------------");
  28.             System.out.println("|      * * * M E N U * * *      |");
  29.             System.out.println("---------------------------------");
  30.             System.out.println("| 1 | New Employee Registration |");
  31.             System.out.println("| 2 | Employee Resignation      |");
  32.             System.out.println("| 3 | Quit Program              |");
  33.             System.out.println("---------------------------------");
  34.  
  35.             System.out.print("Please Input the choice = ");
  36.             select = Integer.parseInt(choice1.readLine());
  37.  
  38.             if(select==1)
  39.             {
  40.                 System.out.println("Please fill up the particular data as the following");
  41.  
  42.                 System.out.print("Employee No: ");
  43.                 int entryA = Integer.parseInt(entry1.readLine());
  44.  
  45.                 System.out.print("Employee Name: ");
  46.                 String entryB = entry2.readLine();
  47.  
  48.                 System.out.print("Employee Salary: ");
  49.                 double entryC = Double.valueOf(entry3.readLine()).doubleValue();
  50.  
  51.                 try
  52.                 {
  53.                     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  54.                     connection = DriverManager.getConnection(URL,"","");
  55.  
  56.                     preparedStatement = connection.prepareStatement(Query1);
  57.                     preparedStatement.setInt(1,entryA);
  58.                     preparedStatement.setString(2,entryB);
  59.                     preparedStatement.setDouble(3,entryC);
  60.  
  61.                     preparedStatement.executeUpdate();
  62.  
  63.                     System.out.println("RECORD INSERTED SUCCESSFULLY");
  64.  
  65.                     preparedStatement.close();
  66.                     connection.close();
  67.  
  68.                 }catch(Exception e) { System.out.println("Error Occured"); }
  69.             }
  70.  
  71.             if(select==2)
  72.             {
  73.                 int delete=0,loop;
  74.  
  75.                 System.out.println("Employee Resignation");
  76.                 System.out.print("Employee No: ");
  77.                 int entryD = Integer.parseInt(entry4.readLine());
  78.  
  79.                 loop=1;
  80.                 while(loop==1)
  81.                 {
  82.                     System.out.println("Are you sure want to delete " +entryD+ "? (1) to continue | (2) to cancel");
  83.                     System.out.print("Choice = ");
  84.                     int choicedel = Integer.parseInt(choice2.readLine());
  85.  
  86.                     if(choicedel==1)
  87.                     {
  88.  
  89.                         try
  90.                         {
  91.                             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  92.                             connection = DriverManager.getConnection(URL,"","");
  93.  
  94.                             preparedStatement = connection.prepareStatement(Query2);
  95.                             preparedStatement.setInt(1,entryD);
  96.  
  97.                             preparedStatement.executeUpdate();
  98.  
  99.                             if(delete!=0) // Problem in this line
  100.                             {
  101.                                 System.out.println("Employee with EmpNo " +entryD+ " has been deleted");
  102.                             }
  103.                             else
  104.                             {
  105.                                 System.out.println("Record not found");
  106.                             }
  107.  
  108.                             preparedStatement.close();
  109.                             connection.close();
  110.                         }catch(Exception e) { System.out.println("Error Occured"); }
  111.                         break;
  112.                     }
  113.  
  114.                     if(choicedel==2)
  115.                     {
  116.                         System.out.println("Resignation has been canceled");
  117.                         break;
  118.                     }
  119.  
  120.                     else
  121.                     {
  122.                         System.out.println("Invalid, please refer to the selection");
  123.                         loop=1;
  124.                     }
  125.                 }
  126.             }
  127.         }while(select!=3);
  128.     }
  129. }
  130.  
  131.  
http://upload2.net/page/download/LVZBO5gb7kMJdY5/Staff.mdb.html
Apr 26 '07 #1
6 2010
r035198x
13,262 8TB
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.

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.sql.*;
  3.  
  4. public class DBMS
  5. {
  6.     public static void main(String args[]) throws Exception
  7.     {
  8.         int select;
  9.  
  10.         BufferedReader choice1 = new BufferedReader (new InputStreamReader(System.in));
  11.         BufferedReader choice2 = new BufferedReader (new InputStreamReader(System.in));
  12.         BufferedReader entry1 = new BufferedReader (new InputStreamReader(System.in));
  13.         BufferedReader entry2 = new BufferedReader (new InputStreamReader(System.in));
  14.         BufferedReader entry3 = new BufferedReader (new InputStreamReader(System.in));
  15.         BufferedReader entry4 = new BufferedReader (new InputStreamReader(System.in));
  16.  
  17.         String URL, Query1, Query2;
  18.         Connection connection;
  19.         PreparedStatement preparedStatement;
  20.  
  21.         URL = "jdbc:odbc:Staff";
  22.         Query1 = "INSERT INTO Emp VALUES(?,?,?)";
  23.         Query2 = "DELETE FROM Emp WHERE EmpNo = ?";
  24.  
  25.         do
  26.         {
  27.             System.out.println("---------------------------------");
  28.             System.out.println("| * * * M E N U * * * |");
  29.             System.out.println("---------------------------------");
  30.             System.out.println("| 1 | New Employee Registration |");
  31.             System.out.println("| 2 | Employee Resignation |");
  32.             System.out.println("| 3 | Quit Program |");
  33.             System.out.println("---------------------------------");
  34.  
  35.             System.out.print("Please Input the choice = ");
  36.             select = Integer.parseInt(choice1.readLine());
  37.  
  38.             if(select==1)
  39.             {
  40.                 System.out.println("Please fill up the particular data as the following");
  41.  
  42.                 System.out.print("Employee No: ");
  43.                 int entryA = Integer.parseInt(entry1.readLine());
  44.  
  45.                 System.out.print("Employee Name: ");
  46.                 String entryB = entry2.readLine();
  47.  
  48.                 System.out.print("Employee Salary: ");
  49.                 double entryC = Double.valueOf(entry3.readLine()).doubleValue();
  50.  
  51.                 try
  52.                 {
  53.                     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  54.                     connection = DriverManager.getConnection(URL,"","");
  55.  
  56.                     preparedStatement = connection.prepareStatement(Query1);
  57.                     preparedStatement.setInt(1,entryA);
  58.                     preparedStatement.setString(2,entryB);
  59.                     preparedStatement.setDouble(3,entryC);
  60.  
  61.                     preparedStatement.executeUpdate();
  62.  
  63.                     System.out.println("RECORD INSERTED SUCCESSFULLY");
  64.  
  65.                     preparedStatement.close();
  66.                     connection.close();
  67.  
  68.                 }catch(Exception e) { System.out.println("Error Occured"); }
  69.             }
  70.  
  71.             if(select==2)
  72.             {
  73.                 int delete=0,loop;
  74.  
  75.                 System.out.println("Employee Resignation");
  76.                 System.out.print("Employee No: ");
  77.                 int entryD = Integer.parseInt(entry4.readLine());
  78.  
  79.                 loop=1;
  80.                 while(loop==1)
  81.                 {
  82.                     System.out.println("Are you sure want to delete " +entryD+ "? (1) to continue | (2) to cancel");
  83.                     System.out.print("Choice = ");
  84.                     int choicedel = Integer.parseInt(choice2.readLine());
  85.  
  86.                     if(choicedel==1)
  87.                     {
  88.  
  89.                         try
  90.                         {
  91.                             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  92.                             connection = DriverManager.getConnection(URL,"","");
  93.  
  94.                             preparedStatement = connection.prepareStatement(Query2);
  95.                             preparedStatement.setInt(1,entryD);
  96.  
  97.                             preparedStatement.executeUpdate();
  98.  
  99.                             if(delete!=0) // Problem in this line
  100.                             {
  101.                                 System.out.println("Employee with EmpNo " +entryD+ " has been deleted");
  102.                             }
  103.                             else
  104.                             {
  105.                                 System.out.println("Record not found");
  106.                             }
  107.  
  108.                             preparedStatement.close();
  109.                             connection.close();
  110.                         }catch(Exception e) { System.out.println("Error Occured"); }
  111.                         break;
  112.                     }
  113.  
  114.                     if(choicedel==2)
  115.                     {
  116.                         System.out.println("Resignation has been canceled");
  117.                         break;
  118.                     }
  119.  
  120.                     else
  121.                     {
  122.                         System.out.println("Invalid, please refer to the selection");
  123.                         loop=1;
  124.                     }
  125.                 }
  126.             }
  127.         }while(select!=3);
  128.     }
  129. }
  130.  
  131.  
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.println("Employee with EmpNo " +entryD+ " has been deleted");
}
else
{
System.out.println("Record not found");
}
Apr 26 '07 #2
How do i use only one BufferedReader object to read the input ??
Apr 27 '07 #3
r035198x
13,262 8TB
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.
Apr 27 '07 #4
dmjpro
2,476 2GB
make one object of ...............

BufferedReader input = new BuufferedReader(new InputStreamReader(System.in));

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

best of luck.
Apr 27 '07 #5
dmjpro
2,476 2GB
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.
Apr 27 '07 #6
Thank you guys for your help. Those are very useful :D
Apr 27 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Jon Dellaria | last post: by
1 post views Thread by Droolboy | last post: by
19 posts views Thread by Davey | last post: by
21 posts views Thread by nihad.nasim | last post: by
6 posts views Thread by zaina | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.