473,385 Members | 2,274 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 2269
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

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

Similar topics

2
by: Jon Dellaria | last post by:
I have been using MySql as the database using JSP's and JavaBeans but recently I have wanted to start using the database connection pooling mechanism built into TomCat. I think I am having a...
1
by: Droolboy | last post by:
Hi, I'm about to start building a simple invoicing system for internal use, and am leaning towards using Java in some form. Thing is, there are so many ways it could be done, I'm having a hard...
5
by: mr.iali | last post by:
Hi Everyone I would like to get into software developent using a programming language like c++, java or pl/sql for oracle. I have no idea where to start from. Which language is there more...
19
by: Davey | last post by:
Which is typically faster - a Java server application or a C++ server application?
0
by: cwho.work | last post by:
Hi! We are using apache ibatis with our MySQL 5.0 database (using innodb tables), in our web application running on Tomcat 5. Recently we started getting a number of errors relating to...
21
by: nihad.nasim | last post by:
Hi there, I have a database in Access that I need on the web. The web page should connect to the database and write records for certain tables and view records for others. I want to know a...
5
by: byahne | last post by:
We just went live today with a production SQL Server 2005 database running with our custom Java application. We are utilizing the jTDS open source driver. We migrated our existing application...
8
by: Ananthu | last post by:
Hi I have done all the codings part for connecting mysql with java in eclipse environment. Coding Part: import java.sql.Connection; import java.sql.DriverManager; public class...
6
by: zaina | last post by:
hi everybody i am nwebie in this forum but i think it is useful for me and the member are helpful my project is about connecting client with the server to start exchanging messages between...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.