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. -
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
6 2269
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. -
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.println("Employee with EmpNo " +entryD+ " has been deleted");
}
else
{
System.out.println("Record not found");
}
How do i use only one BufferedReader object to read the input ??
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.
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.
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.
Thank you guys for your help. Those are very useful :D
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
by: Davey |
last post by:
Which is typically faster - a Java server application or a C++ server
application?
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
| |