SlideShare a Scribd company logo
HND in Computing & Programming in
System Development Java
1
Contents
Task1.....................................................................................................................................................................................................3
1.1. Briefly explain the principles, characteristics features and in Java programming. ....................................................................3
1.2 Explain environmental flexibility of programming in Java programming and evaluate your answer........................................5
Task 02..................................................................................................................................................................................................6
2.1. Design a database with necessary tables....................................................................................................................................6
Normalization....................................................................................................................................................................................6
2.2 Set up the database structure by using suitable RDBMS............................................................................................................9
2.2 b) Populatethe database with suitable test data........................................................................................................................13
Task 03................................................................................................................................................................................................16
3.1. The designs of all the required algorithms are required as part of this task. You can use suitable design techniques ............16
Activity diagram for student registration in Propose System......................................................................................................17
Activity Diagram For record the Payment details in Propose System........................................................................................18
Flow chart for check the validity of users to usethe data in the propose program.....................................................................19
Use case diagram for proposed system .......................................................................................................................................20
3.2. Design necessary user interfaces Login Form..........................................................................................................................21
Main Form..................................................................................................................................................................................21
Student Details form....................................................................................................................................................................22
Add New Student Details Form ..................................................................................................................................................22
Edit Student Details Form...........................................................................................................................................................23
Course Details Form....................................................................................................................................................................23
Add New course Details form.....................................................................................................................................................24
Edit course details Form..............................................................................................................................................................24
Batch Details Form......................................................................................................................................................................25
Add New Batch Details Form.....................................................................................................................................................25
Edit Batch Details........................................................................................................................................................................26
Create New User Account Form.................................................................................................................................................26
Edit User Account .......................................................................................................................................................................27
Payment Details Form.................................................................................................................................................................27
Receive Payments Form..............................................................................................................................................................28
Task 04................................................................................................................................................................................................29
4.1. Using java programming language, implement the Student information system for the HE Institute designed in Task 03. ..29
Login Form......................................................................................................................................................................................29
Main Form.......................................................................................................................................................................................36
Edit Student Details Form ...........................................................................................................................................................69
Course Details Form........................................................................................................................................................................79
Add Course detail Form ..................................................................................................................................................................91
Edit Course Details Form.............................................................................................................................................................102
4.2. Provide evidence and solutions for error handling during software implementation. ..........................................................191
HND in Computing & Programming in
System Development Java
2
Use “Try catch” method...........................................................................................................................................................191
Validate text fields....................................................................................................................................................................192
Task 5...............................................................................................................................................................................................194
5.1. Carry out complete systemtestingand provide user guide. Systemtesting and user guide must include suitable screen shots.
Test your complete project with suitable data. ............................................................................................................................194
Name=Admin password=admin) .................................................................................................................................................194
5.2. Produce suitable screenshots and solutions for error handling during testing. .....................................................................217
When creating new user account..............................................................................................................................................217
When Login to system..................................................................................................................................................................218
When Updating a Student detail…...............................................................................................................................................219
5.3. Include feedbacks on provided java solution (surveys, questionnaire, analyzefeedback and present results), evaluate all and
provide summery report...............................................................................................................................................................220
Required Improvements ...............................................................................................................................................................222
Task 06.............................................................................................................................................................................................223
6.1. Produce a publishable working copy a compiled version of thecompleted assignment together with softwareinstallation
notes, recommendations and future improvements. The installation note should include the system requirements ..................223
Hardware requirements ................................................................................................................................................................223
Software Requirements ................................................................................................................................................................223
Installation Note.......................................................................................................................................................................223
Select the installation Path and click on next button...............................................................................................................226
HND in Computing & Programming in
System Development Java
3
Task1
1.1. Briefly explain the principles, characteristics features and in Java
programming.
Java is an object oriented and architecture-neutral programming language. This agrees
programs created in Java to be ported into different platform without the need for
modifications. As a beginner to the JAVA programming language a person must want to get
the knowledge about what are the principals in java programming language, what are the
characteristics in JAVA programming language and what are the features in Java programming
language.
Basically we can identify there are five principals in java programming language.
• Simple, object oriented and familiar language – it‟s like greatly in its
consistence. Once you learn had to do one thing in Java, you know how to do same
way throughout the language. Because it never deviates from the way the languages
Architecture. It‟s an object oriented language.so once you understand the principals
of encapsulation, inheritance and polymorphism and how those are implemented in
the java programming language you have a much better sense of how to Architect
your Applications.
• Robust and secure – its robustness likes greatly in its object oriented
characteristics. Because you designing everything as an object. Everything has
methods or functions and properties. Also known as fields. And you creating an
application by combining multiple classes together. This let you create your code
in small trunks. And it makes easy to maintain and debug your applications
overtime.
• Architecture- neutral and portable – java was designed to be portable.so that it
able to be compiled once.
• High- performance – Java was created to be high performance the Original version
of the Java virtual machine wasn‟t faster c++ applications. But over the years it‟s
been improved normally. And today justness fast sometimes even faster. Then the
Applications built in c++.
• Interpreted, threaded and dynamic
In above paragraph briefly discussed about the Principals in java programming language
.after getting knowledge about principals a beginner must have to get the knowledge about
what are the characteristics and features in Java programming language. Because it is
helping to understand why we are saying java is an object oriented programing language
and some other sort of things.
HND in Computing & Programming in
System Development Java
4
Characteristics and features of java programming
• Java Is Simple – Java is partially modeled on c++ but greatly simplified and
improved some people refer to java as “C++”because it is like C++but with more
functionality and fewer negative aspects.
• Java Is object- oriented – Java is a fully object oriented language. It supports all
the characteristics needed to be object oriented. In the Java everything is treated as
objects to which methods are applied. As the languages like Objective C, C++
fulfills the above four characteristics yet they are not fully object oriented languages
because they are structured as well as object oriented languages. But in case of java,
it is a fully Object Oriented language because object is at the outer most level of
data structure in java. No standalone methods, constants, and variables are there in
java. Everything in java is object even the primitive data types can also be converted
into object by using the wrapper class. Once of the issue in software development
is how to reprocess code. Object oriented programming provides great Flexibility,
modularity, clarity and reusability trough polymorphism, inheritance and
encapsulation.
• Java is distributed – Distributed computing involves several computers working
together on a network.java is formed to make distributed computing case. Since
networking capability is inherently integrated into java, writing network programs
is like delivering and receiving data to and from a file.
• Java is Platform Independent - Java provides the facility to "Write once -Run
anywhere"(Known as platform independent). Not even a single language is idle to
this feature but java is closer to this feature. Java Provide the facility of cross
platform programs by compiling in intermediate code known as byte code. This
byte code can be interpreted on any system which has Java Virtual Machine (JVM).
• Java is interpreted – you need an interpreter to run java programs. The programs
are compiled to the java virtual machine known as byte code. The byte code is
machine independent and can run on any machine that has a java interpreter, which
is piece of the java virtual machine.
• Java is Robust – java has a runtime exception-handling feature o provide
programming support for robustness.
• Java is secure – its mean is java implements several security mechanisms to
safeguard your system against harm caused stray programs.
• Java is architecture –neutral – it mean is you can write one program that will run
on any platform
• Java is portable –because java is architecture neutral, java programs are portable.
They can be run on any platform without being recompiled.
Those are the main features, Characteristics and principal in java programming language.
HND in Computing & Programming in
System Development Java
5
1.2 Explain environmental flexibility of programming in Java programming and
evaluate your answer.
Java was designed to allow application programs to be built that could be run on any platform
without having to be rewritten or recompiled by the programmer for each separate platform. Java
virtual machine (JVM), an implementation of the Java Virtual Machine Specification, interprets
compiled Java byte code for a computer's processor or hardware platform so that it can execute a
Java program's commands. The Java Virtual Machine Specification defines an abstract rather than
a real machine or processor. The Specification specifies an instruction set, a set of registers, a stack,
a "garbage heap," and a method area. A Java virtual machine makes this possible because it is
aware of the specific instruction lengths and other particularities of the platform. Once a Java
virtual machine has been implemented for a given platform, any Java program which, after
compilation, the byte code can run on that platform. A Java virtual machine can either interpret the
byte code one instruction at a time or the byte code can be compiled further for the real processor
using what is called a just-in-time compiler. The one of main advantage of the Java VM ,it is allow
to a program to be written and compiled only once, which then can be run on a wide variety of
systems and operating systems without modification. Many cell phones and embedded devices
include a Java VM. Another Since programming must be translated from generic "byte code" to
the machine code for the target system as it is being run, it is impossible for Java to perform as
quickly as languages that can compile directly to machine code for the target systems. Because the
Java VM must run on a wide variety of systems, features specific to one OS are often not
implemented into Java programs. In addition, the "look and feel" of Java applications can often be
quite different than the default styles of native applications within an operating system.
HND in Computing & Programming in
System Development Java
6
Task 02
2.1. Design a database with necessary tables.
Normalization
UNF
HE Institute{ Student ID, Name, Age, Gender, Telephone no, Course ID, Course Name, Course
Fee, Duration, Batch ID, Start date, number Of Student, End date, Payment code, Payment type,
Amount, Staff ID, Staff Name, User Name, Password , Account Type}
1NF
Student{ Student ID, Name, Age, Gender, Telephone no }
HE Institute{ Course ID, Course Name, Course Fee, Duration, Batch ID, Start date, number Of
Student, End date, Payment code, Payment type, Amount, Staff ID, Staff Name, User Name,
Password , Account Type, Student ID}
2NF
Student{ Student ID, Name, Age, Gender, Telephone no }
Course { Course ID, Course Name, Course Fee, Duration, Student ID }
HE Institute{ Batch ID, Start date, number Of Student, End date, Payment code, Payment type,
Amount, Staff ID, Staff Name, User Name, Password , Account Type, Student ID}
3NF
Student{ Student ID, Name, Age, Gender, Telephone no }
Course { Course ID, Course Name, Course Fee, Duration, Student ID }
Batch{ Batch ID, Start date, number Of Student, End date, Student ID, Course ID }
Payment{ Payment code, Payment type, Amount, Student ID , Course ID }
Staff{ Staff ID, Staff Name, User Name, Password , Account Type, Student ID } HE
Institute{ Student ID , Course ID , Batch ID , Payment code, Staff ID}
BCNF
Student{ Student ID, Name, Age, Gender, Telephone no }
Course { Course ID, Course Name, Course Fee, Duration, Student ID }
Batch{ Batch ID, Start date, number Of Student, End date, Student ID, Course ID }
Payment{ Payment code, Payment type, Amount, Student ID , Course ID }
Staff{ Staff ID, Staff Name, User Name, Password , Account Type, Student ID }
HND in Computing & Programming in
System Development Java
7
Student_Course{ Student ID , Course ID }
Student_Batch{ Student ID , Batch ID }
Student_Staff{{ Student ID , Staff ID }
HND in Computing & Programming in
System Development Java
8
HND in Computing & Programming in
System Development Java
9
2.2 Set up the database structure by using suitable RDBMS.
HND in Computing & Programming in
System Development Java
10
Table –Staff
Figure 1
Table_student
Figure 2
Table- student_Staff
Figure 3
-
HND in Computing & Programming in
System Development Java
11
Table-Batch
Figure 4
Table-Student_Batch
Figure 5
Table-Course
Figure 6
HND in Computing & Programming in
System Development Java
12
Table – Student_Course
Figure 7
Table- Payment
Figure 8
HND in Computing & Programming in
System Development Java
13
2.2 b) Populate the database with suitable test data.
Table-Staff
Figure 9
Table -Student
Figure 10
HND in Computing & Programming in
System Development Java
14
Table- Student_Staff
Figure 11
Table-Batch
Figure 12
Table-Student_Batch
Figure 13
HND in Computing & Programming in
System Development Java
15
Table-Course
Figure 14
Table – Student_Course
Figure 15
Table- Payment
Figure 16
HND in Computing & Programming in
System Development Java
16
Task 03
3.1. The designs of all the required algorithms are required as part of this task. You
can use suitable design techniques
Class diagram for proposed system
=
Student
Student_ID
Student_Name
Age
Telephone_no
Payments
Payment_code
Payment_Method
Total Amount
Course
Course_ID
Course_Name
Duration
Course_Fee
Staff
Staff ID
Staff Name
User Name
Password
Account type
*
1 *
1
1
Batch
Batch_ID
Start_date
End Date
number_Of_Student
Course_ID
*
*
*
1*
*
*
HND in Computing & Programming in
System Development Java
17
Activity diagram for student registration in Propose System
Input: Course Details
Student Details
Batch Details
Select a Student
If student Want
to Follow a new
Course
Select a Course
If student want to
follow the
selected course
student
Assign to a Batch
True
False
True
False
HND in Computing & Programming in
System Development Java
18
Activity Diagram For record the Payment details in Propose System
Input: Course Details
Student Details
Payment Details
Select a student and the
course
If student want to
pay a premium of
the course
Select a Payment Method
Receive the payments
Record the payment detail
True
False
HND in Computing & Programming in
System Development Java
19
Flow chart for check the validity of users to use the data in the propose program
Start
Input: User type (Admin or Staff
member) password, user name
Enter the User Name and Password
Check the person is an Admin or a staff
member by comparing user Name and
password
If the
person is
an Admin
Check the person is a staff member
Give the full permission to use the
data in the program
If the
person is
a staff
Stop
Give the limited permission to use
the data in the program
True
False
True
False
Provide an error Message
HND in Computing & Programming in
System Development Java
20
Use case diagram for proposed system
Add Studentdetails
EditStudentdetails
Delete Student Details
Add Course Details
EditCourse details
Face to the Assignmentsand
Exams
Admin
Students
Attend to the classes
Delete Course Details
Add Batch details
Select courses
Do payments
Edit Batch details
Add Payment Details
Staff
Edit Payment Details
Delete Payment Details
Provide student Informations
HND in Computing & Programming in
System Development Java
21
3.2. Design necessary user interfaces Login Form
Figure 17
Main Form
Figure 18
HND in Computing & Programming in
System Development Java
22
Student Details form
Figure 19
Add New Student Details Form
Figure 20
HND in Computing & Programming in
System Development Java
23
Edit Student Details Form
Figure 21
Course Details Form
Figure 22
HND in Computing & Programming in
System Development Java
24
Add New course Details form
Figure 23
Edit course details Form
Figure 24
HND in Computing & Programming in
System Development Java
25
Batch Details Form
Figure 25
Add New Batch Details Form
Figure 26
HND in Computing & Programming in
System Development Java
26
Edit Batch Details
Figure 27
Create New User Account Form
Figure 28
HND in Computing & Programming in
System Development Java
27
Edit User Account
Figure 29
Payment Details Form
Figure 30
HND in Computing & Programming in
System Development Java
28
Receive Payments Form
Figure 31
HND in Computing & Programming in
System Development Java
29
Task 04
4.1. Using java programming language, implement the Student information system
for the HE Institute designed in Task 03.
Login Form
Figure 32
Program code:
/*
* To change this license header, choose License Headers in
Project Properties. * To change this template file, choose Tools |
Templates * and open the template in the editor.
*/
package heinstitute;
import javax.swing.JOptionPane;
import
java.sql.Connection;
import
java.sql.DriverManager;
import
java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import javax.swing.UIManager;
/**
*
HND in Computing & Programming in
System Development Java
30
* @author Anushka
*/
public class Login extends javax.swing.JFrame {
/**
* Creates new form Login
*/
private String staffname;
private boolean privileges;
private int staffid;
public Login() {
initComponents();
}
public boolean checkLogin(String user,String pass) throws ClassNotFoundException,
SQLException
{
// connection to the sql server
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://Anushka-
PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;";
Connection connection = DriverManager.getConnection(connectionUrl);
System.out.println("Successfuly Connected "); // connection ok
// retriving user and password gave by user
String SQL = "SELECT * FROM Staff WHERE User_name='"+user+"'AND
Password='"+pass+"'";
Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery(SQL);
return rs.first(); //returns true if ResultSet has a row valid login
} // end con method
public void getStaffName_privileges(String user,String pass) throws ClassNotFoundException,
SQLException
{
// connection to the sql server
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://Anushka-
HND in Computing & Programming in
System Development Java
31
PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;";
Connection connection = DriverManager.getConnection(connectionUrl);
System.out.println("Successfuly Connected "); // connection ok
// Get staff name
String SQL = "SELECT * FROM Staff WHERE User_name='"+user+"'AND
Password='"+pass+"'";
Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery(SQL);
rs.first();
staffname= rs.getString(2);
privileges=rs.getBoolean(5);
staffid=rs.getInt(1);
} // end con method
public void displayMain(){
//display main interface
Main main = new
Main(staffname,privileges,staffid);
main.setVisible(true);
main.setLocationRelativeTo(null);
main.setResizable(false);
}// end displayMain
public void loginToSystem()throws ClassNotFoundException, SQLException {
String username=txtUser.getText();
String password=txtPass.getText();
if (this.checkLogin(username,password)==true){
this.getStaffName_privileges(username,password);
this.displayMain();
this.setVisible(false);
}//end if
else if(this.checkLogin(username,password)==false){
HND in Computing & Programming in
System Development Java
32
JOptionPane.showMessageDialog(null, " None Authorized Access please enter a valid
User-Name and a Password","Error",JOptionPane.ERROR_MESSAGE);
}//end else
}// end of login to system
/**
* This method is called from within the constructor to
initialize the form. * WARNING: Do NOT modify this code.
The content of this method is always * regenerated by the
Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
txtPass = new
javax.swing.JPasswordField();
jLabel2 = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
txtUser = new javax.swing.JTextField();
btnlogin = new javax.swing.JButton();
btnExit = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
txtPass.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
txtPassActionPerformed(evt);
}
});
jLabel2.setText("Pasword :");
jLabel1.setText("User Name :");
btnlogin.setIcon(new
javax.swing.ImageIcon(getClass().getResource("/heinstitute/login.png"))); // NOI18N
btnlogin.setText("LOGIN"); btnlogin.addActionListener(new
HND in Computing & Programming in
System Development Java
33
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
btnloginActionPerformed(evt);
}
});
btnlogin.addKeyListener(new
java.awt.event.KeyAdapter() { public void
keyPressed(java.awt.event.KeyEvent evt) {
btnloginKeyPressed(evt);
}
});
btnExit.setText("Exit");
btnExit.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
btnExitActionPerformed(evt);
}
});
jLabel3.setIcon(new
javax.swing.ImageIcon(getClass().getResource("/heinstitute/1397005153_Refresh_key.png"))); //
NOI18N jLabel3.setText("jLabel3");
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout); layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
false)
.addComponent(txtPass, javax.swing.GroupLayout.DEFAULT_SIZE, 147,
Short.MAX_VALUE) .addComponent(txtUser)))
.addComponent(btnlogin, javax.swing.GroupLayout.PREFERRED_SIZE, 90,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
HND in Computing & Programming in
System Development Java
34
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 71,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnExit, javax.swing.GroupLayout.PREFERRED_SIZE, 73,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(18, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(23, 23, 23)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(txtUser, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(12, 12, 12)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(txtPass, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(27, 27, 27))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup() .addContainerGap()
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnExit, javax.swing.GroupLayout.PREFERRED_SIZE, 33,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnlogin, javax.swing.GroupLayout.PREFERRED_SIZE, 33,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(19, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void txtPassActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void
btnloginActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here: try{
this.loginToSystem();
HND in Computing & Programming in
System Development Java
35
}//end try
catch (ClassNotFoundException e){
System.out.println(e);
}
catch (SQLException e){
System.out.println(e);
}
}
private void btnloginKeyPressed(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
}
private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here: int result
=JOptionPane.showConfirmDialog(null, "Do you Want To Exit ?", "Exit",
JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_ME
SSAGE); if(result==0){ System.exit(0);
}
else if(result==1){
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable()
{ public void run() {
new Login().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnExit;
private javax.swing.JButton btnlogin;
private javax.swing.JLabel jLabel1;
HND in Computing & Programming in
System Development Java
36
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPasswordField
txtPass; private javax.swing.JTextField
txtUser;
// End of variables declaration
}
Main Form
Figure 33
Program Code :
/*
* To change this license header, choose License Headers in
Project Properties. * To change this template file, choose Tools |
Templates * and open the template in the editor.
*/
package heinstitute; import
java.awt.Frame; import
java.sql.Connection;
import
java.sql.DriverManager;
HND in Computing & Programming in
System Development Java
37
import java.sql.ResultSet;
import
java.sql.SQLException;
import java.sql.Statement;
import
javax.swing.JFrame;
import
javax.swing.JOptionPane;
import
javax.swing.UIManager;
/**
*
* @author Anushka
*/
public class Main extends javax.swing.JFrame {
/**
* Creates new form Main
*/
private boolean privileges;
private int staffid;
private String
S_name;
public Main(String staffname,boolean p,int id) {
initComponents();
lblStaffName.setText(staffname.toUpperCase());
this.setUserLimitations(p);
staffid=id; // set Staff id
S_name=staffname;
}
public void setUserLimitations(boolean prm){
privileges=prm;
menuAddUsers.setEnabled(prm);
}
/**
* This method is called from within the constructor to
initialize the form. * WARNING: Do NOT modify this code.
HND in Computing & Programming in
System Development Java
38
The content of this method is always * regenerated by the
Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
btnCourse = new
javax.swing.JButton(); lblStaffName2
= new javax.swing.JLabel(); btnStudent
= new javax.swing.JButton();
lblStaffName3 = new javax.swing.JLabel();
btnCourse1 = new javax.swing.JButton();
lblStaffName4 = new javax.swing.JLabel();
lblStaffName1 = new javax.swing.JLabel();
lblStaffName = new javax.swing.JLabel();
btnCourse2 = new javax.swing.JButton();
lblStaffName5 = new javax.swing.JLabel();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu3 = new javax.swing.JMenu();
menuAddUsers = new
javax.swing.JMenuItem();
menuEditMyAcc = new
javax.swing.JMenuItem(); menulogout
= new javax.swing.JMenu(); jMenu2 =
new javax.swing.JMenu(); jMenuItem1
= new javax.swing.JMenuItem();
jMenuItem2 = new
javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
btnCourse.setBackground(new java.awt.Color(0, 0, 0));
btnCourse.setIcon(new
javax.swing.ImageIcon(getClass().getResource("/heinstitute/1397062581_Courses.png"))); //
NOI18N
btnCourse.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
btnCourseActionPerformed(evt);
}
});
HND in Computing & Programming in
System Development Java
39
lblStaffName2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
lblStaffName2.setForeground(new java.awt.Color(255, 255, 255));
lblStaffName2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lblStaffName2.setText("Course Details");
btnStudent.setBackground(new java.awt.Color(0, 0, 0));
btnStudent.setIcon(new
javax.swing.ImageIcon(getClass().getResource("/heinstitute/1397061362_elementary_scho
ol.png"))); // NOI18N btnStudent.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
btnStudentActionPerformed(evt);
}
});
lblStaffName3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
lblStaffName3.setForeground(new java.awt.Color(255, 255, 255));
lblStaffName3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lblStaffName3.setText("Student Details");
btnCourse1.setBackground(new java.awt.Color(0, 0, 0));
btnCourse1.setIcon(new
javax.swing.ImageIcon(getClass().getResource("/heinstitute/1397064740_Money.png"))); //
NOI18N
lblStaffName4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
lblStaffName4.setForeground(new java.awt.Color(255, 255, 255));
lblStaffName4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lblStaffName4.setText("Batch Details");
lblStaffName1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
lblStaffName1.setForeground(new java.awt.Color(255, 102, 0));
lblStaffName1.setText("User:");
lblStaffName.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
lblStaffName.setForeground(new java.awt.Color(255, 102, 0));
lblStaffName.setText("jLabel8");
btnCourse2.setBackground(new java.awt.Color(0, 0, 0));
btnCourse2.setIcon
HND in Computing & Programming in
System Development Java
40
(new
javax.swing.ImageIcon(getClass().getResource("/heinstitute/1397132431_MySpace.png"))); //
NOI18N
btnCourse2.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
btnCourse2ActionPerformed(evt);
}
});
lblStaffName5.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
lblStaffName5.setForeground(new java.awt.Color(255, 255, 255));
lblStaffName5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lblStaffName5.setText("Payment Details");
jMenu3.setText("Configuration");
menuAddUsers.setText("Create New User");
menuAddUsers.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
menuAddUsersActionPerformed(evt);
}
});
jMenu3.add(menuAddUsers);
menuEditMyAcc.setText("Edit User Account");
menuEditMyAcc.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
menuEditMyAccActionPerformed(evt);
}
});
jMenu3.add(menuEditMyAcc);
jMenuBar1.add(jMenu3);
menulogout.setText("Log Out");
menulogout.addMouseListener(new
java.awt.event.MouseAdapter() {
HND in Computing & Programming in
System Development Java
41
public void mouseClicked(java.awt.event.MouseEvent evt)
{ menulogoutMouseClicked(evt);
}
});
menulogout.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
menulogoutActionPerformed(evt);
}
});
jMenuBar1.add(menulogout);
jMenu2.setText("Help");
jMenuItem1.setText("Help ");
jMenuItem1.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});
jMenu2.add(jMenuItem1);
jMenuItem2.setText("About");
jMenuItem2.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem2ActionPerformed(evt);
}
});
jMenu2.add(jMenuItem2);
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout); layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
HND in Computing & Programming in
System Development Java
42
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(10, 10, 10)
.addComponent(lblStaffName1)
.addGap(3, 3, 3)
.addComponent(lblStaffName, javax.swing.GroupLayout.PREFERRED_SIZE,
161, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblStaffName3, javax.swing.GroupLayout.PREFERRED_SIZE, 150,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnStudent, javax.swing.GroupLayout.PREFERRED_SIZE,
139, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(btnCourse, javax.swing.GroupLayout.DEFAULT_SIZE, 144,
Short.MAX_VALUE)
.addComponent(lblStaffName2, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(btnCourse1,
javax.swing.GroupLayout.PREFERRED_SIZE, 156,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(btnCourse2,
javax.swing.GroupLayout.PREFERRED_SIZE, 156,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup() .addComponent(lblStaffName5,
javax.swing.GroupLayout.PREFERRED_SIZE, 156,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(lblStaffName4,
javax.swing.GroupLayout.PREFERRED_SIZE, 156,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(8, 8, 8)))))
HND in Computing & Programming in
System Development Java
43
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[]
{btnCourse, btnCourse1, btnStudent, lblStaffName4});
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnStudent, javax.swing.GroupLayout.PREFERRED_SIZE, 217,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnCourse, javax.swing.GroupLayout.PREFERRED_SIZE, 217,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnCourse1, javax.swing.GroupLayout.PREFERRED_SIZE,
217, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnCourse2, javax.swing.GroupLayout.PREFERRED_SIZE,
217, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lblStaffName3)
.addComponent(lblStaffName2)
.addComponent(lblStaffName5))
.addComponent(lblStaffName4))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lblStaffName)
.addComponent(lblStaffName1))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
);
HND in Computing & Programming in
System Development Java
44
layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[]
{btnCourse, btnStudent});
pack();
}// </editor-fold>
private void
menuAddUsersActionPerformed(java.awt.event.ActionEvent evt)
{ // TODO add your handling
code here: AddUser adduser = new AddUser();
adduser.setLocationRelativeTo(null);
adduser.setResizable(false); adduser.setVisible(true);
}
private void menuEditMyAccActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
EditMyUserAcc editmyacc = new EditMyUserAcc(staffid);
editmyacc.setLocationRelativeTo(null);
editmyacc.setResizable(false);
editmyacc.setVisible(true);
}
private void
menulogoutMouseClicked(java.awt.event.MouseEvent evt) {
this.setVisible(false); Login log = new Login();
log.setVisible(true);
log.setLocationRelativeTo(null);
}
private void menulogoutActionPerformed(java.awt.event.ActionEvent evt) {
}
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
HND in Computing & Programming in
System Development Java
45
private void
jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt)
{ // TODO add your handling
code here: About about = new About();
about.setVisible(true);
about.setLocationRelativeTo(null);
}
private void btnStudentActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
StudentDetails sudentdetails= new
StudentDetails(privileges); sudentdetails.setVisible(true);
sudentdetails.setLocationRelativeTo(null);
sudentdetails.setResizable(false);
}
private void btnCourseActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
CourseDetails csdetails= new
CourseDetails(privileges);
csdetails.setVisible(true);
csdetails.setLocationRelativeTo(null);
csdetails.setResizable(false);
}
private void btnCourse2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
BatchDetails batch= new BatchDetails(privileges);
batch.setVisible(true);
batch.setLocationRelativeTo(null);
batch.setResizable(false);
}
/**
* @param args the command line arguments
*/
public static void main(String args[])throws ClassNotFoundException,
SQLException {
HND in Computing & Programming in
System Development Java
46
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception er) {
System.out.println(er.toString());
}
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable()
{ public void run() {
//Main m =new Main();
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnCourse;
private javax.swing.JButton btnCourse1;
private javax.swing.JButton btnCourse2;
private javax.swing.JButton btnStudent;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenu jMenu3;
private javax.swing.JMenuBar
jMenuBar1; private
javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem
jMenuItem2; private
javax.swing.JLabel lblStaffName;
private javax.swing.JLabel
lblStaffName1; private
javax.swing.JLabel lblStaffName2;
private javax.swing.JLabel
lblStaffName3; private
javax.swing.JLabel lblStaffName4;
private javax.swing.JLabel
lblStaffName5; private
javax.swing.JMenuItem menuAddUsers;
private javax.swing.JMenuItem
menuEditMyAcc; private
javax.swing.JMenu menulogout;
// End of variables declaration
}
HND in Computing & Programming in
System Development Java
47
Student Details form
Figure 34
Program Code:
/*
* To change this license header, choose License Headers in
Project Properties. * To change this template file, choose Tools |
Templates * and open the template in the editor.
*/
package heinstitute; import
java.sql.Connection;
import
java.sql.DriverManager;
import java.sql.ResultSet;
import
java.sql.SQLException;
import java.sql.Statement;
import
javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/**
*
* @author Anushka
*/
HND in Computing & Programming in
System Development Java
48
public class StudentDetails extends javax.swing.JFrame {
/**
* Creates new form StudentDetails
*/
DefaultTableModel model;
public StudentDetails(boolean q) {
initComponents();
model = new DefaultTableModel();
model.addColumn("Student_ID");
model.addColumn("Student_Name");
model.addColumn("Age");
model.addColumn("Telephone_no");
tblStudent.setModel(model);
this.setUserLimitations(q);
}
/**
* This method is called from within the constructor to
initialize the form. * WARNING: Do NOT modify this code.
The content of this method is always * regenerated by the
Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jScrollPane1 = new
javax.swing.JScrollPane(); tblStudent
= new javax.swing.JTable(); btnDelete
= new javax.swing.JButton(); btnEdit =
new javax.swing.JButton();
txtStudentID = new
javax.swing.JTextField();
btnAddNewStudent = new
HND in Computing & Programming in
System Development Java
49
javax.swing.JButton(); jLabel1 = new
javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setBackground(new java.awt.Color(255,
153, 0)); jButton1.setFont(new
java.awt.Font("Tahoma", 1, 10)); // NOI18N
jButton1.setText("OK");
jButton1.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
tblStudent.setAutoCreateRowSorter(true);
tblStudent.setModel(new
javax.swing.table.DefaultTableModel( new Object
[][] { {null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(tblStudent); if
(tblStudent.getColumnModel().getColumnCount() > 0) {
tblStudent.getColumnModel().getColumn(0).setHeaderValue("
Title 1");
tblStudent.getColumnModel().getColumn(1).setHeaderValue("
Title 2");
tblStudent.getColumnModel().getColumn(2).setHeaderValue("
Title 3");
HND in Computing & Programming in
System Development Java
50
tblStudent.getColumnModel().getColumn(3).setHeaderValue("
Title 4");
}
btnDelete.setBackground(new java.awt.Color(0, 0, 0));
btnDelete.setForeground(new java.awt.Color(255, 255, 255));
btnDelete.setText("Delete Student Details");
btnDelete.setHorizontalTextPosition(javax.swing.SwingConstants.RIG
HT); btnDelete.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
btnDeleteActionPerformed(evt);
}
});
btnEdit.setBackground(new java.awt.Color(0, 0, 0));
btnEdit.setForeground(new java.awt.Color(255, 255, 255));
btnEdit.setText("Edit Student Detais");
btnEdit.setHorizontalTextPosition(javax.swing.SwingConstants.RIG
HT); btnEdit.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
btnEditActionPerformed(evt);
}
});
txtStudentID.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
txtStudentIDActionPerformed(evt);
}
});
btnAddNewStudent.setBackground(new java.awt.Color(0, 0, 0));
btnAddNewStudent.setForeground(new java.awt.Color(255, 255, 255));
btnAddNewStudent.setText("Add New Student Detail");
btnAddNewStudent.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
btnAddNewStudentActionPerformed(evt);
}
});
HND in Computing & Programming in
System Development Java
51
jLabel1.setText("Student ID :");
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout); layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addComponent(btnAddNewStudent)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnEdit)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 149,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 79,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 742,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtStudentID, javax.swing.GroupLayout.PREFERRED_SIZE, 105,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addContainerGap(18, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
HND in Computing & Programming in
System Development Java
52
.addComponent(txtStudentID, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 195,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 31,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 30,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 30,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnAddNewStudent,
javax.swing.GroupLayout.PREFERRED_SIZE, 30,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
pack();
}// </editor-fold>
public void setUserLimitations(boolean prm){
btnDelete.setEnabled(prm);
}
public void findStudentDetails() throws ClassNotFoundException,
SQLException{ // connect to the sql server
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://Anushka-
PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;";
Connection connection = DriverManager.getConnection(connectionUrl);
System.out.println("Successfuly Connected ");
HND in Computing & Programming in
System Development Java
53
String SQL1 = " SELECT * FROM Student_Details WHERE Student_ID LIKE" + "'%"+
txtStudentID.getText() +"%'";
Statement stmt2 =
connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet rls = stmt2.executeQuery(SQL1);
this.refreshTable();
while(rls.next())
{
model.addRow(new Object[]{
rls.getString(1),
rls.getString(2),
rls.getString(3),
rls.getString(4),
}
);
}
}
//--------refreshthe table-------
---- public void
refreshTable(){ while
(model.getRowCount() > 0) {
model.removeRow(0);
}
}
// Delete Student details-------------------
public void deletestudentDetails(String selRow) throws ClassNotFoundException,
SQLException
{
// connect to the sql server
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://Anushka-
PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;";
Connection connection = DriverManager.getConnection(connectionUrl);
System.out.println("Successfuly Connected ");
HND in Computing & Programming in
System Development Java
54
Statement statement = connection.createStatement();
statement.executeUpdate("DELETE FROM Student_Details WHERE Student_ID='" +
selRow + "'");
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.setVisible(false);
}
private void
btnDeleteActionPerformed(java.awt.event.ActionEvent evt)
{ // TODO add your handling
code here: try{
if (tblStudent.getSelectedRow() < 0 || tblStudent.getSelectedColumn() < 0) {
JOptionPane.showMessageDialog(this, "Select a Record From Table To Delete","NO
RECORD
SELECTED",JOptionPane.WARNING_MESSAGE);
}
else {
int respond =JOptionPane.showConfirmDialog(this, "Are You Sure You Want To Delete
This?","Confirm
Deletion",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTIN_
MESSAGE);
if (respond==0){
String selectedRec =
(String)tblStudent.getValueAt(tblStudent.getSelectedRow(), 0);
this.deletestudentDetails(selectedRec); // deleting a cake from Db
this.refreshTable();
this.findStudentDetails();
}
else{
}
}
}//end try
catch (ClassNotFoundException e){
System.out.println(e);
}
HND in Computing & Programming in
System Development Java
55
catch (SQLException e){
JOptionPane.showMessageDialog(null, e, "ERROR", JOptionPane.ERROR_MESSAGE);
}
}
private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (tblStudent.getSelectedRow() <0 || tblStudent.getSelectedColumn()<0) {
JOptionPane.showMessageDialog(this, "First select a record to Edit!", "NO RECORD
SELECTED",
JOptionPane.WARNING_MESSAGE);
} else {
String st = (String)
tblStudent.getValueAt(tblStudent.getSelectedRow(), 0);
EditStudentDetails editstudentdetails = new EditStudentDetails(st);
editstudentdetails.setVisible(true);
editstudentdetails.setLocationRelativeTo(null);
editstudentdetails.setResizable(false);
}
}
private void btnAddNewStudentActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
AddNewStudent addnewstudent = new AddNewStudent ();
addnewstudent.setVisible(true);
addnewstudent.setResizable(false);
addnewstudent.setLocationRelativeTo(null);
}
private void txtStudentIDActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{
if (txtStudentID.getText().equals("")){
//txtStudentID empty
JOptionPane.showMessageDialog(null, "The Student ID Field Is Empty,Enter Student ID
And Search", "EMPTY FIELD", JOptionPane.ERROR_MESSAGE);
}
HND in Computing & Programming in
System Development Java
56
else{//txtxCake not empty
this.findStudentDetails();
if(model.getRowCount()==0){
JOptionPane.showMessageDialog(this, "No Data Found", "NO DATA",
JOptionPane.ERROR_MESSAGE);
}
}
}//end try
catch (ClassNotFoundException e){
System.out.println(e);
}
catch (SQLException e){
JOptionPane.showMessageDialog(null, e, "ERROR IN CONNECTION",
JOptionPane.ERROR_MESSAGE);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting
code (optional) "> /* If Nimbus (introduced in Java SE 6) is not
available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) { if
("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
HND in Computing & Programming in
System Development Java
57
java.util.logging.Logger.getLogger(StudentDetails.class.getName()).log(java.util.logging.Level.
SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(StudentDetails.class.getName()).log(java.util.logging.Level.
SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(StudentDetails.class.getName()).log(java.util.logging.Level.
SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(StudentDetails.class.getName()).log(java.util.logging.Level.
SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new
Runnable() { public void run() {
// new StudentDetails().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnAddNewStudent;
private javax.swing.JButton
btnDelete; private
javax.swing.JButton btnEdit; private
javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane
jScrollPane1; private
javax.swing.JTable tblStudent;
private javax.swing.JTextField
txtStudentID;
// End of variables declaration
}
HND in Computing & Programming in
System Development Java
58
Add New Student Details Form
Figure 35
Program Code:
/*
* To change this license header, choose License Headers in Project Properties. * To change this template
file, choose Tools | Templates * and open the template in the editor.
*/
package heinstitute; import
java.sql.Connection;
import
java.sql.DriverManager;
import java.sql.ResultSet;
import
java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
/**
*
* @author Anushka
*/
public class AddNewStudent extends javax.swing.JFrame {
/**
HND in Computing & Programming in
System Development Java
59
* Creates new form AddNewStudent
*/
public AddNewStudent() {
initComponents();
try{
this.generateID();
}//end try
catch (ClassNotFoundException e){
System.out.println(e);
}
catch (SQLException e){
System.out.println(e);
}
}
/**
* This method is called from within the constructor to initialize the
form. * WARNING: Do NOT modify this code. The content of this
method is always * regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jPanel1 = new javax.swing.JPanel();
jLabel5 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
txtstudID = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
txtstudName = new
javax.swing.JTextField(); txtAge = new
javax.swing.JTextField(); txtTp = new
javax.swing.JTextField(); txtCsID =
new javax.swing.JTextField();
txtBatchID = new javax.swing.JTextField();
btnAdd = new javax.swing.JButton();
btnClear = new javax.swing.JButton();
jSeparator2 = new javax.swing.JSeparator();
jLabel7 = new javax.swing.JLabel();
HND in Computing & Programming in
System Development Java
60
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setBackground(new java.awt.Color(255, 153, 0));
jButton1.setText("ok");
jButton1.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Add new Student",
javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
javax.swing.border.TitledBorder.DEFAULT_POSITION,new java.awt.Font("Arial Unicode MS", 0, 14),
new java.awt.Color(255, 153, 0))); // NOI18N
jLabel5.setText("Name :");
jLabel3.setText("Age :");
jLabel1.setText("Student ID :");
txtstudID.setDisabledTextColor(new java.awt.Color(255, 51, 51));
txtstudID.setEnabled(false);
jLabel4.setText("Telephone_no :");
jLabel2.setText("Course ID :");
jLabel6.setText("Batch ID :");
txtAge.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
txtAgeActionPerformed(evt);
}
});
txtTp.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
txtTpActionPerformed(evt);
}
});
HND in Computing & Programming in
System Development Java
61
javax.swing.GroupLayout jPanel1Layout = new
javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addComponent(jLabel3)
.addComponent(jLabel4)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jLabel6, javax.swing.GroupLayout.Alignment.LEADING,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)))
.addGap(10, 10, 10)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtstudName)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(txtstudID, javax.swing.GroupLayout.PREFERRED_SIZE, 122,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtAge, javax.swing.GroupLayout.DEFAULT_SIZE, 123,
Short.MAX_VALUE) .addComponent(txtTp)
.addComponent(txtCsID)
.addComponent(txtBatchID))
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
HND in Computing & Programming in
System Development Java
62
.addComponent(txtstudID, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(txtstudName, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(txtAge, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(txtTp, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(txtCsID, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtBatchID, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel6))
.addGap(5, 5, 5))
);
btnAdd.setText("Save");
btnAdd.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
btnAddActionPerformed(evt);
}
});
btnClear.setText("Clear");
HND in Computing & Programming in
System Development Java
63
btnClear.setHorizontalTextPosition(javax.swing.SwingConstants.RIG
HT); btnClear.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
btnClearActionPerformed(evt);
}
});
jLabel7.setText("He Institute ");
jLabel7.setEnabled(false);
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout); layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup() .addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(12, 12, 12)
.addComponent(btnClear, javax.swing.GroupLayout.PREFERRED_SIZE, 73,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 87,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 111,
Short.MAX_VALUE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 75,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel7)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jSeparator2)))
.addContainerGap())
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnAdd,
btnClear, jButton1});
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
HND in Computing & Programming in
System Development Java
64
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup() .addGap(21, 21, 21)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 19,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 32,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 30,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnClear))
.addContainerGap())
);
layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btnAdd,
btnClear, jButton1});
pack();
}// </editor-fold>
public void add_student() throws ClassNotFoundException, SQLException{
// connection to the sql server
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://Anushka-
PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;";
Connection connection = DriverManager.getConnection(connectionUrl);
System.out.println("The database is Connected ");
Statement statement = connection.createStatement();
if(txtstudName.getText().equals("")){
JOptionPane.showMessageDialog(null, "The Student name Field Is Empty,Enter the Student name
Name", "EMPTY FIELD", JOptionPane.ERROR_MESSAGE);
}
else if(txtAge.getText().equals("")){
JOptionPane.showMessageDialog(null, "The Age Field Is Empty,Enter the Student Age", "EMPTY
FIELD", JOptionPane.ERROR_MESSAGE);
HND in Computing & Programming in
System Development Java
65
}
else if(txtTp.getText().equals("")){
JOptionPane.showMessageDialog(null, "The Telephone number Field Is Empty,Enter the Student
Telephone number", "EMPTY FIELD", JOptionPane.ERROR_MESSAGE);
}
else if(txtCsID.getText().equals("")){
JOptionPane.showMessageDialog(null, "The Course ID Field Is Empty,Enter a Course ID",
"EMPTY FIELD", JOptionPane.ERROR_MESSAGE);
}
else if(txtBatchID.getText().equals("")){
JOptionPane.showMessageDialog(null, "The Batch ID Field Is Empty,Enter a Batch ID", "EMPTY
FIELD", JOptionPane.ERROR_MESSAGE);
}
else {
statement.executeUpdate("INSERT INTO Student_Details VALUES " +"( '" + txtstudID.getText()
+ "'" + "," +
"'" + txtstudName.getText() + "'" + "," +
"'" + txtAge.getText()+ "'" + "," +
"'" + txtTp.getText() + "'" + ")" + "");
statement.executeUpdate("INSERT INTO [Student_ Course] VALUES "+"( '" +
txtstudID.getText() + "'" + "," +
"'" + txtCsID.getText() + "'" + ")" + "");
statement.executeUpdate("INSERT INTO [Student_ Batch] VALUES "+"( '" +
txtstudID.getText() + "'" + "," +
"'" + txtBatchID.getText() + "'" + ")" + "");
JOptionPane.showMessageDialog(null, "Record has been saved in the database", "INSERT
SUCCESSFUL", JOptionPane.INFORMATION_MESSAGE);
}
}
//-----------------------End of Add Student-------------------------------------------------------
HND in Computing & Programming in
System Development Java
66
public void clearAll(){
txtstudName.setText("");
txtstudID.setText("");
txtAge.setText("");
txtTp.setText("");
txtCsID.setText("");
txtBatchID.setText("");
}
public void generateID()throws ClassNotFoundException, SQLException{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://Anushka-
PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;";
Connection connection = DriverManager.getConnection(connectionUrl);
System.out.println("The database is Connected ");
String sql="SELECT * FROM Student_Details ORDER BY Student_ID ASC";
Statement stmt2 = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet rs =
stmt2.executeQuery(sql); int id;
if(!rs.next()){
id=1;
txtstudID.setText(Integer.toString(id));
}
else{
rs.last();
id=Integer.valueOf(rs.getString(1));
String
str_newid=Integer.toString(id+1);
txtstudID.setText(str_newid);
}
}// end of get details from student
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.setVisible(false);
HND in Computing & Programming in
System Development Java
67
}
private void
btnAddActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here: try{
this.add_student();//add new cake to
database this.clearAll();
this.generateID();
}//end try
catch (ClassNotFoundException e){
System.out.println(e);
}
catch (SQLException e){
JOptionPane.showMessageDialog(null,e, "ERROR", JOptionPane.ERROR_MESSAGE);
System.out.println(e);
}
}
private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.clearAll();
}
private void txtAgeActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void txtTpActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting
code (optional) "> /* If Nimbus (introduced in Java SE 6) is not
available, stay with the default look and feel.
HND in Computing & Programming in
System Development Java
68
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) { if
("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(AddNewStudent.class.getName()).log(java.util.logging.Level.SEVER
E, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(AddNewStudent.class.getName()).log(java.util.logging.Level.SEVER
E, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(AddNewStudent.class.getName()).log(java.util.logging.Level.SEVER
E, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(AddNewStudent.class.getName()).log(java.util.logging.Level.SEVER
E, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable()
{ public void run() {
new AddNewStudent().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnAdd;
private javax.swing.JButton btnClear;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
HND in Computing & Programming in
System Development Java
69
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JPanel jPanel1;
private javax.swing.JSeparator
jSeparator2; private
javax.swing.JTextField txtAge; private
javax.swing.JTextField txtBatchID;
private javax.swing.JTextField txtCsID;
private javax.swing.JTextField txtTp;
private javax.swing.JTextField txtstudID;
private javax.swing.JTextField txtstudName;
// End of variables declaration
}
Edit Student Details Form
Figure 36
Program Code:
/*
* To change this license header, choose License Headers in Project Properties. * To change this template
file, choose Tools | Templates * and open the template in the editor.
*/
HND in Computing & Programming in
System Development Java
70
package heinstitute; import
java.sql.Connection;
import
java.sql.DriverManager;
import java.sql.ResultSet;
import
java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
/**
*
* @author Anushka
*/
public class EditStudentDetails extends javax.swing.JFrame {
/**
* Creates new form EditStudentDetails
*/
public EditStudentDetails(String id) {
initComponents();
txtstudID.setText(id);
try{
this.setStudentDetails(id);
}//end try
catch (ClassNotFoundException e){
System.out.println(e);
}
catch (SQLException e){
JOptionPane.showMessageDialog(null, e, "ERROR", JOptionPane.ERROR_MESSAGE);
}
}
/**
* This method is called from within the constructor to initialize the
form. * WARNING: Do NOT modify this code. The content of this
method is always * regenerated by the Form Editor.
*/
HND in Computing & Programming in
System Development Java
71
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
btnEdit = new javax.swing.JButton();
btnClear = new javax.swing.JButton();
btnCancel = new javax.swing.JButton();
jPanel1 = new javax.swing.JPanel();
jLabel5 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
txtstudName = new
javax.swing.JTextField(); jLabel1 =
new javax.swing.JLabel(); txtstudID =
new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
txtAge = new javax.swing.JTextField();
txtTp = new javax.swing.JTextField();
jLabel6 = new javax.swing.JLabel();
jSeparator2 = new javax.swing.JSeparator();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
btnEdit.setText("Update ");
btnEdit.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
btnEditActionPerformed(evt);
}
});
btnClear.setText("Clear");
btnClear.setHorizontalTextPosition(javax.swing.SwingConstant
s.RIGHT); btnClear.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
btnClearActionPerformed(evt);
}
});
btnCancel.setText("OK");
btnCancel.addActionListener(new
java.awt.event.ActionListener() { public void
HND in Computing & Programming in
System Development Java
72
actionPerformed(java.awt.event.ActionEvent evt) {
btnCancelActionPerformed(evt);
}
});
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Edit Student
details",
javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial Unicode
MS", 0, 14), new java.awt.Color(255, 153, 0))); // NOI18N
jLabel5.setText("Name :");
jLabel3.setText("Age :");
jLabel1.setText("Student ID :");
txtstudID.setDisabledTextColor(new java.awt.Color(255, 51, 51));
txtstudID.setEnabled(false);
jLabel4.setText("Telephone_no :");
javax.swing.GroupLayout jPanel1Layout = new
javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING
)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILIN
G, false)
.addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
HND in Computing & Programming in
System Development Java
73
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING
) .addComponent(txtstudName,
javax.swing.GroupLayout.PREFERRED_SIZE, 205,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtstudID, javax.swing.GroupLayout.PREFERRED_SIZE,
122, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING
)
.addComponent(jLabel3)
.addComponent(jLabel4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
false) .addComponent(txtAge)
.addComponent(txtTp, javax.swing.GroupLayout.DEFAULT_SIZE, 123,
Short.MAX_VALUE)))) .addContainerGap(77, Short.MAX_VALUE))
);
jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new
java.awt.Component[] {jLabel1, jLabel3, jLabel4, jLabel5});
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E) .addComponent(jLabel1)
.addComponent(txtstudID, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E) .addComponent(jLabel5)
.addComponent(txtstudName, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
HND in Computing & Programming in
System Development Java
74
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E) .addComponent(jLabel3)
.addComponent(txtAge, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(9, 9, 9)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E) .addComponent(jLabel4)
.addComponent(txtTp, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(53, 53, 53))
);
jLabel6.setText("He Institute ");
jLabel6.setEnabled(false);
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout); layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(btnClear)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnEdit)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnCancel))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel6)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jSeparator2)))
.addContainerGap())
);
HND in Computing & Programming in
System Development Java
75
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[]
{btnCancel, btnClear, btnEdit});
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 165,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(37, 37, 37)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 19,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 30,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnClear)
.addComponent(btnCancel))
.addContainerGap())
);
layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[]
{btnCancel, btnClear, btnEdit});
pack();
}// </editor-fold>
public void clearAll(){
txtstudName.setText("");
txtAge.setText("");
txtTp.setText("");
}//***********************end cl ******************
public void updateStudentDetails() throws ClassNotFoundException, SQLException
HND in Computing & Programming in
System Development Java
76
{
// connection to the sql server
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://Anushka-
PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;";
Connection connection = DriverManager.getConnection(connectionUrl);
System.out.println("Successfuly Connected ");
Statement statement = connection.createStatement();
String sql = "UPDATE Student_Details SET Student_Name='"+ txtstudName.getText()+
"'" + ","+
"Age='"+ txtAge.getText()+ "'" + ","+
"Telephone_no='"+ txtTp.getText() + "'" +
"WHERE Student_ID='"+ txtstudID.getText() +"'";
statement.executeUpdate(sql);
}//------------------endupdate--------------------------------
public void setStudentDetails(String S_id) throws ClassNotFoundException,
SQLException{ // sql server connection
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://Anushka-
PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;";
Connection connection = DriverManager.getConnection(connectionUrl);
System.out.println("Successfuly Connected ");
String SQL1 = " SELECT * FROM Student_Details WHERE Student_ID ='"+ S_id +"'";
Statement stmt2 =
connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet rls = stmt2.executeQuery(SQL1);
HND in Computing & Programming in
System Development Java
77
rls.first();
txtstudName.setText(rls.getString(2));
txtAge.setText(rls.getString(3));
txtTp.setText(rls.getString(4));
}
private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {
try{
int respond =JOptionPane.showConfirmDialog(this, "Are You Sure You
Want To Update This record?","Confirm
Update",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE)
; if (respond==0){ this.updateStudentDetails();
this.setVisible(false);
}
else{
}
}//end try
catch (ClassNotFoundException e){
System.out.println(e);
}
catch (SQLException e){
JOptionPane.showMessageDialog(null, e, "ERROR",
JOptionPane.ERROR_MESSAGE);
}
}
private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.clearAll();
}
private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.setVisible(false);
}
HND in Computing & Programming in
System Development Java
78
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting
code (optional) "> /* If Nimbus (introduced in Java SE 6) is not
available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) { if
("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(EditStudentDetails.class.getName()).log(java.util.logging.Le
vel.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(EditStudentDetails.class.getName()).log(java.util.logging.Le
vel.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(EditStudentDetails.class.getName()).log(java.util.logging.Le
vel.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(EditStudentDetails.class.getName()).log(java.util.logging.
Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable()
{ public void run() {
new EditStudentDetails("s").setVisible(true);
}
HND in Computing & Programming in
System Development Java
79
});
}
// Variables declaration - do not modify
private javax.swing.JButton
btnCancel; private
javax.swing.JButton btnClear;
private javax.swing.JButton btnEdit;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JPanel jPanel1;
private javax.swing.JSeparator
jSeparator2; private
javax.swing.JTextField txtAge;
private javax.swing.JTextField txtTp;
private javax.swing.JTextField
txtstudID; private
javax.swing.JTextField txtstudName;
// End of variables declaration
}
Course Details Form
Figure 37
HND in Computing & Programming in
System Development Java
80
Program Code:
/*
* To change this license header, choose License Headers in
Project Properties. * To change this template file, choose Tools |
Templates * and open the template in the editor.
*/
package heinstitute;
import
java.sql.Connection;
import
java.sql.DriverManager;
import java.sql.ResultSet;
import
java.sql.SQLException;
import java.sql.Statement;
import
javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/**
*
* @author Anushka
*/
public class CourseDetails extends javax.swing.JFrame {
/**
* Creates new form CourseDetails
*/
DefaultTableModel model;
public CourseDetails(boolean r) {
initComponents();
model = new DefaultTableModel();
HND in Computing & Programming in
System Development Java
81
model.addColumn("Course_ID");
model.addColumn("Course_Name");
model.addColumn("Duration");
model.addColumn("Course_Fee");
tblCourse.setModel(model);
this.setUserLimitations(r);
}
/**
* This method is called from within the constructor to
initialize the form. * WARNING: Do NOT modify this code.
The content of this method is always * regenerated by the
Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
btnDelete = new
javax.swing.JButton();
btnAddNewCourse = new
javax.swing.JButton(); btnEdit = new
javax.swing.JButton(); jButton1 = new
javax.swing.JButton(); jScrollPane1 =
new javax.swing.JScrollPane();
tblCourse = new javax.swing.JTable();
txtCourseID1 = new
javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_O
N_CLOSE); setName("Course Details"); // NOI18N
btnDelete.setBackground(new java.awt.Color(0, 0, 0));
btnDelete.setForeground(new java.awt.Color(255, 255, 255));
btnDelete.setText("Delete Course Details");
btnDelete.setHorizontalTextPosition(javax.swing.SwingConstant
s.RIGHT); btnDelete.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
btnDeleteActionPerformed(evt);
HND in Computing & Programming in
System Development Java
82
}
});
btnAddNewCourse.setBackground(new java.awt.Color(0, 0, 0));
btnAddNewCourse.setForeground(new java.awt.Color(255, 255, 255));
btnAddNewCourse.setText("Add New Course Detail");
btnAddNewCourse.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
btnAddNewCourseActionPerformed(evt);
}
});
btnEdit.setBackground(new java.awt.Color(0, 0, 0));
btnEdit.setForeground(new java.awt.Color(255, 255, 255));
btnEdit.setText("EditCourse Detais");
btnEdit.setHorizontalTextPosition(javax.swing.SwingConstant
s.RIGHT); btnEdit.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
btnEditActionPerformed(evt);
}
});
jButton1.setBackground(new java.awt.Color(255,
153, 0)); jButton1.setFont(new
java.awt.Font("Tahoma", 1, 10)); // NOI18N
jButton1.setText("OK");
jButton1.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
tblCourse.setModel(new
javax.swing.table.DefaultTableModel( new Object
[][] { {null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
HND in Computing & Programming in
System Development Java
83
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title
2", "Title 3",
"Title 4"
}
) {
boolean[] canEdit = new boolean [] {
false, false, false, false
};
public boolean isCellEditable(int rowIndex, int
columnIndex) { return canEdit [columnIndex];
}
});
tblCourse.getTableHeader().setReorderingAllowed(false);
jScrollPane1.setViewportView(tblCourse); if
(tblCourse.getColumnModel().getColumnCount() > 0) {
tblCourse.getColumnModel().getColumn(0).setResizable(
false);
tblCourse.getColumnModel().getColumn(1).setResizable(
false);
tblCourse.getColumnModel().getColumn(2).setResizable(
false);
tblCourse.getColumnModel().getColumn(3).setResizable(
false);
}
txtCourseID1.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
txtCourseID1ActionPerformed(evt);
}
});
jLabel1.setText("Course ID");
HND in Computing & Programming in
System Development Java
84
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout); layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(btnAddNewCourse)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 149,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btnEdit)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 79,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(txtCourseID1, javax.swing.GroupLayout.PREFERRED_SIZE, 116,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE,
742, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(12, Short.MAX_VALUE))
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[]
{btnAddNewCourse, btnDelete, btnEdit});
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup() .addGap(10, 10, 10)
HND in Computing & Programming in
System Development Java
85
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtCourseID1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 195,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 57,
Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 30,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 31,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(btnAddNewCourse, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnDelete, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[]
{btnAddNewCourse, btnDelete, btnEdit});
pack();
}// </editor-fold>
public void setUserLimitations(boolean prm){
btnDelete.setEnabled(prm);
btnAddNewCourse.setEnabled(prm);
btnEdit.setEnabled(prm);
}
public void findCourseDetails() throws ClassNotFoundException,
SQLException{ // connect to the sql server
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://Anushka-
HND in Computing & Programming in
System Development Java
86
PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;";
Connection connection = DriverManager.getConnection(connectionUrl);
System.out.println("Successfuly Connected ");
String SQL1 = " SELECT * FROM Course_Details WHERE Course_ID LIKE" + "'%"+
txtCourseID1.getText() +"%'";
Statement stmt2 =
connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet rls1 = stmt2.executeQuery(SQL1);
this.refreshTable();
while(rls1.next())
{
model.addRow(new Object[]{
rls1.getString(1),
rls1.getString(2),
rls1.getString(3),
rls1.getString(4),
}
);
}
}
//--------refreshthe table-------
---- public void
refreshTable(){ while
(model.getRowCount() > 0) {
model.removeRow(0);
}
}
HND in Computing & Programming in
System Development Java
87
// Delete SCourse details-------------------
public void deleteCourseDetails(String selRow) throws ClassNotFoundException,
SQLException
{
// connect to the sql server
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://Anushka-
PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;";
Connection connection = DriverManager.getConnection(connectionUrl);
System.out.println("Successfuly Connected ");
Statement statement = connection.createStatement();
statement.executeUpdate("DELETE FROM Course_Details WHERE Course_ID='" +
selRow + "'");
}
private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{
if (tblCourse.getSelectedRow() < 0 || tblCourse.getSelectedColumn() < 0) {
JOptionPane.showMessageDialog(this, "Select a Record From Table To Delete","NO
RECORD
SELECTED",JOptionPane.WARNING_MESSAGE);
}
else {
int respond =JOptionPane.showConfirmDialog(this, "Are You Sure You Want To
Delete This?","Confirm
Deletion",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTIO
N_MESSAGE); if (respond==0){
String selectedRec =
(String)tblCourse.getValueAt(tblCourse.getSelectedRow(), 0);
this.deleteCourseDetails(selectedRec); // deleting a cake from Db
this.refreshTable();
this.findCourseDetails();
}
else{
}
}
HND in Computing & Programming in
System Development Java
88
}//end try
catch (ClassNotFoundException e){
System.out.println(e);
}
catch (SQLException e){
JOptionPane.showMessageDialog(null, e, "ERROR",
JOptionPane.ERROR_MESSAGE);
}
}
private void btnAddNewCourseActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
AddNewCourse addnewcourse = new AddNewCourse ();
addnewcourse.setVisible(true);
addnewcourse.setResizable(false);
addnewcourse.setLocationRelativeTo(null);
}
private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (tblCourse.getSelectedRow() <0 || tblCourse.getSelectedColumn()<0) {
JOptionPane.showMessageDialog(this, "First select a record to Edit!", "NO RECORD
SELECTED", JOptionPane.WARNING_MESSAGE);
} else {
String str = (String)
tblCourse.getValueAt(tblCourse.getSelectedRow(), 0);
EditCourseDetails editcoursedetails = new EditCourseDetails(str);
editcoursedetails.setVisible(true);
editcoursedetails.setLocationRelativeTo(null);
editcoursedetails.setResizable(false);
}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.setVisible(false);
}
private void
txtCourseID1ActionPer
formed(java.awt.event.
ActionEvent evt) {
HND in Computing & Programming in
System Development Java
89
// TODO add your
handling code here:
try{
if
(txtCourseID1.getText().equals("")){
//txtcake empty
JOptionPane.showMessageDialog(null, "The Cake Name Field Is Empty,Enter Cake Name
And Search", "EMPTY FIELD", JOptionPane.ERROR_MESSAGE);
}
else{//txtxCake not empty
this.findCourseDetails();
if(model.getRowCount()==0){
JOptionPane.showMessageDialog(this, "No Data Found", "NO DATA",
JOptionPane.ERROR_MESSAGE);
}
}
}//end try
catch (ClassNotFoundException e){
System.out.println(e);
}
catch (SQLException e){
JOptionPane.showMessageDialog(null, e, "ERROR IN CONNECTION",
JOptionPane.ERROR_MESSAGE);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting
code (optional) "> /* If Nimbus (introduced in Java SE 6) is not
available, stay with the default look and feel.
HND in Computing & Programming in
System Development Java
90
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) { if
("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName()); break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(CourseDetails.class.getName()).log(java.util.logging.Level.S
EVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(CourseDetails.class.getName()).log(java.util.logging.Level.S
EVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(CourseDetails.class.getName()).log(java.util.logging.Le
vel.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(CourseDetails.class.getName()).log(java.util.logging.Level.S
EVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new
Runnable() { public void run() {
//new CourseDetails().setVisible(true);
}
});
}
// Variables declaration - do not
modify private
javax.swing.JButton btnAddNewCourse;
private javax.swing.JButton btnDelete;
private javax.swing.JButton btnEdit;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
HND in Computing & Programming in
System Development Java
91
private javax.swing.JScrollPane
jScrollPane1; private
javax.swing.JTable tblCourse; private
javax.swing.JTextField txtCourseID1;
// End of variables declaration
}
Add Course detail Form
Figure 38
Program Code:
/*
* To change this license header, choose License Headers in Project Properties. * To change this
template file, choose Tools | Templates * and open the template in the editor.
*/
package heinstitute;
import
java.sql.Connection;
import
java.sql.DriverManager;
HND in Computing & Programming in
System Development Java
92
import
java.sql.ResultSet;
import
java.sql.SQLException;
import
java.sql.Statement;
import javax.swing.JOptionPane;
/**
*
* @author Anushka
*/
public class AddNewCourse extends javax.swing.JFrame {
/**
* Creates new form AddNewCourse
*/
public AddNewCourse() {
initComponents();
try{
this.generateID();
}//end try
catch (ClassNotFoundException e){
System.out.println(e);
}
catch (SQLException e){
System.out.println(e);
}
}
/**
* This method is called from within the constructor to
initialize the form. * WARNING: Do NOT modify this code.
The content of this method is always * regenerated by the
Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
HND in Computing & Programming in
System Development Java
93
jPanel1 = new
javax.swing.JPanel(); jLabel5 =
new javax.swing.JLabel(); jLabel3
= new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
txtCsID = new
javax.swing.JTextField(); jLabel4 =
new javax.swing.JLabel();
txtcsName = new
javax.swing.JTextField();
txtDuration = new
javax.swing.JTextField(); txtFee =
new javax.swing.JTextField();
btnClear = new javax.swing.JButton();
btnSave = new javax.swing.JButton();
btnCancel = new
javax.swing.JButton(); jLabel6 =
new javax.swing.JLabel();
jSeparator2 = new
javax.swing.JSeparator();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Add New Course ",
javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial Unicode
MS", 0, 14), new java.awt.Color(255, 153, 0))); // NOI18N
jLabel5.setText("Course Name :");
jLabel3.setText("Duration :");
jLabel1.setText("Course ID :");
txtCsID.setDisabledTextColor(new java.awt.Color(255, 51, 51));
txtCsID.setEnabled(false);
txtCsID.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
txtCsIDActionPerformed(evt);
}
HND in Computing & Programming in
System Development Java
94
});
jLabel4.setText("Course fee :");
txtFee.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
txtFeeActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new
javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING
)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILIN
G, false)
.addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(29, 29, 29)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING
)
.addComponent(txtCsID, javax.swing.GroupLayout.PREFERRED_SIZE, 122,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtcsName, javax.swing.GroupLayout.DEFAULT_SIZE, 326,
Short.MAX_VALUE)))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING
)
.addComponent(jLabel3)
HND in Computing & Programming in
System Development Java
95
.addComponent(jLabel4))
.addGap(29, 29, 29)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
false) .addComponent(txtDuration)
.addComponent(txtFee, javax.swing.GroupLayout.DEFAULT_SIZE, 123,
Short.MAX_VALUE)))) .addContainerGap())
);
jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new
java.awt.Component[] {jLabel1, jLabel3, jLabel4, jLabel5});
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E) .addComponent(jLabel1)
.addComponent(txtCsID, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E) .addComponent(jLabel5)
.addComponent(txtcsName, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E) .addComponent(jLabel3)
.addComponent(txtDuration, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(12, 12, 12)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E) .addComponent(jLabel4)
.addComponent(txtFee, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(53, 53, 53))
);
HND in Computing & Programming in
System Development Java
96
btnClear.setBackground(new java.awt.Color(0, 0, 0));
btnClear.setForeground(new java.awt.Color(255, 255, 255));
btnClear.setText("Clear");
btnClear.setHorizontalTextPosition(javax.swing.SwingConstant
s.RIGHT); btnClear.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
btnClearActionPerformed(evt);
}
});
btnSave.setBackground(new java.awt.Color(0, 0, 0));
btnSave.setForeground(new java.awt.Color(255, 255, 255));
btnSave.setText("Save");
btnSave.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
btnSaveActionPerformed(evt);
}
});
btnCancel.setBackground(new java.awt.Color(255, 153, 0));
btnCancel.setText("OK");
btnCancel.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
btnCancelActionPerformed(evt);
}
});
jLabel6.setText("He Institute ");
jLabel6.setEnabled(false);
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
HND in Computing & Programming in
System Development Java
97
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(btnClear)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnSave, javax.swing.GroupLayout.PREFERRED_SIZE, 83,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 71,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel6)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jSeparator2)))
.addContainerGap())
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[]
{btnClear, btnSave});
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(19, 19, 19)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 165,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 19,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnSave)
.addComponent(btnClear)
.addComponent(btnCancel))
HND in Computing & Programming in
System Development Java
98
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
);
layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[]
{btnCancel, btnClear, btnSave});
pack();
}// </editor-fold>
public void generateID()throws ClassNotFoundException, SQLException{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://Anushka-
PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;";
Connection connection = DriverManager.getConnection(connectionUrl);
System.out.println("The database is Connected "); // connection ok
String sql="SELECT * FROM Course_Details ORDER BY Course_ID ASC;";
Statement stmt2 = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet rs =
stmt2.executeQuery(sql); int id;
if(!rs.next()){ id=1;
txtCsID.setText(Integer.toString(id));
}
else{
rs.last()
;
id=Integer.valueOf(rs.getString(1));
txtCsID.setText(Integer.toString(id+1));
}
}
public void add_Course() throws ClassNotFoundException, SQLException{
// connection to the sql server
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://Anushka-
PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;";
Connection connection = DriverManager.getConnection(connectionUrl);
System.out.println("The database is Connected ");
HND in Computing & Programming in
System Development Java
99
Statement statement = connection.createStatement();
if(txtcsName.getText().equals("")){
JOptionPane.showMessageDialog(null, "The Course name Field Is Empty,Enter the Course
name Name", "EMPTY FIELD", JOptionPane.ERROR_MESSAGE);
}
else if(txtDuration.getText().equals("")){
JOptionPane.showMessageDialog(null, "The Duration Field Is Empty,Enter the Duration",
"EMPTY FIELD", JOptionPane.ERROR_MESSAGE);
}
else if(txtFee.getText().equals("")){
JOptionPane.showMessageDialog(null, "The Course Fee Field Is Empty,Enter the Course
Fee ", "EMPTY FIELD", JOptionPane.ERROR_MESSAGE);
}
else if(txtCsID.getText().equals("")){
JOptionPane.showMessageDialog(null, "The Course ID Field Is Empty,Enter a Course ID",
"EMPTY FIELD", JOptionPane.ERROR_MESSAGE);
}
else {
statement.executeUpdate("INSERT INTO Course_Details VALUES " +"( '" +
txtCsID.getText() + "'" + "," +
"'" + txtcsName.getText()
+ "'" + "," + "'" +
txtDuration.getText()+ "'" + "," +
"'" + txtFee.getText() + "'" + ")" + "");
JOptionPane.showMessageDialog(null, "Record has been saved in the database", "INSERT
SUCCESSFUL", JOptionPane.INFORMATION_MESSAGE);
}
}
public void clearAll(){
txtCsID.setText("");
txtcsName.setText("");
txtDuration.setText("");
txtFee.setText("");
}
private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
HND in Computing & Programming in
System Development Java
100
this.clearAll();
}
private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {
try{
this.add_Course();//add new cake to
database this.clearAll();
this.generateID();
}//end try
catch (ClassNotFoundException e){
System.out.println(e);
}
catch (SQLException e){
JOptionPane.showMessageDialog(null,e, "ERROR", JOptionPane.ERROR_MESSAGE);
System.out.println(e);
}
}
private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.setVisible(false);
}
private void txtCsIDActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void txtFeeActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
HND in Computing & Programming in
System Development Java
101
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting
code (optional) "> /* If Nimbus (introduced in Java SE 6) is not
available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) { if
("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(AddNewCourse.class.getName()).log(java.util.logging.Level
.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(AddNewCourse.class.getName()).log(java.util.logging.Level
.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(AddNewCourse.class.getName()).log(java.util.logging.Level
.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(AddNewCourse.class.getName()).log(java.util.logging.Level
.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new
Runnable() { public void run() {
new AddNewCourse().setVisible(true);
}
});
}
// Variables declaration - do not modify
HND in Computing & Programming in
System Development Java
102
private javax.swing.JButton
btnCancel; private
javax.swing.JButton btnClear;
private javax.swing.JButton btnSave;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JPanel jPanel1;
private javax.swing.JSeparator
jSeparator2; private
javax.swing.JTextField txtCsID;
private javax.swing.JTextField
txtDuration; private
javax.swing.JTextField txtFee;
private javax.swing.JTextField
txtcsName;
// End of variables
declaration }
Edit Course Details Form
Figure 39
HND in Computing & Programming in
System Development Java
103
Program Code:
/*
* To change this license header, choose License Headers in Project Properties. * To change this
template file, choose Tools | Templates * and open the template in the editor.
*/
package heinstitute;
import
java.sql.Connection;
import
java.sql.DriverManager;
import
java.sql.ResultSet;
import
java.sql.SQLException;
import
java.sql.Statement;
import javax.swing.JOptionPane;
/**
*
* @author Anushka
*/
public class EditCourseDetails extends javax.swing.JFrame {
/**
* Creates new form EditCourseDetails
*/
public EditCourseDetails(String id) {
initComponents();
txtcourseID.setText(id);
try{
this.setCourseDetails(id);
}//end try
catch (ClassNotFoundException e){
System.out.println(e);
}
HND in Computing & Programming in
System Development Java
104
catch (SQLException e){
JOptionPane.showMessageDialog(null, e, "ERROR", JOptionPane.ERROR_MESSAGE);
}
}
/**
* This method is called from within the constructor to
initialize the form. * WARNING: Do NOT modify this code.
The content of this method is always * regenerated by the
Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new
javax.swing.JPanel(); jLabel5 =
new javax.swing.JLabel(); jLabel3
= new javax.swing.JLabel();
txtcsName = new
javax.swing.JTextField(); jLabel1
= new javax.swing.JLabel();
txtcourseID = new
javax.swing.JTextField(); jLabel4
= new javax.swing.JLabel();
txtDuration = new
javax.swing.JTextField(); txtFee =
new javax.swing.JTextField();
btnEdit = new javax.swing.JButton();
btnClear = new javax.swing.JButton();
btnCancel = new javax.swing.JButton();
jSeparator2 = new
javax.swing.JSeparator(); jLabel6 =
new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Edit Course
details",
HND in Computing & Programming in
System Development Java
105
javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial Unicode
MS", 0, 14), new java.awt.Color(255, 153, 0))); // NOI18N
jLabel5.setText("Course Name :");
jLabel3.setText("Duration :");
jLabel1.setText("Course ID :");
txtcourseID.setDisabledTextColor(new java.awt.Color(255, 51, 51));
txtcourseID.setEnabled(false);
jLabel4.setText("Course fee :");
txtFee.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
txtFeeActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new
javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.
LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING
)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILIN
G, false)
.addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addComponent(jLabel3)
.addComponent(jLabel4))
HND in Computing & Programming in
System Development Java
106
.addGap(29, 29, 29)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING
)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILIN
G, false)
.addComponent(txtDuration, javax.swing.GroupLayout.Alignment.LEADING,
javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE)
.addComponent(txtFee)
.addComponent(txtcourseID, javax.swing.GroupLayout.Alignment.LEADING,
javax.swing.GroupLayout.PREFERRED_SIZE, 40,
javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(txtcsName,
javax.swing.GroupLayout.PREFERRED_SIZE, 221,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(28, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E) .addComponent(jLabel1)
.addComponent(txtcourseID, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E) .addComponent(jLabel5)
.addComponent(txtcsName, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E) .addComponent(jLabel3)
.addComponent(txtDuration, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(12, 12, 12)
HND in Computing & Programming in
System Development Java
107
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E) .addComponent(jLabel4)
.addComponent(txtFee, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(53, 53, 53))
);
jLabel1.getAccessibleContext().setAccessibleName("Course ID :");
btnEdit.setText("Update ");
btnEdit.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
btnEditActionPerformed(evt);
}
});
btnClear.setText("Clear");
btnClear.setHorizontalTextPosition(javax.swing.SwingConstant
s.RIGHT); btnClear.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
btnClearActionPerformed(evt);
}
});
btnCancel.setText("OK");
btnCancel.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
btnCancelActionPerformed(evt);
}
});
jLabel6.setText("He Institute ");
jLabel6.setEnabled(false);
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout); layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
HND in Computing & Programming in
System Development Java
108
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(23, 23, 23)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(btnClear)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnEdit)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 148,
Short.MAX_VALUE)
.addComponent(btnCancel))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel6)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jSeparator2)))))
.addContainerGap())
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[]
{btnCancel, btnClear, btnEdit});
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(34, 34, 34)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 165,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10,
javax.swing.GroupLayout.PREFERRED_SIZE)
HND in Computing & Programming in
System Development Java
109
.addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 19,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 30,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnClear)
.addComponent(btnCancel))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
);
layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[]
{btnCancel, btnClear, btnEdit});
pack();
}// </editor-fold>
public void clearAll(){
txtcsName.setText("");
txtDuration.setText("");
txtFee.setText("");
}
//----------------------end of clearing-----------
public void updateCourseDetails() throws ClassNotFoundException, SQLException
{
// connection to the sql server
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://Anushka-
PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;";
Connection connection = DriverManager.getConnection(connectionUrl);
System.out.println("Successfuly Connected ");
Statement statement = connection.createStatement();
String sql = "UPDATE Course_Details SET Course_Name='"+ txtcsName.getText()+ "'" +
","+
"Duration='"+ txtDuration.getText()+ "'" + ","+
"Course_Fee='"+ txtFee.getText() + "'" +
HND in Computing & Programming in
System Development Java
110
"WHERE Course_ID='"+ txtcourseID.getText() +"'";
statement.executeUpdate(sql);
}
//--------------------------End Update--------------------------------
public void setCourseDetails(String CS_id) throws ClassNotFoundException, SQLException{
// sql server connection
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://Anushka-
PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;";
Connection connection = DriverManager.getConnection(connectionUrl);
System.out.println("Successfuly Connected ");
String SQL1 = " SELECT * FROM Course_Details WHERE Course_ID ='"+ CS_id +"'";
Statement stmt2 =
connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet rls = stmt2.executeQuery(SQL1);
rls.first();
txtcsName.setText(rls.getString(2));
txtDuration.setText(rls.getString(3));
txtFee.setText(rls.getString(4));
}
//--------------------------end setting-------------------------------
private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {
try{
int respond =JOptionPane.showConfirmDialog(this, "Are You Sure You
Want To Update This record?","Confirm
HND in Computing & Programming in
System Development Java
111
Update",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE)
; if (respond==0){ this.updateCourseDetails();
this.setVisible(false);
}
else{
}
}//end try
catch (ClassNotFoundException e){
System.out.println(e);
}
catch (SQLException e){
JOptionPane.showMessageDialog(null, e, "ERROR",
JOptionPane.ERROR_MESSAGE);
}
}
private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.clearAll();
}
private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.setVisible(false);
}
private void txtFeeActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
HND in Computing & Programming in
System Development Java
112
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting
code (optional) "> /* If Nimbus (introduced in Java SE 6) is not
available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) { if
("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(EditCourseDetails.class.getName()).log(java.util.logging.Le
vel.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(EditCourseDetails.class.getName()).log(java.util.logging.Le
vel.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(EditCourseDetails.class.getName()).log(java.util.logging.Le
vel.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(EditCourseDetails.class.getName()).log(java.util.logging.Le
vel.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new
Runnable() { public void run() {
new EditCourseDetails("Cs").setVisible(true);
}
});
}
// Variables declaration - do not modify
HND in Computing & Programming in
System Development Java
113
private javax.swing.JButton
btnCancel; private
javax.swing.JButton btnClear;
private javax.swing.JButton btnEdit;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JPanel jPanel1;
private javax.swing.JSeparator
jSeparator2; private
javax.swing.JTextField txtDuration;
private javax.swing.JTextField
txtFee; private
javax.swing.JTextField txtcourseID;
private javax.swing.JTextField
txtcsName;
// End of variables
declaration }
Batch details Form
Figure 40
HND in Computing & Programming in
System Development Java
114
Program Code:
/*
* To change this license header, choose License Headers in
Project Properties. * To change this template file, choose Tools |
Templates * and open the template in the editor.
*/
package heinstitute; import
java.sql.Connection;
import
java.sql.DriverManager;
import java.sql.ResultSet;
import
java.sql.SQLException;
import java.sql.Statement;
import
javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/**
*
* @author Anushka
*/
public class BatchDetails extends javax.swing.JFrame {
/**
* Creates new form BatchDetails
*/
DefaultTableModel
model; public
BatchDetails(boolean w) {
initComponents();
model = new DefaultTableModel();
model.addColumn("Batch_ID");
model.addColumn("Start_Date");
model.addColumn("End_Date");
model.addColumn("Number_of_Students");
model.addColumn("Course_ID");
tblBatch.setModel(model);
HND in Computing & Programming in
System Development Java
115
this.setUserLimitations(w);
}
/**
* This method is called from within the constructor to
initialize the form. * WARNING: Do NOT modify this code.
The content of this method is always * regenerated by the
Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new
javax.swing.JScrollPane(); tblBatch
= new javax.swing.JTable(); btnEdit
= new javax.swing.JButton();
btnAdd = new javax.swing.JButton();
btnCancel = new javax.swing.JButton();
txtbatchID = new
javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
tblBatch.setModel(new
javax.swing.table.DefaultTableModel( new Object
[][] {
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4", "Title 5"
}
) {
boolean[] canEdit = new boolean [] {
true, true, true, true, false
};
HND in Computing & Programming in
System Development Java
116
public boolean isCellEditable(int rowIndex, int
columnIndex) { return canEdit [columnIndex];
}
});
jScrollPane1.setViewportView(tblBatch);
if
(tblBatch.getColumnModel().getColumnCount()
> 0) {
tblBatch.getColumnModel().getColumn(4).setResizable(false);
}
btnEdit.setBackground(new java.awt.Color(0, 0, 0));
btnEdit.setForeground(new java.awt.Color(255, 255, 255));
btnEdit.setText("Edit Batch");
btnEdit.setHorizontalTextPosition(javax.swing.SwingConstant
s.RIGHT); btnEdit.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
btnEditActionPerformed(evt);
}
});
btnAdd.setBackground(new java.awt.Color(0, 0,
0)); btnAdd.setForeground(new
java.awt.Color(255, 255, 255));
btnAdd.setText("Add Batch");
btnAdd.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
btnAddActionPerformed(evt);
}
});
btnCancel.setBackground(new java.awt.Color(255, 153, 0));
btnCancel.setText("OK");
btnCancel.setHorizontalTextPosition(javax.swing.SwingConstant
s.RIGHT); btnCancel.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
btnCancelActionPerformed(evt);
}
});
HND in Computing & Programming in
System Development Java
117
txtbatchID.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
txtbatchIDActionPerformed(evt);
}
});
jLabel1.setText("Batch ID");
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout); layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI
NG)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(568, Short.MAX_VALUE)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(txtbatchID, javax.swing.GroupLayout.PREFERRED_SIZE, 120,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(12, 12, 12)
.addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 93,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 93,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 73,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 729,
Short.MAX_VALUE) .addContainerGap()))
);
HND in Computing & Programming in
System Development Java
118
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtbatchID, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 310,
Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 30,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 30,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 30,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(48, 48, 48)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 282,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(52, Short.MAX_VALUE)))
);
pack();
}// </editor-fold>
public void setUserLimitations(boolean prm){
btnEdit.setEnabled(prm); btnAdd.setEnabled(prm);
}
public void findBatchDetails() throws ClassNotFoundException,
SQLException{ // connect to the sql server
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://Anushka-
PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;";
HND in Computing & Programming in
System Development Java
119
Connection connection = DriverManager.getConnection(connectionUrl);
System.out.println("Successfuly Connected ");
String SQL1 = " SELECT * FROM Batch_Details WHERE Batch_ID LIKE" + "'%"+
txtbatchID.getText() +"%'"; Statement stmt2 =
connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet rls1 = stmt2.executeQuery(SQL1);
this.refreshTable();
while(rls1.next())
{
model.addRow(new Object[]{
rls1.getString(1),
rls1.getString(2),
rls1.getString(3),
rls1.getString(4),
rls1.getString(5),
}
);
}
}
//
public void refreshTable(){
while (model.getRowCount()
> 0) {
model.removeRow(0);
}
}
private void
btnEditActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here: if
(tblBatch.getSelectedRow() <0 ||
tblBatch.getSelectedColumn()<0) {
HND in Computing & Programming in
System Development Java
120
JOptionPane.showMessageDialog(this, "Please Select record to edit!", "NO RECORD
SELECTED", JOptionPane.WARNING_MESSAGE);
}
else {
String s = (String) tblBatch.getValueAt(tblBatch.getSelectedRow(), 0);
EditBatchDetails editbatch = new EditBatchDetails(s);
editbatch.setVisible(true);
editbatch.setLocationRelativeTo(null);
editbatch.setResizable(false);
}
}
private void
btnAddActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here: AddBatch
addbatch = new AddBatch();
addbatch.setVisible(true);
addbatch.setVisible(true);
addbatch.setResizable(false);
addbatch.setLocationRelativeTo(null);
}
private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.setVisible(false);
}
private void txtbatchIDActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{
if
(txtbatchID.getText().equals("")){
//txtcake empty
JOptionPane.showMessageDialog(null, "The Cake Name Field Is Empty,Enter Cake Name
And Search", "EMPTY FIELD", JOptionPane.ERROR_MESSAGE);
}
else{//txtxCake not empty
this.findBatchDetails();
if(model.getRowCount()==0){
HND in Computing & Programming in
System Development Java
121
JOptionPane.showMessageDialog(this, "No Data Found", "NO DATA",
JOptionPane.ERROR_MESSAGE);
}
}
}//end try
catch (ClassNotFoundException e){
System.out.println(e);
}
catch (SQLException e){
JOptionPane.showMessageDialog(null, e, "ERROR IN CONNECTION",
JOptionPane.ERROR_MESSAGE);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting
code (optional) "> /* If Nimbus (introduced in Java SE 6) is not
available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) { if
("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
HND in Computing & Programming in
System Development Java
122
java.util.logging.Logger.getLogger(BatchDetails.class.getName()).log(java.util.logging.Level.S
EVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(BatchDetails.class.getName()).log(java.util.logging.Level.S
EVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(BatchDetails.class.getName()).log(java.util.logging.Level.S
EVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(BatchDetails.class.getName()).log(java.util.logging.Level.S
EVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new
Runnable() { public void run() {
//new BatchDetails().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnAdd;
private javax.swing.JButton btnCancel;
private javax.swing.JButton btnEdit;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane
jScrollPane1; private
javax.swing.JTable tblBatch; private
javax.swing.JTextField txtbatchID;
// End of variables declaration
}
HND in Computing & Programming in
System Development Java
123
Add New Batch Details Form
Program Code:
/*
* To change this license header, choose License Headers in Project Properties. * To change this
template file, choose Tools | Templates * and open the template in the editor.
*/
package heinstitute;
import
java.sql.Connection;
import
java.sql.DriverManager;
import
java.sql.ResultSet;
import
java.sql.SQLException;
import
java.sql.Statement;
import javax.swing.JOptionPane;
/**
*
HND in Computing & Programming in
System Development Java
124
* @author Anushka
*/
public class AddBatch extends javax.swing.JFrame {
/**
* Creates new form AddBatch
*/
public AddBatch() {
initComponents();
try{
this.generateID();
}//end try
catch (ClassNotFoundException e){
System.out.println(e);
}
catch (SQLException e){
System.out.println(e);
}
}
/**
* This method is called from within the constructor to
initialize the form. * WARNING: Do NOT modify this code.
The content of this method is always * regenerated by the
Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
txtStartDate = new
javax.swing.JFormattedTextField();
txtEndDate = new
HND in Computing & Programming in
System Development Java
125
javax.swing.JFormattedTextField();
txtNumofst = new javax.swing.JTextField();
txtCsID = new javax.swing.JTextField();
txtBatchID = new javax.swing.JTextField();
btnClear = new javax.swing.JButton();
btnAdd = new javax.swing.JButton();
btnOk = new javax.swing.JButton();
jSeparator2 = new javax.swing.JSeparator();
jLabel6 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Add New Batch",
javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 0, 12),
new java.awt.Color(255, 153, 0))); // NOI18N
jLabel1.setText("Batch ID :");
jLabel2.setText("Start Date :");
jLabel3.setText("End Date :");
jLabel4.setText("Number of students :");
jLabel5.setText("Course ID :");
txtStartDate.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new
javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("dd/mm/yyyy"))));
txtStartDate.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
txtEndDate.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new
javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("dd/mm/yyyy"))));
txtEndDate.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
txtBatchID.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
txtBatchID.setForeground(new java.awt.Color(240,
240, 240)); txtBatchID.setDisabledTextColor(new
java.awt.Color(255, 0, 0));
txtBatchID.setEnabled(false);
HND in Computing & Programming in
System Development Java
126
javax.swing.GroupLayout jPanel1Layout = new
javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING
)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 109,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(txtNumofst, javax.swing.GroupLayout.PREFERRED_SIZE, 73,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILIN
G)
.addComponent(txtStartDate, javax.swing.GroupLayout.PREFERRED_SIZE,
171, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
false)
.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING
) .addGroup(jPanel1Layout.createSequentialGroup()
.addGap(59, 59, 59)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING
)
HND in Computing & Programming in
System Development Java
127
.addComponent(txtEndDate,
javax.swing.GroupLayout.PREFERRED_SIZE, 171,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtCsID,
javax.swing.GroupLayout.PREFERRED_SIZE, 73,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtBatchID, javax.swing.GroupLayout.PREFERRED_SIZE, 88,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(81, 81, 81))))))
.addContainerGap(74, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E) .addComponent(jLabel1)
.addComponent(txtBatchID, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(14, 14, 14)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING
) .addComponent(jLabel2)
.addComponent(txtStartDate, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E) .addComponent(jLabel3)
.addComponent(txtEndDate, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(14, 14, 14)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E)
.addComponent(jLabel4)
HND in Computing & Programming in
System Development Java
128
.addComponent(txtNumofst, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E)
.addComponent(jLabel5)
.addComponent(txtCsID, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(37,
Short.MAX_VALUE))
);
btnClear.setBackground(new java.awt.Color(0, 0, 0));
btnClear.setForeground(new java.awt.Color(255, 255, 255));
btnClear.setText("CLEAR");
btnClear.setPreferredSize(new java.awt.Dimension(70,
33)); btnClear.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
btnClearActionPerformed(evt);
}
});
btnAdd.setBackground(new java.awt.Color(0, 0, 0));
btnAdd.setForeground(new java.awt.Color(255, 255, 255));
btnAdd.setText("Save");
btnAdd.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
btnAddActionPerformed(evt);
}
});
btnOk.setBackground(new java.awt.Color(255, 153, 0));
btnOk.setText("Ok");
btnOk.setHorizontalTextPosition(javax.swing.SwingConstants.
CENTER); btnOk.setPreferredSize(new
java.awt.Dimension(70, 33));
btnOk.addActionListener(new java.awt.event.ActionListener()
{ public void
HND in Computing & Programming in
System Development Java
129
actionPerformed(java.awt.event.ActionEvent evt) {
btnOkActionPerformed(evt);
}
});
jLabel6.setText("He Institute ");
jLabel6.setEnabled(false);
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout); layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(btnClear, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 82,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnOk, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGap(6, 6, 6)
.addComponent(jLabel6)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 316,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[]
{btnAdd, btnClear});
layout.setVerticalGroup(
HND in Computing & Programming in
System Development Java
130
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(13, 13, 13)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 19,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24,
Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnAdd)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnClear, javax.swing.GroupLayout.PREFERRED_SIZE, 24,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnOk, javax.swing.GroupLayout.PREFERRED_SIZE, 19,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(19, 19, 19))
);
layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[]
{btnAdd, btnClear, btnOk});
pack();
}// </editor-fold>
public void generateID()throws ClassNotFoundException, SQLException{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://Anushka-
PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;";
Connection connection = DriverManager.getConnection(connectionUrl);
System.out.println("The database is Connected ");
String sql="SELECT * FROM Batch_Details ORDER BY Batch_ID ASC;";
Statement stmt2 = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
HND in Computing & Programming in
System Development Java
131
ResultSet rs =
stmt2.executeQuery(sql); int id;
if(!rs.next()){ id=1;
txtBatchID.setText(Integer.toString(id));
}
else{
rs.last()
;
id=Inte
ger.valu
eOf(rs.
getStrin
g(1));
txtBatchID.setText(Integer.toString(id+1));
}
}
public void AddBatch() throws ClassNotFoundException, SQLException{
// connection to the sql server
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://Anushka-
PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;";
Connection connection = DriverManager.getConnection(connectionUrl);
System.out.println("The database is Connected ");
Statement statement = connection.createStatement();
if(txtStartDate.getText().equals("")){
JOptionPane.showMessageDialog(null, "The Start Date Field Is Empty,Enter the Start Date
", "EMPTY FIELD", JOptionPane.ERROR_MESSAGE);
}
else if(txtNumofst.getText().equals("")){
JOptionPane.showMessageDialog(null, "The Number of student Field Is Empty,Enter the
Number of student ", "EMPTY FIELD", JOptionPane.ERROR_MESSAGE);
}
else if(txtCsID.getText().equals("")){
JOptionPane.showMessageDialog(null, "The Course ID Field Is Empty,Enter a Course ID",
"EMPTY FIELD", JOptionPane.ERROR_MESSAGE);
HND in Computing & Programming in
System Development Java
132
}
else {
statement.executeUpdate("INSERT INTO Batch_Details VALUES " +"( '" +
txtBatchID.getText() + "'" + "," +
"'" + txtStartDate.getText() + "'" + "," +
"'" + txtEndDate.getText()+ "'" + "," +
"'" + txtNumofst.getText()+ "'" + "," +
"'" + txtCsID.getText() + "'" + ")" + "");
JOptionPane.showMessageDialog(null, "Record has been saved in the database", "INSERT
SUCCESSFUL", JOptionPane.INFORMATION_MESSAGE);
}
}
public void clearAll(){
txtStartDate.setText("");
txtBatchID.setText("");
txtEndDate.setText("");
txtNumofst.setText("");
txtCsID.setText("");
}
private void
btnClearActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here: this.clearAll();
}
private void
btnAddActionPerforme
d(java.awt.event.Actio
nEvent evt) {
// TODO add your
handling code here:
try{
HND in Computing & Programming in
System Development Java
133
this.AddBatch();//add new user to
database this.clearAll();
this.generateID();
}//end try
catch (ClassNotFoundException e){
System.out.println(e);
}
catch (SQLException e){
JOptionPane.showMessageDialog(this, e, "ERROR",
JOptionPane.ERROR_MESSAGE);
}
}
private void btnOkActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.setVisible(false);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting
code (optional) "> /* If Nimbus (introduced in Java SE 6) is not
available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) { if
("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
HND in Computing & Programming in
System Development Java
134
java.util.logging.Logger.getLogger(AddBatch.class.getName()).log(java.util.logging.Level.SEV
ERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(AddBatch.class.getName()).log(java.util.logging.Level.SEV
ERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(AddBatch.class.getName()).log(java.util.logging.Level.SEV
ERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(AddBatch.class.getName()).log(java.util.logging.Level.SEV
ERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new
Runnable() { public void run() {
new AddBatch().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton
btnAdd; private
javax.swing.JButton btnClear;
private javax.swing.JButton btnOk;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JPanel jPanel1;
private javax.swing.JSeparator
jSeparator2; private
javax.swing.JTextField txtBatchID;
private javax.swing.JTextField
HND in Computing & Programming in
System Development Java
135
txtCsID; private
javax.swing.JFormattedTextField
txtEndDate; private
javax.swing.JTextField txtNumofst;
private
javax.swing.JFormattedTextField
txtStartDate;
// End of variables declaration
}
Edit Batch Detail Form
Figure 41
Program Code:
/*
* To change this license header, choose License Headers in Project Properties. * To change this
template file, choose Tools | Templates * and open the template in the editor.
*/
package heinstitute; import
java.sql.Connection;
import
HND in Computing & Programming in
System Development Java
136
java.sql.DriverManager;
import java.sql.ResultSet;
import
java.sql.SQLException;
import java.sql.Statement;
import
javax.swing.JOptionPane;
/**
*
* @author Anushka
*/
public class EditBatchDetails extends javax.swing.JFrame {
/**
* Creates new form EditBatchDetails
*/
public EditBatchDetails(String id) {
initComponents();
txtBatchID.setText(id);
try{
this.setBatchDetails(id);
}//end try
catch (ClassNotFoundException e){
System.out.println(e);
}
catch (SQLException e){
JOptionPane.showMessageDialog(null, e, "ERROR", JOptionPane.ERROR_MESSAGE);
}
}
/**
* This method is called from within the constructor to
initialize the form. * WARNING: Do NOT modify this code.
The content of this method is always * regenerated by the
Form Editor.
*/
@SuppressWarnings("unchecked")
HND in Computing & Programming in
System Development Java
137
public void clearAll(){
txtBatchID.setText("");
txtStartDate.setText("");
txtEndDate.setText("");
txtNumofst.setText("");
txtCsID.setText("");
}// end of clear all
public void updateBatchDetails() throws ClassNotFoundException, SQLException
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://Anushka-
PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;";
Connection connection = DriverManager.getConnection(connectionUrl);
System.out.println("The database is Connected ");
Statement statement = connection.createStatement();
String sql = "UPDATE Batch_Details SET Start_date='"+ txtStartDate.getText()+ "'" +
","+
"End_date='"+ txtEndDate.getText()+ "'" + ","+
"number_Of_Student='"+ txtNumofst.getText() + "'" +
"Course_ID='"+ txtCsID.getText() + "'" +
"WHERE Batch_ID='"+ txtBatchID.getText() +"'";
statement.executeUpdate(sql);
}
//-------------------------end update method---------------------------
public void setBatchDetails(String Bt_id) throws
ClassNotFoundException, SQLException{ // connection to the sql server
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
HND in Computing & Programming in
System Development Java
138
String connectionUrl = "jdbc:sqlserver://Anushka-
PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;";
Connection connection = DriverManager.getConnection(connectionUrl);
System.out.println("The database is Connected "); // connection ok
String SQL3 = " SELECT * FROM Batch_Details WHERE Batch_ID='"+ Bt_id +"'";
Statement stmt2 =
connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet rslst = stmt2.executeQuery(SQL3);
rslst.first();
txtBatchID.setText(rslst.getString(1));
txtStartDate.setText(rslst.getString(2));
txtEndDate.setText(rslst.getString(3));
txtNumofst.setText(rslst.getString(4));
txtCsID.setText(rslst.getString(5));
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
txtStartDate = new
javax.swing.JFormattedTextField();
txtEndDate = new
javax.swing.JFormattedTextField();
txtNumofst = new javax.swing.JTextField();
txtCsID = new javax.swing.JTextField();
txtBatchID = new javax.swing.JTextField();
btnClear = new javax.swing.JButton();
btnEdit1 = new javax.swing.JButton();
btnCancel = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
HND in Computing & Programming in
System Development Java
139
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Edit Batch Details",
javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 0, 12),
new java.awt.Color(255, 153, 0))); // NOI18N
jLabel1.setText("Batch ID :");
jLabel2.setText("Start Date :");
jLabel3.setText("End Date :");
jLabel4.setText("Number of students :");
jLabel5.setText("Course ID :");
txtStartDate.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new
javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("dd/mm/yyyy"))));
txtStartDate.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
txtEndDate.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new
javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("dd/mm/yyyy"))));
txtEndDate.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
txtBatchID.setDisabledTextColor(new java.awt.Color(255, 51, 51));
txtBatchID.setEnabled(false);
javax.swing.GroupLayout jPanel1Layout = new
javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING
)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
false)
.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
HND in Computing & Programming in
System Development Java
140
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING
)
.addComponent(txtStartDate, javax.swing.GroupLayout.PREFERRED_SIZE,
171, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtEndDate, javax.swing.GroupLayout.PREFERRED_SIZE,
171, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtCsID, javax.swing.GroupLayout.PREFERRED_SIZE, 73,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtBatchID, javax.swing.GroupLayout.PREFERRED_SIZE,
40, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 109,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(txtNumofst, javax.swing.GroupLayout.PREFERRED_SIZE, 73,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(67, Short.MAX_VALUE))
);
jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new
java.awt.Component[] {jLabel4, jLabel5});
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E) .addComponent(jLabel1)
.addComponent(txtBatchID, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
HND in Computing & Programming in
System Development Java
141
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E)
.addComponent(jLabel2)
.addComponent(txtStartDate, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING
)
.addComponent(jLabel3)
.addComponent(txtEndDate, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E)
.addComponent(jLabel4)
.addComponent(txtNumofst, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E)
.addComponent(jLabel5)
.addComponent(txtCsID, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(37,
Short.MAX_VALUE))
);
btnClear.setBackground(new java.awt.Color(0, 0, 0));
btnClear.setForeground(new java.awt.Color(255, 255, 255));
btnClear.setText("Clear");
btnClear.setHorizontalTextPosition(javax.swing.SwingConstant
s.RIGHT); btnClear.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
btnClearActionPerformed(evt);
}
});
HND in Computing & Programming in
System Development Java
142
btnEdit1.setBackground(new java.awt.Color(0, 0, 0));
btnEdit1.setForeground(new java.awt.Color(255, 255, 255));
btnEdit1.setText("Update ");
btnEdit1.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
btnEdit1ActionPerformed(evt);
}
});
btnCancel.setBackground(new java.awt.Color(255, 153, 0));
btnCancel.setText("OK");
btnCancel.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
btnCancelActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout); layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(btnClear)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnEdit1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnCancel)))
.addContainerGap())
);
HND in Computing & Programming in
System Development Java
143
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[]
{btnCancel, btnClear, btnEdit1});
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(49, 49, 49)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnEdit1, javax.swing.GroupLayout.DEFAULT_SIZE, 30,
Short.MAX_VALUE) .addComponent(btnCancel))
.addComponent(btnClear, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
);
layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[]
{btnCancel, btnClear, btnEdit1});
pack();
}// </editor-fold>
private void btnEdit1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{
int respond =JOptionPane.showConfirmDialog(this, "Do You Want To Update This
record?","Confirm
Update",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTIO
N_MESSAGE); if (respond==0){
this.updateBatchDetails();
this.setVisible(false);
}
else{
}
}//end try
HND in Computing & Programming in
System Development Java
144
catch (ClassNotFoundException e){
System.out.println(e);
}
catch (SQLException e){
JOptionPane.showMessageDialog(null, e, "ERROR", JOptionPane.ERROR_MESSAGE);
}
}
private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.setVisible(false);
}
private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.clearAll();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting
code (optional) "> /* If Nimbus (introduced in Java SE 6) is not
available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) { if
("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
HND in Computing & Programming in
System Development Java
145
java.util.logging.Logger.getLogger(EditBatchDetails.class.getName()).log(java.util.logging.Lev
el.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(EditBatchDetails.class.getName()).log(java.util.logging.Lev
el.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(EditBatchDetails.class.getName()).log(java.util.logging.Lev
el.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(EditBatchDetails.class.getName()).log(java.util.logging.Lev
el.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new
Runnable() { public void run() {
new EditBatchDetails("Bt").setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnCancel;
private javax.swing.JButton btnClear;
private javax.swing.JButton btnEdit1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JPanel jPanel1; private
javax.swing.JTextField txtBatchID;
private javax.swing.JTextField txtCsID;
private javax.swing.JFormattedTextField
txtEndDate; private
javax.swing.JTextField txtNumofst;
private javax.swing.JFormattedTextField txtStartDate;
HND in Computing & Programming in
System Development Java
146
// End of variables declaration
}
Create New User Account Form
Figure 42
Program Code:
/*
* To change this license header, choose License Headers in
Project Properties. * To change this template file, choose Tools |
Templates * and open the template in the editor.
*/
package heinstitute; import
java.sql.Connection; import
java.sql.DriverManager; import
java.sql.ResultSet; import
java.sql.SQLException; import
java.sql.Statement; import
HND in Computing & Programming in
System Development Java
147
javax.swing.JOptionPane; import
javax.swing.table.DefaultTableMod
el;
/**
*
* @author Anushka
*/
public class AddUser extends javax.swing.JFrame {
/**
* Creates new form AddUser
*/
DefaultTableModel model;
public AddUser() {
initComponents();
try{
this.generateID();
}//end try
catch (ClassNotFoundException e){
System.out.println(e);
}
catch (SQLException e){
System.out.println(e);
}
}
/**
* This method is called from within the constructor to
initialize the form. * WARNING: Do NOT modify this code.
The content of this method is always * regenerated by the
Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
HND in Computing & Programming in
System Development Java
148
btnOk = new javax.swing.JButton();
btnAdd = new javax.swing.JButton();
txtStaffid = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
btnClear = new javax.swing.JButton();
jLabel5 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
txtStaffName = new
javax.swing.JTextField(); txtPassword
= new javax.swing.JPasswordField();
jLabel7 = new javax.swing.JLabel();
txtUserName = new
javax.swing.JTextField(); jPanel1 =
new javax.swing.JPanel(); rbStaff =
new javax.swing.JRadioButton();
rbAdmin = new javax.swing.JRadioButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
btnOk.setText("Ok");
btnOk.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
btnOk.setPreferredSize(new java.awt.Dimension(70, 33)); btnOk.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) { btnOkActionPerformed(evt);
}
});
btnAdd.setText("Save");
btnAdd.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
btnAddActionPerformed(evt);
}
});
txtStaffid.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
txtStaffid.setForeground(new java.awt.Color(240, 240, 240));
txtStaffid.setDisabledTextColor(new java.awt.Color(255, 0, 0));
txtStaffid.setEnabled(false);
jLabel2.setText("Password");
HND in Computing & Programming in
System Development Java
149
btnClear.setText("CLEAR");
btnClear.setPreferredSize(new java.awt.Dimension(70,
33)); btnClear.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
btnClearActionPerformed(evt);
}
});
jLabel5.setText(" Staff Name");
jLabel3.setText("User Name");
jLabel7.setText(" Staff ID");
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Account Type",
javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12))); //
NOI18N
rbStaff.setSelected(true);
rbStaff.setText("Staff Account");
rbAdmin.setText("Admin Account");
javax.swing.GroupLayout jPanel1Layout = new
javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createSequentialGroup() .addGap(21, 21, 21)
.addComponent(rbAdmin)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35,
Short.MAX_VALUE) .addComponent(rbStaff)
.addGap(18, 18, 18))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(16, 16, 16)
HND in Computing & Programming in
System Development Java
150
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E) .addComponent(rbStaff)
.addComponent(rbAdmin))
.addContainerGap(20, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout); layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 63,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 72,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtStaffid, javax.swing.GroupLayout.PREFERRED_SIZE, 88,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtStaffName)))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 6,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
HND in Computing & Programming in
System Development Java
151
.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 72,
Short.MAX_VALUE) .addGroup(layout.createSequentialGroup()
.addGap(6, 6, 6)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(txtUserName)
.addComponent(txtPassword,
javax.swing.GroupLayout.PREFERRED_SIZE, 174,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addGap(30, 30, 30)
.addComponent(btnClear, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 82,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(btnOk, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(0, 117, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(36, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtStaffid, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel7))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
HND in Computing & Programming in
System Development Java
152
.addComponent(txtStaffName, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 20,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtUserName, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(28, 28, 28)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnOk, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnClear, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 33,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(20, 20, 20))
);
pack();
}// </editor-fold>
public void generateID()throws ClassNotFoundException, SQLException{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://Anushka-
PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;";
Connection connection = DriverManager.getConnection(connectionUrl);
System.out.println("The database is Connected "); // connection ok
String sql="SELECT * FROM Staff ORDER BY Staff_ID ASC;";
Statement stmt2 = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
HND in Computing & Programming in
System Development Java
153
ResultSet.CONCUR_READ_ONLY);
ResultSet rs =
stmt2.executeQuery(sql); int id;
if(!rs.next()){ id=1;
txtStaffid.setText(Integer.toString(id));
}
else{
rs.last()
;
id=Integer.valueOf(rs.getString(1));
txtStaffid.setText(Integer.toString(id+1));
}
}// end of generateID
public void clearAll(){
txtStaffName.setText("");
txtUserName.setText("");
txtPassword.setText("");
}// end **********************************
public void add_User() throws ClassNotFoundException, SQLException{
// connection to the sql server
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://Anushka-
PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;";
Connection connection = DriverManager.getConnection(connectionUrl);
System.out.println("Successfuly Connected to MS SQL 2005 using windows authentication
1"); // connection ok
// inserting new user
Statement statement = connection.createStatement();
if(txtStaffName.getText().equals("")){
HND in Computing & Programming in
System Development Java
154
JOptionPane.showMessageDialog(null, "The Staff Name Field Is Empty,Enter Staff
Name", "EMPTY FIELD", JOptionPane.ERROR_MESSAGE);
}
else if(txtUserName.getText().equals("")){
JOptionPane.showMessageDialog(null, "The User Name Field Is Empty,Enter User Name",
"EMPTY FIELD", JOptionPane.ERROR_MESSAGE);
}
else if(txtPassword.getText().equals("")){
JOptionPane.showMessageDialog(null, "The Password Field Is Empty,Enter Password",
"EMPTY FIELD", JOptionPane.ERROR_MESSAGE);
}
else {
statement.executeUpdate("INSERT INTO Staff VALUES " +"( '" + txtStaffid.getText()
+ "'" + "," +
"'" + txtStaffName.getText() + "'" + "," +
"'" + txtUserName.getText() + "'" + "," +
"'" + txtPassword.getText() + "'" + "," +
"'" + this.getAccType() + "'" + ")" +" ");
JOptionPane.showMessageDialog(null, "Record are saved in the database", "INSERT
SUCCESSFUL", JOptionPane.INFORMATION_MESSAGE);
}
}
public boolean getAccType(){
if(rbStaff.isSelected()==true){
return false;
}
else if(rbAdmin.isSelected()==true){
return true;
} else{
return false;
}
}
HND in Computing & Programming in
System Development Java
155
private void btnOkActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.setVisible(false);
}
private void
btnAddActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here: try{
this.add_User();//add new user to
database this.clearAll();
this.generateID();
}//end try
catch (ClassNotFoundException e){
System.out.println(e);
}
catch (SQLException e){
JOptionPane.showMessageDialog(this, e, "ERROR",
JOptionPane.ERROR_MESSAGE);
}
}
private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.clearAll();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting
code (optional) "> /* If Nimbus (introduced in Java SE 6) is not
available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
HND in Computing & Programming in
System Development Java
156
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) { if
("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(AddUser.class.getName()).log(java.util.logging.Level.SEVE
RE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(AddUser.class.getName()).log(java.util.logging.Level.SEVE
RE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(AddUser.class.getName()).log(java.util.logging.Level.SEVE
RE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(AddUser.class.getName()).log(java.util.logging.Level.SEVE
RE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new
Runnable() { public void run() {
new AddUser().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton
btnAdd; private
javax.swing.JButton btnClear;
private javax.swing.JButton btnOk;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
HND in Computing & Programming in
System Development Java
157
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel7;
private javax.swing.JPanel jPanel1;
private javax.swing.JRadioButton
rbAdmin; private
javax.swing.JRadioButton rbStaff;
private javax.swing.JPasswordField
txtPassword; private
javax.swing.JTextField
txtStaffName; private
javax.swing.JTextField txtStaffid;
private javax.swing.JTextField
txtUserName;
// End of variables declaration
}
Edit User Account Form
Figure 43
Program Code:
/*
* To change this license header, choose License Headers in Project Properties. * To change this
template file, choose Tools | Templates * and open the template in the editor.
*/
HND in Computing & Programming in
System Development Java
158
package heinstitute;
import
java.sql.Connection;
import
java.sql.DriverManager;
import
java.sql.ResultSet;
import
java.sql.SQLException;
import
java.sql.Statement;
import javax.swing.JOptionPane;
/**
*
* @author Anushka
*/
public class EditMyUserAcc extends javax.swing.JFrame {
/**
* Creates new form EditMyUserAcc
*/
public EditMyUserAcc(int staffid) {
initComponents();
try{
this.getDetails(staffid);
}//end try
catch (ClassNotFoundException e){
System.out.println(e);
}
catch (SQLException e){
JOptionPane.showMessageDialog(null, e, "ERROR", JOptionPane.ERROR_MESSAGE);
}
}
/**
* This method is called from within the constructor to
initialize the form. * WARNING: Do NOT modify this code.
The content of this method is always * regenerated by the
Form Editor.
*/
@SuppressWarnings("unchecked")
HND in Computing & Programming in
System Development Java
159
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
btnClear = new
javax.swing.JButton(); btnOk = new
javax.swing.JButton(); btnUpdate =
new javax.swing.JButton(); jPanel1
= new javax.swing.JPanel();
txtStaffid = new
javax.swing.JTextField();
txtUserName = new
javax.swing.JTextField(); jLabel2 =
new javax.swing.JLabel(); jLabel5
= new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
txtPassword = new
javax.swing.JTextField();
txtStaffName = new
javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
btnClear.setText("Clear");
btnClear.setPreferredSize(new java.awt.Dimension(70,
33)); btnClear.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
btnClearActionPerformed(evt);
}
});
btnOk.setText("Ok");
btnOk.setCursor(new
java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
btnOk.setHorizontalTextPosition(javax.swing.SwingConstants.C
ENTER); btnOk.setPreferredSize(new
java.awt.Dimension(70, 33)); btnOk.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
btnOkActionPerformed(evt);
}
HND in Computing & Programming in
System Development Java
160
});
btnUpdate.setText("Save");
btnUpdate.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
btnUpdateActionPerformed(evt);
}
});
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Edit User
Account",
javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 0, 12),
new java.awt.Color(255, 153, 0))); // NOI18N
txtStaffid.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
txtStaffid.setForeground(new java.awt.Color(240, 240, 240));
txtStaffid.setDisabledTextColor(new java.awt.Color(255, 51, 51));
txtStaffid.setEnabled(false);
jLabel2.setText("Password");
jLabel5.setText(" Staff Name");
jLabel3.setText("User Name");
jLabel7.setText(" Staff ID");
javax.swing.GroupLayout jPanel1Layout = new
javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING
)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 67,
javax.swing.GroupLayout.PREFERRED_SIZE)
HND in Computing & Programming in
System Development Java
161
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 62,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 72,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 63,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING
)
.addComponent(txtStaffid, javax.swing.GroupLayout.PREFERRED_SIZE, 105,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
false) .addComponent(txtPassword)
.addComponent(txtUserName)
.addComponent(txtStaffName, javax.swing.GroupLayout.PREFERRED_SIZE,
166, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(150, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(28, 28, 28)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILIN
G)
.addComponent(txtStaffid, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel7))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E) .addComponent(jLabel5)
.addComponent(txtStaffName, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 20,
javax.swing.GroupLayout.PREFERRED_SIZE)
HND in Computing & Programming in
System Development Java
162
.addComponent(txtUserName, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E) .addComponent(jLabel2)
.addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(29,
Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout); layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(btnUpdate)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnClear, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnOk, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(24, 24, 24))))
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[]
{btnClear, btnUpdate});
layout.setVerticalGroup(
HND in Computing & Programming in
System Development Java
163
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(36, 36, 36)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, 24,
Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnOk, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnClear, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 33,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);
pack();
}// </editor-fold>
public void getDetails(int Staffid)throws ClassNotFoundException, SQLException {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://Anushka-
PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;";
Connection connection = DriverManager.getConnection(connectionUrl);
System.out.println("Successfuly Connected");
String SQL1 = " SELECT * FROM Staff WHERE Staff_ID=" + "'"+Staffid+"'";
Statement stmt2 =
connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt2.executeQuery(SQL1);
rs.first();
txtStaffid.setText(rs.getString(1));
txtStaffName.setText(rs.getString(2));
txtUserName.setText(rs.getString(3));
txtPassword.setText(rs.getString(4));
HND in Computing & Programming in
System Development Java
164
}
public void update_user() throws ClassNotFoundException, SQLException
{
// connection to the sql server
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://Anushka-
PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;";
Connection connection = DriverManager.getConnection(connectionUrl);
// System.out.println("Successfuly Connected to MS SQL 2005 using windows
authentication 1"); // command
Statement statement = connection.createStatement();
String sql = "UPDATE Staff SET Staff_name='"+ txtStaffName.getText()+ "'" + ","+
"User_name='"+ txtUserName.getText() + "'" + ","+
"Password='" + txtPassword.getText() + "'" +
"WHERE Staff_ID='" + txtStaffid.getText() +"'";
int updateCount = statement.executeUpdate(sql);
}
public void clearAll(){
txtStaffName.setText("");
txtUserName.setText("");
txtPassword.setText("");
}
private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.clearAll();
}
HND in Computing & Programming in
System Development Java
165
private void btnOkActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.setVisible(false);
}
private void
btnUpdateActionPerformed(java.awt.event.ActionEvent evt)
{ // TODO add your handling
code here: try{
int respond =JOptionPane.showConfirmDialog(this, "Do You Want To Update This
Record?","Confirm
Update",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION
_MESSAGE); if (respond==0){
this.update_user();
}
else{
}
}//end try
catch (ClassNotFoundException e){
System.out.println(e);
}
catch (SQLException e){
JOptionPane.showMessageDialog(null, e, "ERROR",
JOptionPane.ERROR_MESSAGE);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting
code (optional) "> /* If Nimbus (introduced in Java SE 6) is not
available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
HND in Computing & Programming in
System Development Java
166
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) { if
("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(EditMyUserAcc.class.getName()).log(java.util.logging.Leve
l.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(EditMyUserAcc.class.getName()).log(java.util.logging.Leve
l.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(EditMyUserAcc.class.getName()).log(java.util.logging.Leve
l.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(EditMyUserAcc.class.getName()).log(java.util.logging.Leve
l.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new
Runnable() { public void run() {
//new EditMyUserAcc().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton
btnClear; private
javax.swing.JButton btnOk; private
javax.swing.JButton btnUpdate;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
HND in Computing & Programming in
System Development Java
167
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel7;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField
txtPassword; private
javax.swing.JTextField txtStaffName;
private javax.swing.JTextField
txtStaffid;
private javax.swing.JTextField txtUserName;
// End of variables declaration
}
Payment Details Form
Figure 44
Code :
/*
* To change this license header, choose License Headers in
Project Properties. * To change this template file, choose Tools |
Templates * and open the template in the editor.
*/
package heinstitute; import
java.sql.Connection;
import
HND in Computing & Programming in
System Development Java
168
java.sql.DriverManager;
import java.sql.ResultSet;
import
java.sql.SQLException;
import java.sql.Statement;
import
javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/**
*
* @author Anushka
*/
public class frmPaymentDetails extends javax.swing.JFrame {
/**
* Creates new form frmPaymentDetails
*/
DefaultTableModel model; public
frmPaymentDetails(boolean z) {
initComponents(); model = new
DefaultTableModel();
model.addColumn("Payment_code");
model.addColumn("Payment_Method")
;
model.addColumn("Credit_Card_Num
ber");
model.addColumn("Bank_Name");
model.addColumn("Total_Amount");
model.addColumn("Course_ID");
model.addColumn("Student_ID");
tblPayments.setModel(model);
this.setUserLimitations(z);
}
/**
* This method is called from within the constructor to
initialize the form. * WARNING: Do NOT modify this code.
The content of this method is always * regenerated by the
Form Editor.
*/
@SuppressWarnings("unchecked")
HND in Computing & Programming in
System Development Java
169
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new
javax.swing.JScrollPane();
tblPayments = new javax.swing.JTable();
btnAddPay = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
txtPaycode = new
javax.swing.JTextField(); btnDelete =
new javax.swing.JButton(); jButton1
= new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
tblPayments.setAutoCreateRowSorter(true);
tblPayments.setModel(new
javax.swing.table.DefaultTableModel( new Object
[][] {
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null}
},
new String [] {
"Title1", "Title2", "Title3", "Title4", "Title5", "Title6", "Title7"
}
));
jScrollPane1.setViewportView(tblPayments);
HND in Computing & Programming in
System Development Java
170
btnAddPay.setBackground(new java.awt.Color(0, 0, 0));
btnAddPay.setForeground(new java.awt.Color(255, 255,
255)); btnAddPay.setText("Receive Payment");
btnAddPay.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
btnAddPayActionPerformed(evt);
}
});
jLabel1.setText("Payment Code :");
txtPaycode.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
txtPaycodeActionPerformed(evt);
}
});
btnDelete.setBackground(new java.awt.Color(0, 0, 0));
btnDelete.setForeground(new java.awt.Color(255, 255, 255));
btnDelete.setText("DeletePayment Detail");
btnDelete.setHorizontalTextPosition(javax.swing.SwingConstant
s.RIGHT); btnDelete.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
btnDeleteActionPerformed(evt);
}
});
jButton1.setBackground(new java.awt.Color(255,
153, 0)); jButton1.setFont(new
java.awt.Font("Tahoma", 1, 10)); // NOI18N
jButton1.setText("OK");
jButton1.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
HND in Computing & Programming in
System Development Java
171
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout); layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 824,
Short.MAX_VALUE) .addGroup(layout.createSequentialGroup()
.addComponent(btnAddPay)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 149,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 79,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup() .addGap(0, 0, Short.MAX_VALUE)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtPaycode, javax.swing.GroupLayout.PREFERRED_SIZE, 105,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(18, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtPaycode, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 195,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(26, 26, 26)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
HND in Computing & Programming in
System Development Java
172
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnAddPay, javax.swing.GroupLayout.PREFERRED_SIZE, 30,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 30,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 31,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(21, 21, 21))
);
pack();
}// </editor-fold>
public void setUserLimitations(boolean prm){
btnDelete.setEnabled(prm);
}
public void findPaymentDetails() throws ClassNotFoundException,
SQLException{ // connect to the sql server
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://Anushka-
PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;";
Connection connection = DriverManager.getConnection(connectionUrl);
System.out.println("Successfuly Connected ");
String SQL1 = " SELECT * FROM Student_Details WHERE Student_ID LIKE" + "'%"+
txtPaycode.getText() +"%'";
Statement stmt2 =
connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet rls = stmt2.executeQuery(SQL1);
this.refreshTable();
while(rls.next())
{
model.addRow(new Object[]{
HND in Computing & Programming in
System Development Java
173
rls.getString(1),
rls.getString(2),
rls.getString(3),
rls.getString(4),
}
);
}
}
//--------refreshthe table-------
---- public void
refreshTable(){ while
(model.getRowCount() > 0) {
model.removeRow(0);
}
}
// Delete payment details-------------------
public void deletePaymentDetail(String selRow) throws ClassNotFoundException,
SQLException
{
// connect to the sql server
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://Anushka-
PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;";
Connection connection = DriverManager.getConnection(connectionUrl);
System.out.println("Successfuly Connected ");
Statement statement = connection.createStatement();
statement.executeUpdate("DELETE FROM Payment_Details WHERE Payment_code='" +
selRow + "'");
}
private void btnAddPayActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Addpay addpa = new Addpay ();
HND in Computing & Programming in
System Development Java
174
addpa.setVisible(true);
addpa.setResizable(false);
addpa.setLocationRelativeTo(null);
}
private void txtPaycodeActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{
if (txtPaycode.getText().equals("")){
//txtStudentID empty
JOptionPane.showMessageDialog(null, "The Payment Code Field Is Empty,Enter
Payment Code And Search", "EMPTY FIELD", JOptionPane.ERROR_MESSAGE);
}
else{//txtxCake not empty
this.findPaymentDetails();
if(model.getRowCount()==0){
JOptionPane.showMessageDialog(this, "No Data Found", "NO DATA",
JOptionPane.ERROR_MESSAGE);
}
}
}//end try
catch (ClassNotFoundException e){
System.out.println(e);
}
catch (SQLException e){
JOptionPane.showMessageDialog(null, e, "ERROR IN CONNECTION",
JOptionPane.ERROR_MESSAGE);
}
}
private void
btnDeleteActionPerformed(java.awt.event.ActionEvent evt)
{ // TODO add your handling
code here: try{
if (tblPayments.getSelectedRow() < 0 || tblPayments.getSelectedColumn() < 0) {
HND in Computing & Programming in
System Development Java
175
JOptionPane.showMessageDialog(this, "Select a Record From Table To Delete","NO
RECORD
SELECTED",JOptionPane.WARNING_MESSAGE);
}
else {
int respond =JOptionPane.showConfirmDialog(this, "Are You Sure You Want To
Delete This?","Confirm
Deletion",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTIO
N_MESSAGE); if (respond==0){
String selectedRec =
(String)tblPayments.getValueAt(tblPayments.getSelectedRow(), 0);
this.deletePaymentDetail(selectedRec); // deleting a payment from Db
this.refreshTable();
this.findPaymentDetails();
}
else{
}
}
}//end try
catch (ClassNotFoundException e){
System.out.println(e);
}
catch (SQLException e){
JOptionPane.showMessageDialog(null, e, "ERROR",
JOptionPane.ERROR_MESSAGE);
}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.setVisible(false);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting
code (optional) "> /* If Nimbus (introduced in Java SE 6) is not
available, stay with the default look and feel.
HND in Computing & Programming in
System Development Java
176
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) { if
("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(frmPaymentDetails.class.getName()).log(java.util.logging.L
evel.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(frmPaymentDetails.class.getName()).log(java.util.logging.L
evel.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(frmPaymentDetails.class.getName()).log(java.util.logging.L
evel.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(frmPaymentDetails.class.getName()).log(java.util.logging.L
evel.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new
Runnable() { public void run() {
// new frmPaymentDetails().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton
btnAddPay; private
javax.swing.JButton btnDelete;
HND in Computing & Programming in
System Development Java
177
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane
jScrollPane1; private
javax.swing.JTable tblPayments;
private javax.swing.JTextField
txtPaycode;
// End of variables declaration
}
Receive payment detail form.
Figure 45
Code :
/*
* To change this license header, choose License Headers in
Project Properties. * To change this template file, choose Tools |
Templates * and open the template in the editor.
*/
package heinstitute; import
java.sql.Connection; import
java.sql.DriverManager; import
java.sql.ResultSet; import
java.sql.SQLException; import
java.sql.Statement; import
HND in Computing & Programming in
System Development Java
178
javax.swing.JOptionPane; import
javax.swing.table.DefaultTableMod
el;
/**
*
* @author Anushka
*/
public class Addpay extends javax.swing.JFrame {
/**
* Creates new form Add pay
*/
DefaultTableModel
model; public Addpay() {
initComponents();
try{
this.generateID();
}//end try
catch (ClassNotFoundException e){
System.out.println(e);
}
catch (SQLException e){
System.out.println(e);
}
}
/**
* This method is called from within the constructor to
initialize the form. * WARNING: Do NOT modify this code.
The content of this method is always * regenerated by the
Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel2 = new javax.swing.JPanel();
txtcridetNO = new
HND in Computing & Programming in
System Development Java
179
javax.swing.JTextField(); jLabel6 =
new javax.swing.JLabel(); txtCiD =
new javax.swing.JPasswordField();
jLabel4 = new javax.swing.JLabel();
txtSiD = new
javax.swing.JPasswordField();
jLabel3 = new javax.swing.JLabel();
txtPaycode = new
javax.swing.JTextField(); jLabel2 =
new javax.swing.JLabel();
jComboBox1 = new
javax.swing.JComboBox(); jLabel8 =
new javax.swing.JLabel(); jPanel1 =
new javax.swing.JPanel(); rbCashe =
new javax.swing.JRadioButton();
rdbCcard = new
javax.swing.JRadioButton(); jLabel5
= new javax.swing.JLabel(); jLabel7
= new javax.swing.JLabel(); btnAdd
= new javax.swing.JButton(); txtTot
= new javax.swing.JTextField();
btnClear = new javax.swing.JButton();
btnOk = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Recive Payment",
javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12),
new java.awt.Color(255, 102, 0))); // NOI18N
jLabel6.setText("Credit Card Number");
jLabel4.setText("Course ID");
jLabel3.setText("Total Ammount");
txtPaycode.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
txtPaycode.setForeground(new java.awt.Color(240, 240, 240));
txtPaycode.setDisabledTextColor(new java.awt.Color(255, 0, 0));
txtPaycode.setEnabled(false);
jLabel2.setText("Stuent ID");
HND in Computing & Programming in
System Development Java
180
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select
One......", "Sampath
Bank", "Bank of ceylone", "NSB Bank", "Hattern National Bank", "Comercial Bank" }));
jComboBox1.setName("com"); // NOI18N
jLabel8.setText("Bank Name");
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Payment Method",
javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12))); //
NOI18N
rbCashe.setSelected(true);
rbCashe.setText("Cashe");
rbCashe.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
rbCasheActionPerformed(evt);
}
});
rdbCcard.setText("Credit Card");
javax.swing.GroupLayout jPanel1Layout = new
javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createSequentialGroup() .addContainerGap()
.addComponent(rdbCcard)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(rbCashe)
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.Grou
HND in Computing & Programming in
System Development Java
181
pLayout.Alignment.BASELINE)
.addComponent(rbCashe)
.addComponent(rdbCcard))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
);
jLabel5.setText("Payment Method");
jLabel7.setText("Payment Code");
btnAdd.setText("Save");
btnAdd.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
btnAddActionPerformed(evt);
}
});
btnClear.setText("CLEAR");
btnClear.setPreferredSize(new java.awt.Dimension(70,
33)); btnClear.addActionListener(new
java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) {
btnClearActionPerformed(evt);
}
});
btnOk.setText("Ok");
btnOk.setHorizontalTextPosition(javax.swing.SwingConstants.
CENTER); btnOk.setPreferredSize(new
java.awt.Dimension(70, 33));
btnOk.addActionListener(new java.awt.event.ActionListener()
{ public void
actionPerformed(java.awt.event.ActionEvent evt) {
btnOkActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel2Layout = new
javax.swing.GroupLayout(jPanel2);
HND in Computing & Programming in
System Development Java
182
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING
)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jLabel8)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 139,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(btnClear, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 82,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(btnOk, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING
)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILIN
G, false)
.addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE,
81, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5))
.addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING
)
.addComponent(txtcridetNO, javax.swing.GroupLayout.PREFERRED_SIZE,
139, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtPaycode, javax.swing.GroupLayout.PREFERRED_SIZE,
88, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
HND in Computing & Programming in
System Development Java
183
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING
) .addComponent(jLabel3)
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 66,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 66,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING
)
.addComponent(txtSiD, javax.swing.GroupLayout.PREFERRED_SIZE, 80,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtCiD, javax.swing.GroupLayout.PREFERRED_SIZE, 80,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtTot, javax.swing.GroupLayout.Alignment.TRAILING,
javax.swing.GroupLayout.PREFERRED_SIZE, 138,
javax.swing.GroupLayout.PREFERRED_SIZE)))) .addContainerGap(66,
Short.MAX_VALUE))
);
jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new
java.awt.Component[] {jLabel2, jLabel3, jLabel4, jLabel5, jLabel6, jLabel7, jLabel8});
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(31, 31, 31)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E)
.addComponent(txtPaycode, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel7))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING
)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
HND in Computing & Programming in
System Development Java
184
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E) .addComponent(txtcridetNO,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel6,
javax.swing.GroupLayout.PREFERRED_SIZE, 20,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(jLabel5))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E)
.addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 20,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 20,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtTot, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E) .addComponent(jLabel2)
.addComponent(txtSiD, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E) .addComponent(jLabel4)
.addComponent(txtCiD, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31,
Short.MAX_VALUE)
HND in Computing & Programming in
System Development Java
185
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E)
.addComponent(btnOk, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnClear, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 33,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(24, 24, 24))
);
jPanel1.getAccessibleContext().setAccessibleName("Payment method");
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout); layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI
NG)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(27, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup() .addContainerGap(24, Short.MAX_VALUE)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
pack();
}// </editor-fold>
public void generateID()throws ClassNotFoundException, SQLException{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://Anushka-
PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;";
Connection connection = DriverManager.getConnection(connectionUrl);
System.out.println("The database is Connected "); // connection ok
HND in Computing & Programming in
System Development Java
186
String sql="SELECT * FROM Payment_Details ORDER BY Payment_code ASC;";
Statement stmt2 = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet rs =
stmt2.executeQuery(sql); int id;
if(!rs.next()){
id=1;
txtPaycode.setText(Integer.toString(id));
}
else{
rs.last()
;
id=Integer.valueOf(rs.getString(1));
txtPaycode.setText(Integer.toString(id+1));
}
}// end of generateID
public void clearAll(){
txtPaycode.setText("");
txtTot.setText("");
txtSiD.setText("");
txtCiD.setText("");
}
public void add_payment() throws ClassNotFoundException, SQLException{
// connection to the sql server
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://Anushka-
PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;";
Connection connection = DriverManager.getConnection(connectionUrl);
System.out.println("Successfuly Connected to MS SQL 2005 using windows authentication
1"); // connection ok
Statement statement = connection.createStatement();
if(txtTot.getText().equals("")){
JOptionPane.showMessageDialog(null, "The Total Amount Field Is Empty,Enter Total
Amount", "EMPTY FIELD", JOptionPane.ERROR_MESSAGE);
}
HND in Computing & Programming in
System Development Java
187
else
{
if(rb
Cas
he.i
sSel
ecte
d()=
=tru
e){
statement.executeUpdate("INSERT INTO Payment_Details VALUES " +"( '" +
txtPaycode.getText() + "'" + "," +
"'" + this.getPayMethod() + "'" + ","+
"'" + txtTot.getText() + "'" + "," +
"'" + txtCiD.getText() + "'" + "," +
"'" + txtSiD.getText() + "'" + ")" +" ");
JOptionPane.showMessageDialog(null, "Record are saved in the database", "INSERT
SUCCESSFUL", JOptionPane.INFORMATION_MESSAGE);
}
else if(rdbCcard.isSelected()==true){
statement.executeUpdate("INSERT INTO Payment_Details VALUES " +"( '" +
txtPaycode.getText() + "'" + "," +
"'" + this.getPayMethod() + "'" + ","+
"'" + txtTot.getText() + "'" + "," +
"'" + txtCiD.getText() + "'" + "," +
"'" + txtSiD.getText() + "'" + ","+
"'" + txtcridetNO.getText() + "'" + ","+
"'" + jComboBox1.getToolTipText()+ "'" + " )" +" ");
}
else{
}
}
}
public boolean getPayMethod(){
if(rbCashe.isSelected()==true)
{ return false;
HND in Computing & Programming in
System Development Java
188
}
else if(rdbCcard.isSelected()==true){
return true;
}
else{
return false;
}
}
private void
btnAddActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here: try{
this.add_payment();//add new Payment
to database this.clearAll();
this.generateID();
}//end try
catch (ClassNotFoundException e){
System.out.println(e);
}
catch (SQLException e){
JOptionPane.showMessageDialog(this, e, "ERROR",
JOptionPane.ERROR_MESSAGE);
}
}
private void btnOkActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.setVisible(false);
}
private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.clearAll();
}
private void rbCasheActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
HND in Computing & Programming in
System Development Java
189
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting
code (optional) "> /* If Nimbus (introduced in Java SE 6) is not
available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) { if
("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Addpay.class.getName()).log(java.util.logging.Level.SEVE
RE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Addpay.class.getName()).log(java.util.logging.Level.SEVE
RE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Addpay.class.getName()).log(java.util.logging.Level.SEVE
RE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Addpay.class.getName()).log(java.util.logging.Level.SEVE
RE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new
Runnable() { public void run() {
new Addpay().setVisible(true);
HND in Computing & Programming in
System Development Java
190
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnAdd;
private javax.swing.JButton
btnClear; private
javax.swing.JButton btnOk; private
javax.swing.JComboBox jComboBox1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JRadioButton
rbCashe; private
javax.swing.JRadioButton rdbCcard;
private javax.swing.JPasswordField
txtCiD; private
javax.swing.JTextField txtPaycode;
private javax.swing.JPasswordField
txtSiD; private
javax.swing.JTextField txtTot;
private javax.swing.JTextField
txtcridetNO;
// End of variables declaration
}
HND in Computing & Programming in
System Development Java
191
4.2. Provide evidence and solutions for error handling during software
implementation.
In Software programming, a development error is one that can be prevented. Such an error can
occur in syntax or logic.
• Syntax errors, which are typographical mistakes or improper use of special characters, are
handled by rigorous proofreading.
• Logic errors, also called bugs, occur when executed code does not produce the expected
or desired result. Logic errors are best handled by meticulous program debugging.
This can be an ongoing process that involves, in addition to the traditional debugging routine,
beta testing prior to official release and customer feedback after official release.
There are several techniques that I used to handle above mentioned errors. Those are given
below.
Use “Try catch” method
Aa try statementa is used to catcha exceptionsa thata mighta be throwna as programa executesa.
Should use a try statement wherevera use a statement that might throw an exception that way,
program won‟t crash if the exception occurs. This is the code of this method. I used this method
in my all application forms.
try{
this.add_payment();//add new Payment to database
this.clearAll();
this.generateID();
//end try}
catch (ClassNotFoundException e){
System.out.println(e);
}
catch (SQLException e){
JOptionPane.showMessageDialog(this,e, "ERROR", JOptionPane.ERROR_MESSAGE);
}
HND in Computing & Programming in
System Development Java
192
Validate text fields
This is the second method that I used to handle errors. If any user will press a button without
filling the required data it will show the error message As the following picture.
Figure 46
Enable/ Disable buttons.
Figure 47
private voidbtnAddActionPerformed(java.awt.event.ActionEventevt) {
if(txtstudName.getText().equals("")){ JOptionPane.showMessageDialog(null,"The Student
name FieldIsEmpty,Enterthe Studentname Name", "EMPTY FIELD",
JOptionPane.ERROR_MESSAGE);
}
}elseif { ……..
HND in Computing & Programming in
System Development Java
193
This is the third method that I used to handle the errors. In here I disable some buttons when
window appear.as an example in student details interface the delete student detail button is
disabled because this User haven‟t permission to use it.
public void setUserLimitations(boolean prm){
btnDelete.setEnabled(prm);
}
HND in Computing & Programming in
System Development Java
194
Task 5
5.1. Carry out complete system testing and provide user guide. System testing and
user guide must include suitable screen shots. Testyour complete project with
suitable data.
First I‟m tried to login to the system by using default User Name and password ( user
Name=Admin password=admin)
Figure 48
Thenit continuedto themain Interface.
HND in Computing & Programming in
System Development Java
195
Then I‟m tried to create personal account to an user , to create new account first click on the
Configuration in the menu bar and expand it. After that click on the
Create new User
Figure 49
So it continued to an interface as the following picture.(I early day created some user accounts so
the User ID is Start With the number 4)
I filled the blanks that what they need to know about the new user in above form and I clicked
on Save button.
Blank
s
HND in Computing & Programming in
System Development Java
196
Figure 50
To return to the Main interface I click on the ok button in the Add user details form.
As the final of Adding new User I tried to login to the system by using the new Account
If filled data is correct, it will show a message as the following picture. then I clicked on
the ok button
HND in Computing & Programming in
System Development Java
197
(user Name = Rajapaksha, Password = 11 ).
Figure 51
Then it continued to the Main interface. to View the Course details Interface I clicked on the
Course details button ( I used an Administrator Account to use the all the
Figure 52
functions ).
HND in Computing & Programming in
System Development Java
198
As my need it continued to the Course details Interface
Figure 53
Figure 54
To Add a new Course detail i clicked on Add New Course details button then it
showed the Add new Course interface as the following picture.
Blanks
HND in Computing & Programming in
System Development Java
199
So I Filled the blanks and I clicked on save button
course interface then it return to the Course details interface….
Then it showed the following message after that I clicked on ok button.
Finally return to the Course details interface I clicked on ok button in the Add new
HND in Computing & Programming in
System Development Java
200
figure 55
To Edit a Course detail, i entered a course id on the text box and pressed enter
button.as the final I was selected that Course id from the table and clicked on
Edit Course details button .
Edit Course details button
Course details Table
Text Box
HND in Computing & Programming in
System Development Java
201
Then it showed the Edit Course interface as the following picture….
Figure 55
I done some modifications to the information.(as a modification I changed the course fee to
$200)
Figure 56
HND in Computing & Programming in
System Development Java
202
As the final I clicked on Update button .after that it showed the following message then I
clicked on yes button.
Figure 57
For return to the Course details Interface I clicked on Ok button in the Edit course details
interface.
Figure 58
HND in Computing & Programming in
System Development Java
203
Then it was returned to Course Details Interface.to return from this interface to the Main
interface I clicked on the ok button ….
Figure 59
Then it was returned to the Main interface. To view Batch details interface I clicked on
Figure 60
the Batch details Button.
HND in Computing & Programming in
System Development Java
204
Then it was continued to the batch details Interface.
Figure 61
So first I tried to add a batch detail.to view Add batch interface I was clicked on Add batch
button.
Then it was showed the Add batch Interface.
Figure 62
Blanks
HND in Computing & Programming in
System Development Java
205
So I was filled the blanks and I clicked on ok button
Figure 63
It was showed the following message. then i clicked on ok button in the message
Figure 64
HND in Computing & Programming in
System Development Java
206
For return to the Batch details I clicked on the ok button in the Add New Batch Interface.
Figure 65
Then it returned to the Batch details interface. To edit a batch detail I was Entered a batch id on
the text box and I pressed enter button then the detail was showed on the table finally I was
selected that record and clicked on the Edit batch detail button.
Figure 66
HND in Computing & Programming in
System Development Java
207
Then it was viewed the Edit Batch detail Form with the Batch information….
Figure 67
In the above interface I had done a modification to the information (as a modification I was
changed the Number of students up to 70 ) then I clicked on Update button.
Figure 68
HND in Computing & Programming in
System Development Java
208
Finally it was showed a message as below. Then I clicked on yes button.
Figure 69
Then the record was updated.
To return from the Edit batch details interface to the Batch details interface I was clicked on the
ok button.
Figure 70
HND in Computing & Programming in
System Development Java
209
Finally it returned to the Batch details Interface .to return to the Main interface I was clicked on
the Ok button under the Batch details interface…
Figure 71
To view the Student details I was clicked on the Student details button.
Figure 72
After that it was returned …
HND in Computing & Programming in
System Development Java
210
Then it continued to Student details Interface.
Figure 73
Figure 74
To Add a new student detail i clicked on Add New student details button then it
showed the Add new student interface as the following picture.
Blanks
HND in Computing & Programming in
System Development Java
211
So I Filled that Blanks and I clicked on Save button .
Figure 75
To return to the student details interface I clicked on ok button in the Add new student interface
.then it was returned…
To view edit student detail interface first I was entered a user ID on the text box and pressed
the enter button.as the second step I was selected that user id from the table and clicked on
Edit Student Details button.
To confirm it showed a message as below then i clicked on OK button.
HND in Computing & Programming in
System Development Java
212
Figure 76
Figure 77
Then it was viewed as below …
HND in Computing & Programming in
System Development Java
213
As a modification I was changed telephone number to 0774455754… then I clicked on Update
button
Figure 78
Finally it showed a message as below ,then I was clicked on Yes button.
Figure 79
HND in Computing & Programming in
System Development Java
214
To return to the Student details interface I was clicked on ok button in the Edit student details
interface.
Figure 80
Then it returned to the Student detail Interface.to return from this to the Min interface I was
clicked on ok button.
Figure 81
HND in Computing & Programming in
System Development Java
215
Then it was return to the Main interface.to View the payment detail interface I was clicked on
the payment details button in the Main interface..
Figure 82
Then it was continued to the Payment details form.
Figure 83
HND in Computing & Programming in
System Development Java
216
To receive a payment I was clicked on Receive payment button. Then It was showed the
following form
Figure 84
I was filled that form and I clicked on Save button then it was showed the following message
then I was clicked on the ok button .
Figure 85
According to customer requirements the above interfaces are I had implemented to the new
system.so one by one i was tested in this software and there is no any errors.
HND in Computing & Programming in
System Development Java
217
5.2. Producesuitable screenshots and solutions for error handling during testing.
When creating new user account
When creating a new account customer must enter the valid data to the system. If Customer
didn‟t enter the correct data there will some Error messages.
If User click Save button without fills the data, it will show a message as the following picture.
Figure 86
HND in Computing & Programming in
System Development Java
218
Figure 87
When Login to system
When log in to the system the user must enter their correct user name and password. Otherwise
there will be an error message as following picture. For this User should enter the correct user
name and password.
Figure 88
HND in Computing & Programming in
System Development Java
219
When Updating a Student detail…
Figure 89
When updating a student detail the user must confirm it. Else it wont to be update.
HND in Computing & Programming in
System Development Java
220
5.3. Include feedbacks on provided java solution (surveys, questionnaire, analyze
feedback and present results), evaluate all and provide summery report
Questionnaire for Administrator.
1) As an administrator what do you thinking about the new Software?
Excellent
Very good
Good
Poor
2) Is this system easy to Handel?
Yes
No
May be
No idea
3) Are you satisfied with the new System?
Satisfied
Fairly Satisfied
Dissatisfied
4) What accept were you mostly satisfied?
When entering a new student
When make a payment
When adding a course detail
When adding a batch detail
No idea
5) What you accept were you mostly dissatisfied?
When entering a new student
When make a payment
When adding a course detail
When deleting the student details
No idea
HND in Computing & Programming in
System Development Java
221
6) Do you have any comments ?
This software is easy to understand any one when it operate first time.
There is a little issue when back to previous interface by using close button. But in this
software it approaches to a new method to prevent the above problem by giving the close
command to ok button. That is good and deviation from other softwares.
When we‟re doing payments, this software gives us better help to increase accuracy of that
step.
This software is easy to handle and anyone can get an idea by operate this once. because of
that, this software is really successful and good solution to the given problem.
Questionnaire For Staff Members
2) As a Staff member what do you thinking about the new Software?
Excellent
Very good
Good
Poor
7) Is this system easy to Handel?
Yes
No
May be
No idea
8) Are you satisfied with the new System?
Satisfied
Fairly Satisfied
Dissatisfied
9) What accept were you mostly satisfied?
When entering a new student
When make a payment
When adding a course detail
When adding a batch detail
No idea
HND in Computing & Programming in
System Development Java
222
10)What you accept were you mostly dissatisfied?
When entering a new student
When make a payment
When adding a course detail
When adding a batch detail
When deleting students details
No idea
11)Do you have any comments ?
This software gives some benefits to staff members as well as administrators. Because staff
members also allowed to keep user accounts to their own. This benefit is useful to decrease
errors when operate the software by difference users.
Summary Report of the User feedback
How many customers are satisfied with about the system : 05
How many customers are dis-satisfied about the system : 03
How many customers said the system is difficult to handle : 02
How many customers said the system is easy to handle : 03
How many customers said „No idea‟ about the system : 03
*According to above feedback many Users are satisfied with the system*
Required Improvements
So according above answers to the above Questionnaires, the followings are the some
required improvements to the software.
• IF the User wants to delete a specific record about the student from the database
then it can be achieve without using many interfaces.
• The program won‟t to be fully terminate by Clicking on the close button.
• A profile picture can be added to the user profile.
HND in Computing & Programming in
System Development Java
223
Task 06
6.1. Producea publishable working copya compiled version of the completed
assignment together with software installation notes, recommendations and future
improvements. The installation note should include the system requirements
Software Installation
System requirements for the software installation
Hardware requirements
PIV or Higher Computer 1.6GHz or faster processor 40GB Hard disk Minimum 512 MB RAM
Free Disk Space 11 MB
Software Requirements
Any kind of an Operating system form this : Windows XP/ Windows 8 (Desktop) / Windows 7/
Windows Vista SP2/ Mac OS/ Oracle Linux 5.5+/ Ubuntu Linux* 10.04 and above. Java
Platform, Standard Edition Development Kit (Java SE 6 Update 27).
Installation Note
• Double click on the He institute Folder
Figure 90
• Then it will show an icon and a folder. click on the folder.
Figure 91
HND in Computing & Programming in
System Development Java
224
An user can see the following setup file then double click on it
Figure 92
• After that the installation will start as the following picture. Then click Next button to go
to the next step.
Figure 93
HND in Computing & Programming in
System Development Java
225
In here ,Click on next button…
Figure 94
HND in Computing & Programming in
System Development Java
226
Select the installation Path and click on next button
Figure 95
In here Click on Start button
Figure 96
HND in Computing & Programming in
System Development Java
227
• After that the software will be installing. When it complete can see the below window.
Then simply click Next button and then click finish.
Figure 97
HND in Computing & Programming in
System Development Java
228
Figure 98
• After completing those things the user must restore the Database to the system. then the user
must follow the following steps.
1. Double click on the He institute folder.
Figure 99
Finally click on the Exit button…
HND in Computing & Programming in
System Development Java
229
2. Then Copy the database backup folder to the system.
Figure 100
3. After that double click on the SQL server management studio Express…
Figure 101
HND in Computing & Programming in
System Development Java
230
4. Then it will continue to the following interface. Then click on the Connect button.
Figure 102
HND in Computing & Programming in
System Development Java
231
5. So it will continue ... then right click on the database tab and click on Restore database
as the following picture.
Figure 103
HND in Computing & Programming in
System Development Java
232
6. Then it will show an interface as below.. then type the name of a new database as
mention in the following picture and specify the source and location of backup sets to
restore.
Figure 104
Database
Name
Clickon
here
HND in Computing & Programming in
System Development Java
233
7. When click on as it mention in the above picture then it will show a interface as below.
Then click on Add button.
Figure 105
HND in Computing & Programming in
System Development Java
234
8. Then give the path as below. And click ok
Figure 106
HND in Computing & Programming in
System Development Java
235
9. After that you can see the following specify backup interface as below. Click on ok
button.
Figure 107
HND in Computing & Programming in
System Development Java
236
10.Finally it will return to the following Interface then Select the backup sets to restore as
below picture and click on ok button.
Figure 108
Figure 109
To start the software click on the icon as the following picture.
HND in Computing & Programming in
System Development Java
237
Reference Sheet
http://www.webopedia.com/TERM/J/Java.html
https://www.bsi.bund.de/EN/Topics/OtherTopics/Java/javaprinciples.html
http://www.cs.armstrong.edu/liang/JavaCharacteristics.pdf http://www.makeuseof.com/tag/java-
virtual-machine-work-makeuseof-explains/ http://searchcrm.techtarget.com/definition/entity-
relationship-diagram http://msdn.microsoft.com/en-us/library/vstudio/dd409437.aspx
http://www.visual-paradigm.com/VPGallery/diagrams/UseCase.html
http://www.techterms.com/definition/user_interface

More Related Content

What's hot (14)

PDF
FASB Learning Guide
alanshaffer2000
 
PDF
sun-java-style
AbrarMoiz
 
PDF
Java interview questions
rithustutorials
 
PDF
U M Lvs I D E F
Ngo Hung Long
 
PDF
Java tutorial
IIPM JAIPUR
 
PDF
Course lab 2_guide_eng
tameranwaralzohairy
 
PDF
Language in leadership and management: ACE School Management and Leadership (...
Saide OER Africa
 
PDF
Java script tools guide cs6
Sadiq Momin
 
PDF
Java tutorial
Yin Miroslawa
 
PDF
Flash as3 programming
allen cervantes
 
PDF
In designcs5 scripting tutorial
Mustfeez Rasul
 
PDF
Visualforce Workbook
SLMaster
 
PDF
Google Search Quality Rating Program General Guidelines 2011
Interrocks, Koppelkracht, ELF Voetbal, Voetbalprimeur.nl en Blue Linking
 
PDF
Google guidelines
SEOM
 
FASB Learning Guide
alanshaffer2000
 
sun-java-style
AbrarMoiz
 
Java interview questions
rithustutorials
 
U M Lvs I D E F
Ngo Hung Long
 
Java tutorial
IIPM JAIPUR
 
Course lab 2_guide_eng
tameranwaralzohairy
 
Language in leadership and management: ACE School Management and Leadership (...
Saide OER Africa
 
Java script tools guide cs6
Sadiq Momin
 
Java tutorial
Yin Miroslawa
 
Flash as3 programming
allen cervantes
 
In designcs5 scripting tutorial
Mustfeez Rasul
 
Visualforce Workbook
SLMaster
 
Google Search Quality Rating Program General Guidelines 2011
Interrocks, Koppelkracht, ELF Voetbal, Voetbalprimeur.nl en Blue Linking
 
Google guidelines
SEOM
 

Similar to Java completed assignment (20)

DOCX
HND Assignment Brief Session Sept.docx
joyjonna282
 
PDF
SE-IT JAVA LAB SYLLABUS
nikshaikh786
 
PDF
DAA-LAB-MANFSDSDFSDFSDSDSFDSDAA LAB MANUAL FOR BTECH STUDENTSUAL2020-1.pdf
karunakar43
 
PDF
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...
Nithin Kumar,VVCE, Mysuru
 
DOC
Proposal e learning
sablahhh
 
PPTX
University online course registration system
Md. Zahid Hasan
 
DOCX
Final srs of academic a webpage based android app
preeta sinha
 
DOCX
COLLEGE PHONE BOOK Final documentation
Bhadra Gowdra
 
PDF
Attendance Management System
Arhind Gautam
 
PDF
java programming slideshow which helps you in many ways
ArpitDalal8
 
PDF
Daa final 2019 20
Snk Nks
 
PPT
Budget
Samhitha Samhy
 
DOC
College information management system.doc
Kamal Acharya
 
PDF
Sr Full Stack Developer
Muhammad Suhaib
 
DOC
Strayer cis 406 week 10 assignment 2 u grade new
shyaminfo16
 
DOC
Strayer cis 406 week 10 assignment 2 u grade new
dixonbakerr
 
DOC
Strayer cis 406 week 10 assignment 2 u grade new
charlesangles123
 
DOC
Strayer cis 406 week 10 assignment 2 u grade new
shyaminfo40
 
DOC
Strayer cis 406 week 10 assignment 2 u grade new
chanduruc123
 
DOC
Vivek\'s Resume
Vivek Pandey
 
HND Assignment Brief Session Sept.docx
joyjonna282
 
SE-IT JAVA LAB SYLLABUS
nikshaikh786
 
DAA-LAB-MANFSDSDFSDFSDSDSFDSDAA LAB MANUAL FOR BTECH STUDENTSUAL2020-1.pdf
karunakar43
 
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...
Nithin Kumar,VVCE, Mysuru
 
Proposal e learning
sablahhh
 
University online course registration system
Md. Zahid Hasan
 
Final srs of academic a webpage based android app
preeta sinha
 
COLLEGE PHONE BOOK Final documentation
Bhadra Gowdra
 
Attendance Management System
Arhind Gautam
 
java programming slideshow which helps you in many ways
ArpitDalal8
 
Daa final 2019 20
Snk Nks
 
College information management system.doc
Kamal Acharya
 
Sr Full Stack Developer
Muhammad Suhaib
 
Strayer cis 406 week 10 assignment 2 u grade new
shyaminfo16
 
Strayer cis 406 week 10 assignment 2 u grade new
dixonbakerr
 
Strayer cis 406 week 10 assignment 2 u grade new
charlesangles123
 
Strayer cis 406 week 10 assignment 2 u grade new
shyaminfo40
 
Strayer cis 406 week 10 assignment 2 u grade new
chanduruc123
 
Vivek\'s Resume
Vivek Pandey
 
Ad

Recently uploaded (20)

PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PDF
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
PDF
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
PPTX
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
PDF
Executive Business Intelligence Dashboards
vandeslie24
 
PPTX
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PPTX
The Role of a PHP Development Company in Modern Web Development
SEO Company for School in Delhi NCR
 
PDF
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PPTX
How Apagen Empowered an EPC Company with Engineering ERP Software
SatishKumar2651
 
PPTX
An Introduction to ZAP by Checkmarx - Official Version
Simon Bennetts
 
PPTX
Engineering the Java Web Application (MVC)
abhishekoza1981
 
PDF
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
PPTX
A Complete Guide to Salesforce SMS Integrations Build Scalable Messaging With...
360 SMS APP
 
PPTX
Revolutionizing Code Modernization with AI
KrzysztofKkol1
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
PDF
Capcut Pro Crack For PC Latest Version {Fully Unlocked} 2025
hashhshs786
 
PPTX
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
Executive Business Intelligence Dashboards
vandeslie24
 
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
The Role of a PHP Development Company in Modern Web Development
SEO Company for School in Delhi NCR
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
How Apagen Empowered an EPC Company with Engineering ERP Software
SatishKumar2651
 
An Introduction to ZAP by Checkmarx - Official Version
Simon Bennetts
 
Engineering the Java Web Application (MVC)
abhishekoza1981
 
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
A Complete Guide to Salesforce SMS Integrations Build Scalable Messaging With...
360 SMS APP
 
Revolutionizing Code Modernization with AI
KrzysztofKkol1
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
Capcut Pro Crack For PC Latest Version {Fully Unlocked} 2025
hashhshs786
 
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
 
Ad

Java completed assignment

  • 1. HND in Computing & Programming in System Development Java 1 Contents Task1.....................................................................................................................................................................................................3 1.1. Briefly explain the principles, characteristics features and in Java programming. ....................................................................3 1.2 Explain environmental flexibility of programming in Java programming and evaluate your answer........................................5 Task 02..................................................................................................................................................................................................6 2.1. Design a database with necessary tables....................................................................................................................................6 Normalization....................................................................................................................................................................................6 2.2 Set up the database structure by using suitable RDBMS............................................................................................................9 2.2 b) Populatethe database with suitable test data........................................................................................................................13 Task 03................................................................................................................................................................................................16 3.1. The designs of all the required algorithms are required as part of this task. You can use suitable design techniques ............16 Activity diagram for student registration in Propose System......................................................................................................17 Activity Diagram For record the Payment details in Propose System........................................................................................18 Flow chart for check the validity of users to usethe data in the propose program.....................................................................19 Use case diagram for proposed system .......................................................................................................................................20 3.2. Design necessary user interfaces Login Form..........................................................................................................................21 Main Form..................................................................................................................................................................................21 Student Details form....................................................................................................................................................................22 Add New Student Details Form ..................................................................................................................................................22 Edit Student Details Form...........................................................................................................................................................23 Course Details Form....................................................................................................................................................................23 Add New course Details form.....................................................................................................................................................24 Edit course details Form..............................................................................................................................................................24 Batch Details Form......................................................................................................................................................................25 Add New Batch Details Form.....................................................................................................................................................25 Edit Batch Details........................................................................................................................................................................26 Create New User Account Form.................................................................................................................................................26 Edit User Account .......................................................................................................................................................................27 Payment Details Form.................................................................................................................................................................27 Receive Payments Form..............................................................................................................................................................28 Task 04................................................................................................................................................................................................29 4.1. Using java programming language, implement the Student information system for the HE Institute designed in Task 03. ..29 Login Form......................................................................................................................................................................................29 Main Form.......................................................................................................................................................................................36 Edit Student Details Form ...........................................................................................................................................................69 Course Details Form........................................................................................................................................................................79 Add Course detail Form ..................................................................................................................................................................91 Edit Course Details Form.............................................................................................................................................................102 4.2. Provide evidence and solutions for error handling during software implementation. ..........................................................191
  • 2. HND in Computing & Programming in System Development Java 2 Use “Try catch” method...........................................................................................................................................................191 Validate text fields....................................................................................................................................................................192 Task 5...............................................................................................................................................................................................194 5.1. Carry out complete systemtestingand provide user guide. Systemtesting and user guide must include suitable screen shots. Test your complete project with suitable data. ............................................................................................................................194 Name=Admin password=admin) .................................................................................................................................................194 5.2. Produce suitable screenshots and solutions for error handling during testing. .....................................................................217 When creating new user account..............................................................................................................................................217 When Login to system..................................................................................................................................................................218 When Updating a Student detail…...............................................................................................................................................219 5.3. Include feedbacks on provided java solution (surveys, questionnaire, analyzefeedback and present results), evaluate all and provide summery report...............................................................................................................................................................220 Required Improvements ...............................................................................................................................................................222 Task 06.............................................................................................................................................................................................223 6.1. Produce a publishable working copy a compiled version of thecompleted assignment together with softwareinstallation notes, recommendations and future improvements. The installation note should include the system requirements ..................223 Hardware requirements ................................................................................................................................................................223 Software Requirements ................................................................................................................................................................223 Installation Note.......................................................................................................................................................................223 Select the installation Path and click on next button...............................................................................................................226
  • 3. HND in Computing & Programming in System Development Java 3 Task1 1.1. Briefly explain the principles, characteristics features and in Java programming. Java is an object oriented and architecture-neutral programming language. This agrees programs created in Java to be ported into different platform without the need for modifications. As a beginner to the JAVA programming language a person must want to get the knowledge about what are the principals in java programming language, what are the characteristics in JAVA programming language and what are the features in Java programming language. Basically we can identify there are five principals in java programming language. • Simple, object oriented and familiar language – it‟s like greatly in its consistence. Once you learn had to do one thing in Java, you know how to do same way throughout the language. Because it never deviates from the way the languages Architecture. It‟s an object oriented language.so once you understand the principals of encapsulation, inheritance and polymorphism and how those are implemented in the java programming language you have a much better sense of how to Architect your Applications. • Robust and secure – its robustness likes greatly in its object oriented characteristics. Because you designing everything as an object. Everything has methods or functions and properties. Also known as fields. And you creating an application by combining multiple classes together. This let you create your code in small trunks. And it makes easy to maintain and debug your applications overtime. • Architecture- neutral and portable – java was designed to be portable.so that it able to be compiled once. • High- performance – Java was created to be high performance the Original version of the Java virtual machine wasn‟t faster c++ applications. But over the years it‟s been improved normally. And today justness fast sometimes even faster. Then the Applications built in c++. • Interpreted, threaded and dynamic In above paragraph briefly discussed about the Principals in java programming language .after getting knowledge about principals a beginner must have to get the knowledge about what are the characteristics and features in Java programming language. Because it is helping to understand why we are saying java is an object oriented programing language and some other sort of things.
  • 4. HND in Computing & Programming in System Development Java 4 Characteristics and features of java programming • Java Is Simple – Java is partially modeled on c++ but greatly simplified and improved some people refer to java as “C++”because it is like C++but with more functionality and fewer negative aspects. • Java Is object- oriented – Java is a fully object oriented language. It supports all the characteristics needed to be object oriented. In the Java everything is treated as objects to which methods are applied. As the languages like Objective C, C++ fulfills the above four characteristics yet they are not fully object oriented languages because they are structured as well as object oriented languages. But in case of java, it is a fully Object Oriented language because object is at the outer most level of data structure in java. No standalone methods, constants, and variables are there in java. Everything in java is object even the primitive data types can also be converted into object by using the wrapper class. Once of the issue in software development is how to reprocess code. Object oriented programming provides great Flexibility, modularity, clarity and reusability trough polymorphism, inheritance and encapsulation. • Java is distributed – Distributed computing involves several computers working together on a network.java is formed to make distributed computing case. Since networking capability is inherently integrated into java, writing network programs is like delivering and receiving data to and from a file. • Java is Platform Independent - Java provides the facility to "Write once -Run anywhere"(Known as platform independent). Not even a single language is idle to this feature but java is closer to this feature. Java Provide the facility of cross platform programs by compiling in intermediate code known as byte code. This byte code can be interpreted on any system which has Java Virtual Machine (JVM). • Java is interpreted – you need an interpreter to run java programs. The programs are compiled to the java virtual machine known as byte code. The byte code is machine independent and can run on any machine that has a java interpreter, which is piece of the java virtual machine. • Java is Robust – java has a runtime exception-handling feature o provide programming support for robustness. • Java is secure – its mean is java implements several security mechanisms to safeguard your system against harm caused stray programs. • Java is architecture –neutral – it mean is you can write one program that will run on any platform • Java is portable –because java is architecture neutral, java programs are portable. They can be run on any platform without being recompiled. Those are the main features, Characteristics and principal in java programming language.
  • 5. HND in Computing & Programming in System Development Java 5 1.2 Explain environmental flexibility of programming in Java programming and evaluate your answer. Java was designed to allow application programs to be built that could be run on any platform without having to be rewritten or recompiled by the programmer for each separate platform. Java virtual machine (JVM), an implementation of the Java Virtual Machine Specification, interprets compiled Java byte code for a computer's processor or hardware platform so that it can execute a Java program's commands. The Java Virtual Machine Specification defines an abstract rather than a real machine or processor. The Specification specifies an instruction set, a set of registers, a stack, a "garbage heap," and a method area. A Java virtual machine makes this possible because it is aware of the specific instruction lengths and other particularities of the platform. Once a Java virtual machine has been implemented for a given platform, any Java program which, after compilation, the byte code can run on that platform. A Java virtual machine can either interpret the byte code one instruction at a time or the byte code can be compiled further for the real processor using what is called a just-in-time compiler. The one of main advantage of the Java VM ,it is allow to a program to be written and compiled only once, which then can be run on a wide variety of systems and operating systems without modification. Many cell phones and embedded devices include a Java VM. Another Since programming must be translated from generic "byte code" to the machine code for the target system as it is being run, it is impossible for Java to perform as quickly as languages that can compile directly to machine code for the target systems. Because the Java VM must run on a wide variety of systems, features specific to one OS are often not implemented into Java programs. In addition, the "look and feel" of Java applications can often be quite different than the default styles of native applications within an operating system.
  • 6. HND in Computing & Programming in System Development Java 6 Task 02 2.1. Design a database with necessary tables. Normalization UNF HE Institute{ Student ID, Name, Age, Gender, Telephone no, Course ID, Course Name, Course Fee, Duration, Batch ID, Start date, number Of Student, End date, Payment code, Payment type, Amount, Staff ID, Staff Name, User Name, Password , Account Type} 1NF Student{ Student ID, Name, Age, Gender, Telephone no } HE Institute{ Course ID, Course Name, Course Fee, Duration, Batch ID, Start date, number Of Student, End date, Payment code, Payment type, Amount, Staff ID, Staff Name, User Name, Password , Account Type, Student ID} 2NF Student{ Student ID, Name, Age, Gender, Telephone no } Course { Course ID, Course Name, Course Fee, Duration, Student ID } HE Institute{ Batch ID, Start date, number Of Student, End date, Payment code, Payment type, Amount, Staff ID, Staff Name, User Name, Password , Account Type, Student ID} 3NF Student{ Student ID, Name, Age, Gender, Telephone no } Course { Course ID, Course Name, Course Fee, Duration, Student ID } Batch{ Batch ID, Start date, number Of Student, End date, Student ID, Course ID } Payment{ Payment code, Payment type, Amount, Student ID , Course ID } Staff{ Staff ID, Staff Name, User Name, Password , Account Type, Student ID } HE Institute{ Student ID , Course ID , Batch ID , Payment code, Staff ID} BCNF Student{ Student ID, Name, Age, Gender, Telephone no } Course { Course ID, Course Name, Course Fee, Duration, Student ID } Batch{ Batch ID, Start date, number Of Student, End date, Student ID, Course ID } Payment{ Payment code, Payment type, Amount, Student ID , Course ID } Staff{ Staff ID, Staff Name, User Name, Password , Account Type, Student ID }
  • 7. HND in Computing & Programming in System Development Java 7 Student_Course{ Student ID , Course ID } Student_Batch{ Student ID , Batch ID } Student_Staff{{ Student ID , Staff ID }
  • 8. HND in Computing & Programming in System Development Java 8
  • 9. HND in Computing & Programming in System Development Java 9 2.2 Set up the database structure by using suitable RDBMS.
  • 10. HND in Computing & Programming in System Development Java 10 Table –Staff Figure 1 Table_student Figure 2 Table- student_Staff Figure 3 -
  • 11. HND in Computing & Programming in System Development Java 11 Table-Batch Figure 4 Table-Student_Batch Figure 5 Table-Course Figure 6
  • 12. HND in Computing & Programming in System Development Java 12 Table – Student_Course Figure 7 Table- Payment Figure 8
  • 13. HND in Computing & Programming in System Development Java 13 2.2 b) Populate the database with suitable test data. Table-Staff Figure 9 Table -Student Figure 10
  • 14. HND in Computing & Programming in System Development Java 14 Table- Student_Staff Figure 11 Table-Batch Figure 12 Table-Student_Batch Figure 13
  • 15. HND in Computing & Programming in System Development Java 15 Table-Course Figure 14 Table – Student_Course Figure 15 Table- Payment Figure 16
  • 16. HND in Computing & Programming in System Development Java 16 Task 03 3.1. The designs of all the required algorithms are required as part of this task. You can use suitable design techniques Class diagram for proposed system = Student Student_ID Student_Name Age Telephone_no Payments Payment_code Payment_Method Total Amount Course Course_ID Course_Name Duration Course_Fee Staff Staff ID Staff Name User Name Password Account type * 1 * 1 1 Batch Batch_ID Start_date End Date number_Of_Student Course_ID * * * 1* * *
  • 17. HND in Computing & Programming in System Development Java 17 Activity diagram for student registration in Propose System Input: Course Details Student Details Batch Details Select a Student If student Want to Follow a new Course Select a Course If student want to follow the selected course student Assign to a Batch True False True False
  • 18. HND in Computing & Programming in System Development Java 18 Activity Diagram For record the Payment details in Propose System Input: Course Details Student Details Payment Details Select a student and the course If student want to pay a premium of the course Select a Payment Method Receive the payments Record the payment detail True False
  • 19. HND in Computing & Programming in System Development Java 19 Flow chart for check the validity of users to use the data in the propose program Start Input: User type (Admin or Staff member) password, user name Enter the User Name and Password Check the person is an Admin or a staff member by comparing user Name and password If the person is an Admin Check the person is a staff member Give the full permission to use the data in the program If the person is a staff Stop Give the limited permission to use the data in the program True False True False Provide an error Message
  • 20. HND in Computing & Programming in System Development Java 20 Use case diagram for proposed system Add Studentdetails EditStudentdetails Delete Student Details Add Course Details EditCourse details Face to the Assignmentsand Exams Admin Students Attend to the classes Delete Course Details Add Batch details Select courses Do payments Edit Batch details Add Payment Details Staff Edit Payment Details Delete Payment Details Provide student Informations
  • 21. HND in Computing & Programming in System Development Java 21 3.2. Design necessary user interfaces Login Form Figure 17 Main Form Figure 18
  • 22. HND in Computing & Programming in System Development Java 22 Student Details form Figure 19 Add New Student Details Form Figure 20
  • 23. HND in Computing & Programming in System Development Java 23 Edit Student Details Form Figure 21 Course Details Form Figure 22
  • 24. HND in Computing & Programming in System Development Java 24 Add New course Details form Figure 23 Edit course details Form Figure 24
  • 25. HND in Computing & Programming in System Development Java 25 Batch Details Form Figure 25 Add New Batch Details Form Figure 26
  • 26. HND in Computing & Programming in System Development Java 26 Edit Batch Details Figure 27 Create New User Account Form Figure 28
  • 27. HND in Computing & Programming in System Development Java 27 Edit User Account Figure 29 Payment Details Form Figure 30
  • 28. HND in Computing & Programming in System Development Java 28 Receive Payments Form Figure 31
  • 29. HND in Computing & Programming in System Development Java 29 Task 04 4.1. Using java programming language, implement the Student information system for the HE Institute designed in Task 03. Login Form Figure 32 Program code: /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package heinstitute; import javax.swing.JOptionPane; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.sql.ResultSet; import javax.swing.UIManager; /** *
  • 30. HND in Computing & Programming in System Development Java 30 * @author Anushka */ public class Login extends javax.swing.JFrame { /** * Creates new form Login */ private String staffname; private boolean privileges; private int staffid; public Login() { initComponents(); } public boolean checkLogin(String user,String pass) throws ClassNotFoundException, SQLException { // connection to the sql server Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String connectionUrl = "jdbc:sqlserver://Anushka- PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;"; Connection connection = DriverManager.getConnection(connectionUrl); System.out.println("Successfuly Connected "); // connection ok // retriving user and password gave by user String SQL = "SELECT * FROM Staff WHERE User_name='"+user+"'AND Password='"+pass+"'"; Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet rs = stmt.executeQuery(SQL); return rs.first(); //returns true if ResultSet has a row valid login } // end con method public void getStaffName_privileges(String user,String pass) throws ClassNotFoundException, SQLException { // connection to the sql server Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String connectionUrl = "jdbc:sqlserver://Anushka-
  • 31. HND in Computing & Programming in System Development Java 31 PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;"; Connection connection = DriverManager.getConnection(connectionUrl); System.out.println("Successfuly Connected "); // connection ok // Get staff name String SQL = "SELECT * FROM Staff WHERE User_name='"+user+"'AND Password='"+pass+"'"; Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet rs = stmt.executeQuery(SQL); rs.first(); staffname= rs.getString(2); privileges=rs.getBoolean(5); staffid=rs.getInt(1); } // end con method public void displayMain(){ //display main interface Main main = new Main(staffname,privileges,staffid); main.setVisible(true); main.setLocationRelativeTo(null); main.setResizable(false); }// end displayMain public void loginToSystem()throws ClassNotFoundException, SQLException { String username=txtUser.getText(); String password=txtPass.getText(); if (this.checkLogin(username,password)==true){ this.getStaffName_privileges(username,password); this.displayMain(); this.setVisible(false); }//end if else if(this.checkLogin(username,password)==false){
  • 32. HND in Computing & Programming in System Development Java 32 JOptionPane.showMessageDialog(null, " None Authorized Access please enter a valid User-Name and a Password","Error",JOptionPane.ERROR_MESSAGE); }//end else }// end of login to system /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { txtPass = new javax.swing.JPasswordField(); jLabel2 = new javax.swing.JLabel(); jLabel1 = new javax.swing.JLabel(); txtUser = new javax.swing.JTextField(); btnlogin = new javax.swing.JButton(); btnExit = new javax.swing.JButton(); jLabel3 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); txtPass.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { txtPassActionPerformed(evt); } }); jLabel2.setText("Pasword :"); jLabel1.setText("User Name :"); btnlogin.setIcon(new javax.swing.ImageIcon(getClass().getResource("/heinstitute/login.png"))); // NOI18N btnlogin.setText("LOGIN"); btnlogin.addActionListener(new
  • 33. HND in Computing & Programming in System Development Java 33 java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnloginActionPerformed(evt); } }); btnlogin.addKeyListener(new java.awt.event.KeyAdapter() { public void keyPressed(java.awt.event.KeyEvent evt) { btnloginKeyPressed(evt); } }); btnExit.setText("Exit"); btnExit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnExitActionPerformed(evt); } }); jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/heinstitute/1397005153_Refresh_key.png"))); // NOI18N jLabel3.setText("jLabel3"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel2) .addComponent(jLabel1)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(txtPass, javax.swing.GroupLayout.DEFAULT_SIZE, 147, Short.MAX_VALUE) .addComponent(txtUser))) .addComponent(btnlogin, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18)
  • 34. HND in Computing & Programming in System Development Java 34 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnExit, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(18, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(23, 23, 23) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1) .addComponent(txtUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(12, 12, 12) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2) .addComponent(txtPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(27, 27, 27)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED))) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnExit, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnlogin, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(19, Short.MAX_VALUE)) ); pack(); }// </editor-fold> private void txtPassActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } private void btnloginActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: try{ this.loginToSystem();
  • 35. HND in Computing & Programming in System Development Java 35 }//end try catch (ClassNotFoundException e){ System.out.println(e); } catch (SQLException e){ System.out.println(e); } } private void btnloginKeyPressed(java.awt.event.KeyEvent evt) { // TODO add your handling code here: } private void btnExitActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: int result =JOptionPane.showConfirmDialog(null, "Do you Want To Exit ?", "Exit", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_ME SSAGE); if(result==0){ System.exit(0); } else if(result==1){ } } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Login().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton btnExit; private javax.swing.JButton btnlogin; private javax.swing.JLabel jLabel1;
  • 36. HND in Computing & Programming in System Development Java 36 private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JPasswordField txtPass; private javax.swing.JTextField txtUser; // End of variables declaration } Main Form Figure 33 Program Code : /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package heinstitute; import java.awt.Frame; import java.sql.Connection; import java.sql.DriverManager;
  • 37. HND in Computing & Programming in System Development Java 37 import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.UIManager; /** * * @author Anushka */ public class Main extends javax.swing.JFrame { /** * Creates new form Main */ private boolean privileges; private int staffid; private String S_name; public Main(String staffname,boolean p,int id) { initComponents(); lblStaffName.setText(staffname.toUpperCase()); this.setUserLimitations(p); staffid=id; // set Staff id S_name=staffname; } public void setUserLimitations(boolean prm){ privileges=prm; menuAddUsers.setEnabled(prm); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code.
  • 38. HND in Computing & Programming in System Development Java 38 The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { btnCourse = new javax.swing.JButton(); lblStaffName2 = new javax.swing.JLabel(); btnStudent = new javax.swing.JButton(); lblStaffName3 = new javax.swing.JLabel(); btnCourse1 = new javax.swing.JButton(); lblStaffName4 = new javax.swing.JLabel(); lblStaffName1 = new javax.swing.JLabel(); lblStaffName = new javax.swing.JLabel(); btnCourse2 = new javax.swing.JButton(); lblStaffName5 = new javax.swing.JLabel(); jMenuBar1 = new javax.swing.JMenuBar(); jMenu3 = new javax.swing.JMenu(); menuAddUsers = new javax.swing.JMenuItem(); menuEditMyAcc = new javax.swing.JMenuItem(); menulogout = new javax.swing.JMenu(); jMenu2 = new javax.swing.JMenu(); jMenuItem1 = new javax.swing.JMenuItem(); jMenuItem2 = new javax.swing.JMenuItem(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); btnCourse.setBackground(new java.awt.Color(0, 0, 0)); btnCourse.setIcon(new javax.swing.ImageIcon(getClass().getResource("/heinstitute/1397062581_Courses.png"))); // NOI18N btnCourse.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnCourseActionPerformed(evt); } });
  • 39. HND in Computing & Programming in System Development Java 39 lblStaffName2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N lblStaffName2.setForeground(new java.awt.Color(255, 255, 255)); lblStaffName2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); lblStaffName2.setText("Course Details"); btnStudent.setBackground(new java.awt.Color(0, 0, 0)); btnStudent.setIcon(new javax.swing.ImageIcon(getClass().getResource("/heinstitute/1397061362_elementary_scho ol.png"))); // NOI18N btnStudent.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnStudentActionPerformed(evt); } }); lblStaffName3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N lblStaffName3.setForeground(new java.awt.Color(255, 255, 255)); lblStaffName3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); lblStaffName3.setText("Student Details"); btnCourse1.setBackground(new java.awt.Color(0, 0, 0)); btnCourse1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/heinstitute/1397064740_Money.png"))); // NOI18N lblStaffName4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N lblStaffName4.setForeground(new java.awt.Color(255, 255, 255)); lblStaffName4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); lblStaffName4.setText("Batch Details"); lblStaffName1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N lblStaffName1.setForeground(new java.awt.Color(255, 102, 0)); lblStaffName1.setText("User:"); lblStaffName.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N lblStaffName.setForeground(new java.awt.Color(255, 102, 0)); lblStaffName.setText("jLabel8"); btnCourse2.setBackground(new java.awt.Color(0, 0, 0)); btnCourse2.setIcon
  • 40. HND in Computing & Programming in System Development Java 40 (new javax.swing.ImageIcon(getClass().getResource("/heinstitute/1397132431_MySpace.png"))); // NOI18N btnCourse2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnCourse2ActionPerformed(evt); } }); lblStaffName5.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N lblStaffName5.setForeground(new java.awt.Color(255, 255, 255)); lblStaffName5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); lblStaffName5.setText("Payment Details"); jMenu3.setText("Configuration"); menuAddUsers.setText("Create New User"); menuAddUsers.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { menuAddUsersActionPerformed(evt); } }); jMenu3.add(menuAddUsers); menuEditMyAcc.setText("Edit User Account"); menuEditMyAcc.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { menuEditMyAccActionPerformed(evt); } }); jMenu3.add(menuEditMyAcc); jMenuBar1.add(jMenu3); menulogout.setText("Log Out"); menulogout.addMouseListener(new java.awt.event.MouseAdapter() {
  • 41. HND in Computing & Programming in System Development Java 41 public void mouseClicked(java.awt.event.MouseEvent evt) { menulogoutMouseClicked(evt); } }); menulogout.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { menulogoutActionPerformed(evt); } }); jMenuBar1.add(menulogout); jMenu2.setText("Help"); jMenuItem1.setText("Help "); jMenuItem1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItem1ActionPerformed(evt); } }); jMenu2.add(jMenuItem1); jMenuItem2.setText("About"); jMenuItem2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItem2ActionPerformed(evt); } }); jMenu2.add(jMenuItem2); jMenuBar1.add(jMenu2); setJMenuBar(jMenuBar1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap()
  • 42. HND in Computing & Programming in System Development Java 42 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(10, 10, 10) .addComponent(lblStaffName1) .addGap(3, 3, 3) .addComponent(lblStaffName, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(lblStaffName3, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnStudent, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(btnCourse, javax.swing.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE) .addComponent(lblStaffName2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(btnCourse1, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(btnCourse2, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(lblStaffName5, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(lblStaffName4, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(8, 8, 8)))))
  • 43. HND in Computing & Programming in System Development Java 43 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnCourse, btnCourse1, btnStudent, lblStaffName4}); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(btnStudent, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(btnCourse, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnCourse1, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnCourse2, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(lblStaffName3) .addComponent(lblStaffName2) .addComponent(lblStaffName5)) .addComponent(lblStaffName4)))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(lblStaffName) .addComponent(lblStaffName1)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) );
  • 44. HND in Computing & Programming in System Development Java 44 layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btnCourse, btnStudent}); pack(); }// </editor-fold> private void menuAddUsersActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: AddUser adduser = new AddUser(); adduser.setLocationRelativeTo(null); adduser.setResizable(false); adduser.setVisible(true); } private void menuEditMyAccActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: EditMyUserAcc editmyacc = new EditMyUserAcc(staffid); editmyacc.setLocationRelativeTo(null); editmyacc.setResizable(false); editmyacc.setVisible(true); } private void menulogoutMouseClicked(java.awt.event.MouseEvent evt) { this.setVisible(false); Login log = new Login(); log.setVisible(true); log.setLocationRelativeTo(null); } private void menulogoutActionPerformed(java.awt.event.ActionEvent evt) { } private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: }
  • 45. HND in Computing & Programming in System Development Java 45 private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: About about = new About(); about.setVisible(true); about.setLocationRelativeTo(null); } private void btnStudentActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: StudentDetails sudentdetails= new StudentDetails(privileges); sudentdetails.setVisible(true); sudentdetails.setLocationRelativeTo(null); sudentdetails.setResizable(false); } private void btnCourseActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: CourseDetails csdetails= new CourseDetails(privileges); csdetails.setVisible(true); csdetails.setLocationRelativeTo(null); csdetails.setResizable(false); } private void btnCourse2ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: BatchDetails batch= new BatchDetails(privileges); batch.setVisible(true); batch.setLocationRelativeTo(null); batch.setResizable(false); } /** * @param args the command line arguments */ public static void main(String args[])throws ClassNotFoundException, SQLException {
  • 46. HND in Computing & Programming in System Development Java 46 try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch (Exception er) { System.out.println(er.toString()); } /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { //Main m =new Main(); } }); } // Variables declaration - do not modify private javax.swing.JButton btnCourse; private javax.swing.JButton btnCourse1; private javax.swing.JButton btnCourse2; private javax.swing.JButton btnStudent; private javax.swing.JMenu jMenu2; private javax.swing.JMenu jMenu3; private javax.swing.JMenuBar jMenuBar1; private javax.swing.JMenuItem jMenuItem1; private javax.swing.JMenuItem jMenuItem2; private javax.swing.JLabel lblStaffName; private javax.swing.JLabel lblStaffName1; private javax.swing.JLabel lblStaffName2; private javax.swing.JLabel lblStaffName3; private javax.swing.JLabel lblStaffName4; private javax.swing.JLabel lblStaffName5; private javax.swing.JMenuItem menuAddUsers; private javax.swing.JMenuItem menuEditMyAcc; private javax.swing.JMenu menulogout; // End of variables declaration }
  • 47. HND in Computing & Programming in System Development Java 47 Student Details form Figure 34 Program Code: /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package heinstitute; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.swing.JOptionPane; import javax.swing.table.DefaultTableModel; /** * * @author Anushka */
  • 48. HND in Computing & Programming in System Development Java 48 public class StudentDetails extends javax.swing.JFrame { /** * Creates new form StudentDetails */ DefaultTableModel model; public StudentDetails(boolean q) { initComponents(); model = new DefaultTableModel(); model.addColumn("Student_ID"); model.addColumn("Student_Name"); model.addColumn("Age"); model.addColumn("Telephone_no"); tblStudent.setModel(model); this.setUserLimitations(q); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jButton1 = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); tblStudent = new javax.swing.JTable(); btnDelete = new javax.swing.JButton(); btnEdit = new javax.swing.JButton(); txtStudentID = new javax.swing.JTextField(); btnAddNewStudent = new
  • 49. HND in Computing & Programming in System Development Java 49 javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jButton1.setBackground(new java.awt.Color(255, 153, 0)); jButton1.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N jButton1.setText("OK"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); tblStudent.setAutoCreateRowSorter(true); tblStudent.setModel(new javax.swing.table.DefaultTableModel( new Object [][] { {null, null, null, null}, {null, null, null, null}, {null, null, null, null}, {null, null, null, null}, {null, null, null, null}, {null, null, null, null}, {null, null, null, null}, {null, null, null, null}, {null, null, null, null}, {null, null, null, null} }, new String [] { "Title 1", "Title 2", "Title 3", "Title 4" } )); jScrollPane1.setViewportView(tblStudent); if (tblStudent.getColumnModel().getColumnCount() > 0) { tblStudent.getColumnModel().getColumn(0).setHeaderValue(" Title 1"); tblStudent.getColumnModel().getColumn(1).setHeaderValue(" Title 2"); tblStudent.getColumnModel().getColumn(2).setHeaderValue(" Title 3");
  • 50. HND in Computing & Programming in System Development Java 50 tblStudent.getColumnModel().getColumn(3).setHeaderValue(" Title 4"); } btnDelete.setBackground(new java.awt.Color(0, 0, 0)); btnDelete.setForeground(new java.awt.Color(255, 255, 255)); btnDelete.setText("Delete Student Details"); btnDelete.setHorizontalTextPosition(javax.swing.SwingConstants.RIG HT); btnDelete.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnDeleteActionPerformed(evt); } }); btnEdit.setBackground(new java.awt.Color(0, 0, 0)); btnEdit.setForeground(new java.awt.Color(255, 255, 255)); btnEdit.setText("Edit Student Detais"); btnEdit.setHorizontalTextPosition(javax.swing.SwingConstants.RIG HT); btnEdit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnEditActionPerformed(evt); } }); txtStudentID.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { txtStudentIDActionPerformed(evt); } }); btnAddNewStudent.setBackground(new java.awt.Color(0, 0, 0)); btnAddNewStudent.setForeground(new java.awt.Color(255, 255, 255)); btnAddNewStudent.setText("Add New Student Detail"); btnAddNewStudent.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnAddNewStudentActionPerformed(evt); } });
  • 51. HND in Computing & Programming in System Development Java 51 jLabel1.setText("Student ID :"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(layout.createSequentialGroup() .addComponent(btnAddNewStudent) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnEdit) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 742, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(txtStudentID, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(18, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  • 52. HND in Computing & Programming in System Development Java 52 .addComponent(txtStudentID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel1)) .addGap(18, 18, 18) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnAddNewStudent, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap()) ); pack(); }// </editor-fold> public void setUserLimitations(boolean prm){ btnDelete.setEnabled(prm); } public void findStudentDetails() throws ClassNotFoundException, SQLException{ // connect to the sql server Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String connectionUrl = "jdbc:sqlserver://Anushka- PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;"; Connection connection = DriverManager.getConnection(connectionUrl); System.out.println("Successfuly Connected ");
  • 53. HND in Computing & Programming in System Development Java 53 String SQL1 = " SELECT * FROM Student_Details WHERE Student_ID LIKE" + "'%"+ txtStudentID.getText() +"%'"; Statement stmt2 = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet rls = stmt2.executeQuery(SQL1); this.refreshTable(); while(rls.next()) { model.addRow(new Object[]{ rls.getString(1), rls.getString(2), rls.getString(3), rls.getString(4), } ); } } //--------refreshthe table------- ---- public void refreshTable(){ while (model.getRowCount() > 0) { model.removeRow(0); } } // Delete Student details------------------- public void deletestudentDetails(String selRow) throws ClassNotFoundException, SQLException { // connect to the sql server Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String connectionUrl = "jdbc:sqlserver://Anushka- PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;"; Connection connection = DriverManager.getConnection(connectionUrl); System.out.println("Successfuly Connected ");
  • 54. HND in Computing & Programming in System Development Java 54 Statement statement = connection.createStatement(); statement.executeUpdate("DELETE FROM Student_Details WHERE Student_ID='" + selRow + "'"); } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: this.setVisible(false); } private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: try{ if (tblStudent.getSelectedRow() < 0 || tblStudent.getSelectedColumn() < 0) { JOptionPane.showMessageDialog(this, "Select a Record From Table To Delete","NO RECORD SELECTED",JOptionPane.WARNING_MESSAGE); } else { int respond =JOptionPane.showConfirmDialog(this, "Are You Sure You Want To Delete This?","Confirm Deletion",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTIN_ MESSAGE); if (respond==0){ String selectedRec = (String)tblStudent.getValueAt(tblStudent.getSelectedRow(), 0); this.deletestudentDetails(selectedRec); // deleting a cake from Db this.refreshTable(); this.findStudentDetails(); } else{ } } }//end try catch (ClassNotFoundException e){ System.out.println(e); }
  • 55. HND in Computing & Programming in System Development Java 55 catch (SQLException e){ JOptionPane.showMessageDialog(null, e, "ERROR", JOptionPane.ERROR_MESSAGE); } } private void btnEditActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: if (tblStudent.getSelectedRow() <0 || tblStudent.getSelectedColumn()<0) { JOptionPane.showMessageDialog(this, "First select a record to Edit!", "NO RECORD SELECTED", JOptionPane.WARNING_MESSAGE); } else { String st = (String) tblStudent.getValueAt(tblStudent.getSelectedRow(), 0); EditStudentDetails editstudentdetails = new EditStudentDetails(st); editstudentdetails.setVisible(true); editstudentdetails.setLocationRelativeTo(null); editstudentdetails.setResizable(false); } } private void btnAddNewStudentActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: AddNewStudent addnewstudent = new AddNewStudent (); addnewstudent.setVisible(true); addnewstudent.setResizable(false); addnewstudent.setLocationRelativeTo(null); } private void txtStudentIDActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: try{ if (txtStudentID.getText().equals("")){ //txtStudentID empty JOptionPane.showMessageDialog(null, "The Student ID Field Is Empty,Enter Student ID And Search", "EMPTY FIELD", JOptionPane.ERROR_MESSAGE); }
  • 56. HND in Computing & Programming in System Development Java 56 else{//txtxCake not empty this.findStudentDetails(); if(model.getRowCount()==0){ JOptionPane.showMessageDialog(this, "No Data Found", "NO DATA", JOptionPane.ERROR_MESSAGE); } } }//end try catch (ClassNotFoundException e){ System.out.println(e); } catch (SQLException e){ JOptionPane.showMessageDialog(null, e, "ERROR IN CONNECTION", JOptionPane.ERROR_MESSAGE); } } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) {
  • 57. HND in Computing & Programming in System Development Java 57 java.util.logging.Logger.getLogger(StudentDetails.class.getName()).log(java.util.logging.Level. SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(StudentDetails.class.getName()).log(java.util.logging.Level. SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(StudentDetails.class.getName()).log(java.util.logging.Level. SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(StudentDetails.class.getName()).log(java.util.logging.Level. SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { // new StudentDetails().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton btnAddNewStudent; private javax.swing.JButton btnDelete; private javax.swing.JButton btnEdit; private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTable tblStudent; private javax.swing.JTextField txtStudentID; // End of variables declaration }
  • 58. HND in Computing & Programming in System Development Java 58 Add New Student Details Form Figure 35 Program Code: /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package heinstitute; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.swing.JOptionPane; /** * * @author Anushka */ public class AddNewStudent extends javax.swing.JFrame { /**
  • 59. HND in Computing & Programming in System Development Java 59 * Creates new form AddNewStudent */ public AddNewStudent() { initComponents(); try{ this.generateID(); }//end try catch (ClassNotFoundException e){ System.out.println(e); } catch (SQLException e){ System.out.println(e); } } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jButton1 = new javax.swing.JButton(); jPanel1 = new javax.swing.JPanel(); jLabel5 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jLabel1 = new javax.swing.JLabel(); txtstudID = new javax.swing.JTextField(); jLabel4 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel6 = new javax.swing.JLabel(); txtstudName = new javax.swing.JTextField(); txtAge = new javax.swing.JTextField(); txtTp = new javax.swing.JTextField(); txtCsID = new javax.swing.JTextField(); txtBatchID = new javax.swing.JTextField(); btnAdd = new javax.swing.JButton(); btnClear = new javax.swing.JButton(); jSeparator2 = new javax.swing.JSeparator(); jLabel7 = new javax.swing.JLabel();
  • 60. HND in Computing & Programming in System Development Java 60 setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jButton1.setBackground(new java.awt.Color(255, 153, 0)); jButton1.setText("ok"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Add new Student", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION,new java.awt.Font("Arial Unicode MS", 0, 14), new java.awt.Color(255, 153, 0))); // NOI18N jLabel5.setText("Name :"); jLabel3.setText("Age :"); jLabel1.setText("Student ID :"); txtstudID.setDisabledTextColor(new java.awt.Color(255, 51, 51)); txtstudID.setEnabled(false); jLabel4.setText("Telephone_no :"); jLabel2.setText("Course ID :"); jLabel6.setText("Batch ID :"); txtAge.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { txtAgeActionPerformed(evt); } }); txtTp.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { txtTpActionPerformed(evt); } });
  • 61. HND in Computing & Programming in System Development Java 61 javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addComponent(jLabel3) .addComponent(jLabel4) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) .addGap(10, 10, 10) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(txtstudName) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(txtstudID, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(txtAge, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE) .addComponent(txtTp) .addComponent(txtCsID) .addComponent(txtBatchID)) .addGap(0, 0, Short.MAX_VALUE))) .addContainerGap()) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  • 62. HND in Computing & Programming in System Development Java 62 .addComponent(txtstudID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel1)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel5) .addComponent(txtstudName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3) .addComponent(txtAge, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel4) .addComponent(txtTp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2) .addComponent(txtCsID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(txtBatchID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel6)) .addGap(5, 5, 5)) ); btnAdd.setText("Save"); btnAdd.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnAddActionPerformed(evt); } }); btnClear.setText("Clear");
  • 63. HND in Computing & Programming in System Development Java 63 btnClear.setHorizontalTextPosition(javax.swing.SwingConstants.RIG HT); btnClear.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnClearActionPerformed(evt); } }); jLabel7.setText("He Institute "); jLabel7.setEnabled(false); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap() .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup() .addGap(12, 12, 12) .addComponent(btnClear, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 111, Short.MAX_VALUE) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel7) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jSeparator2))) .addContainerGap()) ); layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnAdd, btnClear, jButton1}); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  • 64. HND in Computing & Programming in System Development Java 64 .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGap(21, 21, 21) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnClear)) .addContainerGap()) ); layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btnAdd, btnClear, jButton1}); pack(); }// </editor-fold> public void add_student() throws ClassNotFoundException, SQLException{ // connection to the sql server Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String connectionUrl = "jdbc:sqlserver://Anushka- PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;"; Connection connection = DriverManager.getConnection(connectionUrl); System.out.println("The database is Connected "); Statement statement = connection.createStatement(); if(txtstudName.getText().equals("")){ JOptionPane.showMessageDialog(null, "The Student name Field Is Empty,Enter the Student name Name", "EMPTY FIELD", JOptionPane.ERROR_MESSAGE); } else if(txtAge.getText().equals("")){ JOptionPane.showMessageDialog(null, "The Age Field Is Empty,Enter the Student Age", "EMPTY FIELD", JOptionPane.ERROR_MESSAGE);
  • 65. HND in Computing & Programming in System Development Java 65 } else if(txtTp.getText().equals("")){ JOptionPane.showMessageDialog(null, "The Telephone number Field Is Empty,Enter the Student Telephone number", "EMPTY FIELD", JOptionPane.ERROR_MESSAGE); } else if(txtCsID.getText().equals("")){ JOptionPane.showMessageDialog(null, "The Course ID Field Is Empty,Enter a Course ID", "EMPTY FIELD", JOptionPane.ERROR_MESSAGE); } else if(txtBatchID.getText().equals("")){ JOptionPane.showMessageDialog(null, "The Batch ID Field Is Empty,Enter a Batch ID", "EMPTY FIELD", JOptionPane.ERROR_MESSAGE); } else { statement.executeUpdate("INSERT INTO Student_Details VALUES " +"( '" + txtstudID.getText() + "'" + "," + "'" + txtstudName.getText() + "'" + "," + "'" + txtAge.getText()+ "'" + "," + "'" + txtTp.getText() + "'" + ")" + ""); statement.executeUpdate("INSERT INTO [Student_ Course] VALUES "+"( '" + txtstudID.getText() + "'" + "," + "'" + txtCsID.getText() + "'" + ")" + ""); statement.executeUpdate("INSERT INTO [Student_ Batch] VALUES "+"( '" + txtstudID.getText() + "'" + "," + "'" + txtBatchID.getText() + "'" + ")" + ""); JOptionPane.showMessageDialog(null, "Record has been saved in the database", "INSERT SUCCESSFUL", JOptionPane.INFORMATION_MESSAGE); } } //-----------------------End of Add Student-------------------------------------------------------
  • 66. HND in Computing & Programming in System Development Java 66 public void clearAll(){ txtstudName.setText(""); txtstudID.setText(""); txtAge.setText(""); txtTp.setText(""); txtCsID.setText(""); txtBatchID.setText(""); } public void generateID()throws ClassNotFoundException, SQLException{ Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String connectionUrl = "jdbc:sqlserver://Anushka- PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;"; Connection connection = DriverManager.getConnection(connectionUrl); System.out.println("The database is Connected "); String sql="SELECT * FROM Student_Details ORDER BY Student_ID ASC"; Statement stmt2 = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet rs = stmt2.executeQuery(sql); int id; if(!rs.next()){ id=1; txtstudID.setText(Integer.toString(id)); } else{ rs.last(); id=Integer.valueOf(rs.getString(1)); String str_newid=Integer.toString(id+1); txtstudID.setText(str_newid); } }// end of get details from student private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: this.setVisible(false);
  • 67. HND in Computing & Programming in System Development Java 67 } private void btnAddActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: try{ this.add_student();//add new cake to database this.clearAll(); this.generateID(); }//end try catch (ClassNotFoundException e){ System.out.println(e); } catch (SQLException e){ JOptionPane.showMessageDialog(null,e, "ERROR", JOptionPane.ERROR_MESSAGE); System.out.println(e); } } private void btnClearActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: this.clearAll(); } private void txtAgeActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } private void txtTpActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  • 68. HND in Computing & Programming in System Development Java 68 * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(AddNewStudent.class.getName()).log(java.util.logging.Level.SEVER E, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(AddNewStudent.class.getName()).log(java.util.logging.Level.SEVER E, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(AddNewStudent.class.getName()).log(java.util.logging.Level.SEVER E, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(AddNewStudent.class.getName()).log(java.util.logging.Level.SEVER E, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new AddNewStudent().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton btnAdd; private javax.swing.JButton btnClear; private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3;
  • 69. HND in Computing & Programming in System Development Java 69 private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel7; private javax.swing.JPanel jPanel1; private javax.swing.JSeparator jSeparator2; private javax.swing.JTextField txtAge; private javax.swing.JTextField txtBatchID; private javax.swing.JTextField txtCsID; private javax.swing.JTextField txtTp; private javax.swing.JTextField txtstudID; private javax.swing.JTextField txtstudName; // End of variables declaration } Edit Student Details Form Figure 36 Program Code: /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */
  • 70. HND in Computing & Programming in System Development Java 70 package heinstitute; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.swing.JOptionPane; /** * * @author Anushka */ public class EditStudentDetails extends javax.swing.JFrame { /** * Creates new form EditStudentDetails */ public EditStudentDetails(String id) { initComponents(); txtstudID.setText(id); try{ this.setStudentDetails(id); }//end try catch (ClassNotFoundException e){ System.out.println(e); } catch (SQLException e){ JOptionPane.showMessageDialog(null, e, "ERROR", JOptionPane.ERROR_MESSAGE); } } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */
  • 71. HND in Computing & Programming in System Development Java 71 @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { btnEdit = new javax.swing.JButton(); btnClear = new javax.swing.JButton(); btnCancel = new javax.swing.JButton(); jPanel1 = new javax.swing.JPanel(); jLabel5 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); txtstudName = new javax.swing.JTextField(); jLabel1 = new javax.swing.JLabel(); txtstudID = new javax.swing.JTextField(); jLabel4 = new javax.swing.JLabel(); txtAge = new javax.swing.JTextField(); txtTp = new javax.swing.JTextField(); jLabel6 = new javax.swing.JLabel(); jSeparator2 = new javax.swing.JSeparator(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); btnEdit.setText("Update "); btnEdit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnEditActionPerformed(evt); } }); btnClear.setText("Clear"); btnClear.setHorizontalTextPosition(javax.swing.SwingConstant s.RIGHT); btnClear.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnClearActionPerformed(evt); } }); btnCancel.setText("OK"); btnCancel.addActionListener(new java.awt.event.ActionListener() { public void
  • 72. HND in Computing & Programming in System Development Java 72 actionPerformed(java.awt.event.ActionEvent evt) { btnCancelActionPerformed(evt); } }); jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Edit Student details", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial Unicode MS", 0, 14), new java.awt.Color(255, 153, 0))); // NOI18N jLabel5.setText("Name :"); jLabel3.setText("Age :"); jLabel1.setText("Student ID :"); txtstudID.setDisabledTextColor(new java.awt.Color(255, 51, 51)); txtstudID.setEnabled(false); jLabel4.setText("Telephone_no :"); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING ) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILIN G, false) .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  • 73. HND in Computing & Programming in System Development Java 73 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING ) .addComponent(txtstudName, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(txtstudID, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING ) .addComponent(jLabel3) .addComponent(jLabel4)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(txtAge) .addComponent(txtTp, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)))) .addContainerGap(77, Short.MAX_VALUE)) ); jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel1, jLabel3, jLabel4, jLabel5}); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN E) .addComponent(jLabel1) .addComponent(txtstudID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN E) .addComponent(jLabel5) .addComponent(txtstudName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  • 74. HND in Computing & Programming in System Development Java 74 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN E) .addComponent(jLabel3) .addComponent(txtAge, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(9, 9, 9) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN E) .addComponent(jLabel4) .addComponent(txtTp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(53, 53, 53)) ); jLabel6.setText("He Institute "); jLabel6.setEnabled(false); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addComponent(btnClear) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnEdit) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(btnCancel)) .addGroup(layout.createSequentialGroup() .addComponent(jLabel6) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jSeparator2))) .addContainerGap()) );
  • 75. HND in Computing & Programming in System Development Java 75 layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnCancel, btnClear, btnEdit}); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(37, 37, 37) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnClear) .addComponent(btnCancel)) .addContainerGap()) ); layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btnCancel, btnClear, btnEdit}); pack(); }// </editor-fold> public void clearAll(){ txtstudName.setText(""); txtAge.setText(""); txtTp.setText(""); }//***********************end cl ****************** public void updateStudentDetails() throws ClassNotFoundException, SQLException
  • 76. HND in Computing & Programming in System Development Java 76 { // connection to the sql server Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String connectionUrl = "jdbc:sqlserver://Anushka- PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;"; Connection connection = DriverManager.getConnection(connectionUrl); System.out.println("Successfuly Connected "); Statement statement = connection.createStatement(); String sql = "UPDATE Student_Details SET Student_Name='"+ txtstudName.getText()+ "'" + ","+ "Age='"+ txtAge.getText()+ "'" + ","+ "Telephone_no='"+ txtTp.getText() + "'" + "WHERE Student_ID='"+ txtstudID.getText() +"'"; statement.executeUpdate(sql); }//------------------endupdate-------------------------------- public void setStudentDetails(String S_id) throws ClassNotFoundException, SQLException{ // sql server connection Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String connectionUrl = "jdbc:sqlserver://Anushka- PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;"; Connection connection = DriverManager.getConnection(connectionUrl); System.out.println("Successfuly Connected "); String SQL1 = " SELECT * FROM Student_Details WHERE Student_ID ='"+ S_id +"'"; Statement stmt2 = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet rls = stmt2.executeQuery(SQL1);
  • 77. HND in Computing & Programming in System Development Java 77 rls.first(); txtstudName.setText(rls.getString(2)); txtAge.setText(rls.getString(3)); txtTp.setText(rls.getString(4)); } private void btnEditActionPerformed(java.awt.event.ActionEvent evt) { try{ int respond =JOptionPane.showConfirmDialog(this, "Are You Sure You Want To Update This record?","Confirm Update",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE) ; if (respond==0){ this.updateStudentDetails(); this.setVisible(false); } else{ } }//end try catch (ClassNotFoundException e){ System.out.println(e); } catch (SQLException e){ JOptionPane.showMessageDialog(null, e, "ERROR", JOptionPane.ERROR_MESSAGE); } } private void btnClearActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: this.clearAll(); } private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: this.setVisible(false); }
  • 78. HND in Computing & Programming in System Development Java 78 /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(EditStudentDetails.class.getName()).log(java.util.logging.Le vel.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(EditStudentDetails.class.getName()).log(java.util.logging.Le vel.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(EditStudentDetails.class.getName()).log(java.util.logging.Le vel.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(EditStudentDetails.class.getName()).log(java.util.logging. Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new EditStudentDetails("s").setVisible(true); }
  • 79. HND in Computing & Programming in System Development Java 79 }); } // Variables declaration - do not modify private javax.swing.JButton btnCancel; private javax.swing.JButton btnClear; private javax.swing.JButton btnEdit; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JPanel jPanel1; private javax.swing.JSeparator jSeparator2; private javax.swing.JTextField txtAge; private javax.swing.JTextField txtTp; private javax.swing.JTextField txtstudID; private javax.swing.JTextField txtstudName; // End of variables declaration } Course Details Form Figure 37
  • 80. HND in Computing & Programming in System Development Java 80 Program Code: /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package heinstitute; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.swing.JOptionPane; import javax.swing.table.DefaultTableModel; /** * * @author Anushka */ public class CourseDetails extends javax.swing.JFrame { /** * Creates new form CourseDetails */ DefaultTableModel model; public CourseDetails(boolean r) { initComponents(); model = new DefaultTableModel();
  • 81. HND in Computing & Programming in System Development Java 81 model.addColumn("Course_ID"); model.addColumn("Course_Name"); model.addColumn("Duration"); model.addColumn("Course_Fee"); tblCourse.setModel(model); this.setUserLimitations(r); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { btnDelete = new javax.swing.JButton(); btnAddNewCourse = new javax.swing.JButton(); btnEdit = new javax.swing.JButton(); jButton1 = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); tblCourse = new javax.swing.JTable(); txtCourseID1 = new javax.swing.JTextField(); jLabel1 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_O N_CLOSE); setName("Course Details"); // NOI18N btnDelete.setBackground(new java.awt.Color(0, 0, 0)); btnDelete.setForeground(new java.awt.Color(255, 255, 255)); btnDelete.setText("Delete Course Details"); btnDelete.setHorizontalTextPosition(javax.swing.SwingConstant s.RIGHT); btnDelete.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnDeleteActionPerformed(evt);
  • 82. HND in Computing & Programming in System Development Java 82 } }); btnAddNewCourse.setBackground(new java.awt.Color(0, 0, 0)); btnAddNewCourse.setForeground(new java.awt.Color(255, 255, 255)); btnAddNewCourse.setText("Add New Course Detail"); btnAddNewCourse.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnAddNewCourseActionPerformed(evt); } }); btnEdit.setBackground(new java.awt.Color(0, 0, 0)); btnEdit.setForeground(new java.awt.Color(255, 255, 255)); btnEdit.setText("EditCourse Detais"); btnEdit.setHorizontalTextPosition(javax.swing.SwingConstant s.RIGHT); btnEdit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnEditActionPerformed(evt); } }); jButton1.setBackground(new java.awt.Color(255, 153, 0)); jButton1.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N jButton1.setText("OK"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); tblCourse.setModel(new javax.swing.table.DefaultTableModel( new Object [][] { {null, null, null, null}, {null, null, null, null}, {null, null, null, null}, {null, null, null, null}, {null, null, null, null},
  • 83. HND in Computing & Programming in System Development Java 83 {null, null, null, null}, {null, null, null, null}, {null, null, null, null}, {null, null, null, null}, {null, null, null, null} }, new String [] { "Title 1", "Title 2", "Title 3", "Title 4" } ) { boolean[] canEdit = new boolean [] { false, false, false, false }; public boolean isCellEditable(int rowIndex, int columnIndex) { return canEdit [columnIndex]; } }); tblCourse.getTableHeader().setReorderingAllowed(false); jScrollPane1.setViewportView(tblCourse); if (tblCourse.getColumnModel().getColumnCount() > 0) { tblCourse.getColumnModel().getColumn(0).setResizable( false); tblCourse.getColumnModel().getColumn(1).setResizable( false); tblCourse.getColumnModel().getColumn(2).setResizable( false); tblCourse.getColumnModel().getColumn(3).setResizable( false); } txtCourseID1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { txtCourseID1ActionPerformed(evt); } }); jLabel1.setText("Course ID");
  • 84. HND in Computing & Programming in System Development Java 84 javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(layout.createSequentialGroup() .addComponent(btnAddNewCourse) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(btnEdit) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(layout.createSequentialGroup() .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(txtCourseID1, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 742, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(12, Short.MAX_VALUE)) ); layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnAddNewCourse, btnDelete, btnEdit}); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGap(10, 10, 10)
  • 85. HND in Computing & Programming in System Development Java 85 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(txtCourseID1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel1)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 57, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(btnAddNewCourse, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(btnDelete, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap()) ); layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btnAddNewCourse, btnDelete, btnEdit}); pack(); }// </editor-fold> public void setUserLimitations(boolean prm){ btnDelete.setEnabled(prm); btnAddNewCourse.setEnabled(prm); btnEdit.setEnabled(prm); } public void findCourseDetails() throws ClassNotFoundException, SQLException{ // connect to the sql server Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String connectionUrl = "jdbc:sqlserver://Anushka-
  • 86. HND in Computing & Programming in System Development Java 86 PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;"; Connection connection = DriverManager.getConnection(connectionUrl); System.out.println("Successfuly Connected "); String SQL1 = " SELECT * FROM Course_Details WHERE Course_ID LIKE" + "'%"+ txtCourseID1.getText() +"%'"; Statement stmt2 = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet rls1 = stmt2.executeQuery(SQL1); this.refreshTable(); while(rls1.next()) { model.addRow(new Object[]{ rls1.getString(1), rls1.getString(2), rls1.getString(3), rls1.getString(4), } ); } } //--------refreshthe table------- ---- public void refreshTable(){ while (model.getRowCount() > 0) { model.removeRow(0); } }
  • 87. HND in Computing & Programming in System Development Java 87 // Delete SCourse details------------------- public void deleteCourseDetails(String selRow) throws ClassNotFoundException, SQLException { // connect to the sql server Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String connectionUrl = "jdbc:sqlserver://Anushka- PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;"; Connection connection = DriverManager.getConnection(connectionUrl); System.out.println("Successfuly Connected "); Statement statement = connection.createStatement(); statement.executeUpdate("DELETE FROM Course_Details WHERE Course_ID='" + selRow + "'"); } private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: try{ if (tblCourse.getSelectedRow() < 0 || tblCourse.getSelectedColumn() < 0) { JOptionPane.showMessageDialog(this, "Select a Record From Table To Delete","NO RECORD SELECTED",JOptionPane.WARNING_MESSAGE); } else { int respond =JOptionPane.showConfirmDialog(this, "Are You Sure You Want To Delete This?","Confirm Deletion",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTIO N_MESSAGE); if (respond==0){ String selectedRec = (String)tblCourse.getValueAt(tblCourse.getSelectedRow(), 0); this.deleteCourseDetails(selectedRec); // deleting a cake from Db this.refreshTable(); this.findCourseDetails(); } else{ } }
  • 88. HND in Computing & Programming in System Development Java 88 }//end try catch (ClassNotFoundException e){ System.out.println(e); } catch (SQLException e){ JOptionPane.showMessageDialog(null, e, "ERROR", JOptionPane.ERROR_MESSAGE); } } private void btnAddNewCourseActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: AddNewCourse addnewcourse = new AddNewCourse (); addnewcourse.setVisible(true); addnewcourse.setResizable(false); addnewcourse.setLocationRelativeTo(null); } private void btnEditActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: if (tblCourse.getSelectedRow() <0 || tblCourse.getSelectedColumn()<0) { JOptionPane.showMessageDialog(this, "First select a record to Edit!", "NO RECORD SELECTED", JOptionPane.WARNING_MESSAGE); } else { String str = (String) tblCourse.getValueAt(tblCourse.getSelectedRow(), 0); EditCourseDetails editcoursedetails = new EditCourseDetails(str); editcoursedetails.setVisible(true); editcoursedetails.setLocationRelativeTo(null); editcoursedetails.setResizable(false); } } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: this.setVisible(false); } private void txtCourseID1ActionPer formed(java.awt.event. ActionEvent evt) {
  • 89. HND in Computing & Programming in System Development Java 89 // TODO add your handling code here: try{ if (txtCourseID1.getText().equals("")){ //txtcake empty JOptionPane.showMessageDialog(null, "The Cake Name Field Is Empty,Enter Cake Name And Search", "EMPTY FIELD", JOptionPane.ERROR_MESSAGE); } else{//txtxCake not empty this.findCourseDetails(); if(model.getRowCount()==0){ JOptionPane.showMessageDialog(this, "No Data Found", "NO DATA", JOptionPane.ERROR_MESSAGE); } } }//end try catch (ClassNotFoundException e){ System.out.println(e); } catch (SQLException e){ JOptionPane.showMessageDialog(null, e, "ERROR IN CONNECTION", JOptionPane.ERROR_MESSAGE); } } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  • 90. HND in Computing & Programming in System Development Java 90 * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(CourseDetails.class.getName()).log(java.util.logging.Level.S EVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(CourseDetails.class.getName()).log(java.util.logging.Level.S EVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(CourseDetails.class.getName()).log(java.util.logging.Le vel.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(CourseDetails.class.getName()).log(java.util.logging.Level.S EVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { //new CourseDetails().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton btnAddNewCourse; private javax.swing.JButton btnDelete; private javax.swing.JButton btnEdit; private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1;
  • 91. HND in Computing & Programming in System Development Java 91 private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTable tblCourse; private javax.swing.JTextField txtCourseID1; // End of variables declaration } Add Course detail Form Figure 38 Program Code: /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package heinstitute; import java.sql.Connection; import java.sql.DriverManager;
  • 92. HND in Computing & Programming in System Development Java 92 import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.swing.JOptionPane; /** * * @author Anushka */ public class AddNewCourse extends javax.swing.JFrame { /** * Creates new form AddNewCourse */ public AddNewCourse() { initComponents(); try{ this.generateID(); }//end try catch (ClassNotFoundException e){ System.out.println(e); } catch (SQLException e){ System.out.println(e); } } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() {
  • 93. HND in Computing & Programming in System Development Java 93 jPanel1 = new javax.swing.JPanel(); jLabel5 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jLabel1 = new javax.swing.JLabel(); txtCsID = new javax.swing.JTextField(); jLabel4 = new javax.swing.JLabel(); txtcsName = new javax.swing.JTextField(); txtDuration = new javax.swing.JTextField(); txtFee = new javax.swing.JTextField(); btnClear = new javax.swing.JButton(); btnSave = new javax.swing.JButton(); btnCancel = new javax.swing.JButton(); jLabel6 = new javax.swing.JLabel(); jSeparator2 = new javax.swing.JSeparator(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Add New Course ", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial Unicode MS", 0, 14), new java.awt.Color(255, 153, 0))); // NOI18N jLabel5.setText("Course Name :"); jLabel3.setText("Duration :"); jLabel1.setText("Course ID :"); txtCsID.setDisabledTextColor(new java.awt.Color(255, 51, 51)); txtCsID.setEnabled(false); txtCsID.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { txtCsIDActionPerformed(evt); }
  • 94. HND in Computing & Programming in System Development Java 94 }); jLabel4.setText("Course fee :"); txtFee.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { txtFeeActionPerformed(evt); } }); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING ) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILIN G, false) .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGap(29, 29, 29) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING ) .addComponent(txtCsID, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(txtcsName, javax.swing.GroupLayout.DEFAULT_SIZE, 326, Short.MAX_VALUE))) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING ) .addComponent(jLabel3)
  • 95. HND in Computing & Programming in System Development Java 95 .addComponent(jLabel4)) .addGap(29, 29, 29) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(txtDuration) .addComponent(txtFee, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)))) .addContainerGap()) ); jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel1, jLabel3, jLabel4, jLabel5}); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN E) .addComponent(jLabel1) .addComponent(txtCsID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN E) .addComponent(jLabel5) .addComponent(txtcsName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN E) .addComponent(jLabel3) .addComponent(txtDuration, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(12, 12, 12) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN E) .addComponent(jLabel4) .addComponent(txtFee, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(53, 53, 53)) );
  • 96. HND in Computing & Programming in System Development Java 96 btnClear.setBackground(new java.awt.Color(0, 0, 0)); btnClear.setForeground(new java.awt.Color(255, 255, 255)); btnClear.setText("Clear"); btnClear.setHorizontalTextPosition(javax.swing.SwingConstant s.RIGHT); btnClear.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnClearActionPerformed(evt); } }); btnSave.setBackground(new java.awt.Color(0, 0, 0)); btnSave.setForeground(new java.awt.Color(255, 255, 255)); btnSave.setText("Save"); btnSave.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnSaveActionPerformed(evt); } }); btnCancel.setBackground(new java.awt.Color(255, 153, 0)); btnCancel.setText("OK"); btnCancel.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnCancelActionPerformed(evt); } }); jLabel6.setText("He Institute "); jLabel6.setEnabled(false); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap()
  • 97. HND in Computing & Programming in System Development Java 97 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addComponent(btnClear) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnSave, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addComponent(jLabel6) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jSeparator2))) .addContainerGap()) ); layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnClear, btnSave}); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(19, 19, 19) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnSave) .addComponent(btnClear) .addComponent(btnCancel))
  • 98. HND in Computing & Programming in System Development Java 98 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btnCancel, btnClear, btnSave}); pack(); }// </editor-fold> public void generateID()throws ClassNotFoundException, SQLException{ Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String connectionUrl = "jdbc:sqlserver://Anushka- PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;"; Connection connection = DriverManager.getConnection(connectionUrl); System.out.println("The database is Connected "); // connection ok String sql="SELECT * FROM Course_Details ORDER BY Course_ID ASC;"; Statement stmt2 = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet rs = stmt2.executeQuery(sql); int id; if(!rs.next()){ id=1; txtCsID.setText(Integer.toString(id)); } else{ rs.last() ; id=Integer.valueOf(rs.getString(1)); txtCsID.setText(Integer.toString(id+1)); } } public void add_Course() throws ClassNotFoundException, SQLException{ // connection to the sql server Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String connectionUrl = "jdbc:sqlserver://Anushka- PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;"; Connection connection = DriverManager.getConnection(connectionUrl); System.out.println("The database is Connected ");
  • 99. HND in Computing & Programming in System Development Java 99 Statement statement = connection.createStatement(); if(txtcsName.getText().equals("")){ JOptionPane.showMessageDialog(null, "The Course name Field Is Empty,Enter the Course name Name", "EMPTY FIELD", JOptionPane.ERROR_MESSAGE); } else if(txtDuration.getText().equals("")){ JOptionPane.showMessageDialog(null, "The Duration Field Is Empty,Enter the Duration", "EMPTY FIELD", JOptionPane.ERROR_MESSAGE); } else if(txtFee.getText().equals("")){ JOptionPane.showMessageDialog(null, "The Course Fee Field Is Empty,Enter the Course Fee ", "EMPTY FIELD", JOptionPane.ERROR_MESSAGE); } else if(txtCsID.getText().equals("")){ JOptionPane.showMessageDialog(null, "The Course ID Field Is Empty,Enter a Course ID", "EMPTY FIELD", JOptionPane.ERROR_MESSAGE); } else { statement.executeUpdate("INSERT INTO Course_Details VALUES " +"( '" + txtCsID.getText() + "'" + "," + "'" + txtcsName.getText() + "'" + "," + "'" + txtDuration.getText()+ "'" + "," + "'" + txtFee.getText() + "'" + ")" + ""); JOptionPane.showMessageDialog(null, "Record has been saved in the database", "INSERT SUCCESSFUL", JOptionPane.INFORMATION_MESSAGE); } } public void clearAll(){ txtCsID.setText(""); txtcsName.setText(""); txtDuration.setText(""); txtFee.setText(""); } private void btnClearActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here:
  • 100. HND in Computing & Programming in System Development Java 100 this.clearAll(); } private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) { try{ this.add_Course();//add new cake to database this.clearAll(); this.generateID(); }//end try catch (ClassNotFoundException e){ System.out.println(e); } catch (SQLException e){ JOptionPane.showMessageDialog(null,e, "ERROR", JOptionPane.ERROR_MESSAGE); System.out.println(e); } } private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: this.setVisible(false); } private void txtCsIDActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } private void txtFeeActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */
  • 101. HND in Computing & Programming in System Development Java 101 //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(AddNewCourse.class.getName()).log(java.util.logging.Level .SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(AddNewCourse.class.getName()).log(java.util.logging.Level .SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(AddNewCourse.class.getName()).log(java.util.logging.Level .SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(AddNewCourse.class.getName()).log(java.util.logging.Level .SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new AddNewCourse().setVisible(true); } }); } // Variables declaration - do not modify
  • 102. HND in Computing & Programming in System Development Java 102 private javax.swing.JButton btnCancel; private javax.swing.JButton btnClear; private javax.swing.JButton btnSave; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JPanel jPanel1; private javax.swing.JSeparator jSeparator2; private javax.swing.JTextField txtCsID; private javax.swing.JTextField txtDuration; private javax.swing.JTextField txtFee; private javax.swing.JTextField txtcsName; // End of variables declaration } Edit Course Details Form Figure 39
  • 103. HND in Computing & Programming in System Development Java 103 Program Code: /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package heinstitute; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.swing.JOptionPane; /** * * @author Anushka */ public class EditCourseDetails extends javax.swing.JFrame { /** * Creates new form EditCourseDetails */ public EditCourseDetails(String id) { initComponents(); txtcourseID.setText(id); try{ this.setCourseDetails(id); }//end try catch (ClassNotFoundException e){ System.out.println(e); }
  • 104. HND in Computing & Programming in System Development Java 104 catch (SQLException e){ JOptionPane.showMessageDialog(null, e, "ERROR", JOptionPane.ERROR_MESSAGE); } } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jPanel1 = new javax.swing.JPanel(); jLabel5 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); txtcsName = new javax.swing.JTextField(); jLabel1 = new javax.swing.JLabel(); txtcourseID = new javax.swing.JTextField(); jLabel4 = new javax.swing.JLabel(); txtDuration = new javax.swing.JTextField(); txtFee = new javax.swing.JTextField(); btnEdit = new javax.swing.JButton(); btnClear = new javax.swing.JButton(); btnCancel = new javax.swing.JButton(); jSeparator2 = new javax.swing.JSeparator(); jLabel6 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Edit Course details",
  • 105. HND in Computing & Programming in System Development Java 105 javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial Unicode MS", 0, 14), new java.awt.Color(255, 153, 0))); // NOI18N jLabel5.setText("Course Name :"); jLabel3.setText("Duration :"); jLabel1.setText("Course ID :"); txtcourseID.setDisabledTextColor(new java.awt.Color(255, 51, 51)); txtcourseID.setEnabled(false); jLabel4.setText("Course fee :"); txtFee.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { txtFeeActionPerformed(evt); } }); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment. LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING ) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILIN G, false) .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addComponent(jLabel3) .addComponent(jLabel4))
  • 106. HND in Computing & Programming in System Development Java 106 .addGap(29, 29, 29) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING ) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILIN G, false) .addComponent(txtDuration, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE) .addComponent(txtFee) .addComponent(txtcourseID, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(txtcsName, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(28, Short.MAX_VALUE)) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN E) .addComponent(jLabel1) .addComponent(txtcourseID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN E) .addComponent(jLabel5) .addComponent(txtcsName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN E) .addComponent(jLabel3) .addComponent(txtDuration, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(12, 12, 12)
  • 107. HND in Computing & Programming in System Development Java 107 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN E) .addComponent(jLabel4) .addComponent(txtFee, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(53, 53, 53)) ); jLabel1.getAccessibleContext().setAccessibleName("Course ID :"); btnEdit.setText("Update "); btnEdit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnEditActionPerformed(evt); } }); btnClear.setText("Clear"); btnClear.setHorizontalTextPosition(javax.swing.SwingConstant s.RIGHT); btnClear.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnClearActionPerformed(evt); } }); btnCancel.setText("OK"); btnCancel.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnCancelActionPerformed(evt); } }); jLabel6.setText("He Institute "); jLabel6.setEnabled(false); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  • 108. HND in Computing & Programming in System Development Java 108 .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup() .addGap(23, 23, 23) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(btnClear) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnEdit) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 148, Short.MAX_VALUE) .addComponent(btnCancel)) .addGroup(layout.createSequentialGroup() .addComponent(jLabel6) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jSeparator2))))) .addContainerGap()) ); layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnCancel, btnClear, btnEdit}); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(34, 34, 34) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
  • 109. HND in Computing & Programming in System Development Java 109 .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnClear) .addComponent(btnCancel)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btnCancel, btnClear, btnEdit}); pack(); }// </editor-fold> public void clearAll(){ txtcsName.setText(""); txtDuration.setText(""); txtFee.setText(""); } //----------------------end of clearing----------- public void updateCourseDetails() throws ClassNotFoundException, SQLException { // connection to the sql server Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String connectionUrl = "jdbc:sqlserver://Anushka- PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;"; Connection connection = DriverManager.getConnection(connectionUrl); System.out.println("Successfuly Connected "); Statement statement = connection.createStatement(); String sql = "UPDATE Course_Details SET Course_Name='"+ txtcsName.getText()+ "'" + ","+ "Duration='"+ txtDuration.getText()+ "'" + ","+ "Course_Fee='"+ txtFee.getText() + "'" +
  • 110. HND in Computing & Programming in System Development Java 110 "WHERE Course_ID='"+ txtcourseID.getText() +"'"; statement.executeUpdate(sql); } //--------------------------End Update-------------------------------- public void setCourseDetails(String CS_id) throws ClassNotFoundException, SQLException{ // sql server connection Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String connectionUrl = "jdbc:sqlserver://Anushka- PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;"; Connection connection = DriverManager.getConnection(connectionUrl); System.out.println("Successfuly Connected "); String SQL1 = " SELECT * FROM Course_Details WHERE Course_ID ='"+ CS_id +"'"; Statement stmt2 = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet rls = stmt2.executeQuery(SQL1); rls.first(); txtcsName.setText(rls.getString(2)); txtDuration.setText(rls.getString(3)); txtFee.setText(rls.getString(4)); } //--------------------------end setting------------------------------- private void btnEditActionPerformed(java.awt.event.ActionEvent evt) { try{ int respond =JOptionPane.showConfirmDialog(this, "Are You Sure You Want To Update This record?","Confirm
  • 111. HND in Computing & Programming in System Development Java 111 Update",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE) ; if (respond==0){ this.updateCourseDetails(); this.setVisible(false); } else{ } }//end try catch (ClassNotFoundException e){ System.out.println(e); } catch (SQLException e){ JOptionPane.showMessageDialog(null, e, "ERROR", JOptionPane.ERROR_MESSAGE); } } private void btnClearActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: this.clearAll(); } private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: this.setVisible(false); } private void txtFeeActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */
  • 112. HND in Computing & Programming in System Development Java 112 //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(EditCourseDetails.class.getName()).log(java.util.logging.Le vel.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(EditCourseDetails.class.getName()).log(java.util.logging.Le vel.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(EditCourseDetails.class.getName()).log(java.util.logging.Le vel.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(EditCourseDetails.class.getName()).log(java.util.logging.Le vel.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new EditCourseDetails("Cs").setVisible(true); } }); } // Variables declaration - do not modify
  • 113. HND in Computing & Programming in System Development Java 113 private javax.swing.JButton btnCancel; private javax.swing.JButton btnClear; private javax.swing.JButton btnEdit; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JPanel jPanel1; private javax.swing.JSeparator jSeparator2; private javax.swing.JTextField txtDuration; private javax.swing.JTextField txtFee; private javax.swing.JTextField txtcourseID; private javax.swing.JTextField txtcsName; // End of variables declaration } Batch details Form Figure 40
  • 114. HND in Computing & Programming in System Development Java 114 Program Code: /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package heinstitute; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.swing.JOptionPane; import javax.swing.table.DefaultTableModel; /** * * @author Anushka */ public class BatchDetails extends javax.swing.JFrame { /** * Creates new form BatchDetails */ DefaultTableModel model; public BatchDetails(boolean w) { initComponents(); model = new DefaultTableModel(); model.addColumn("Batch_ID"); model.addColumn("Start_Date"); model.addColumn("End_Date"); model.addColumn("Number_of_Students"); model.addColumn("Course_ID"); tblBatch.setModel(model);
  • 115. HND in Computing & Programming in System Development Java 115 this.setUserLimitations(w); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane(); tblBatch = new javax.swing.JTable(); btnEdit = new javax.swing.JButton(); btnAdd = new javax.swing.JButton(); btnCancel = new javax.swing.JButton(); txtbatchID = new javax.swing.JTextField(); jLabel1 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); tblBatch.setModel(new javax.swing.table.DefaultTableModel( new Object [][] { {null, null, null, null, null}, {null, null, null, null, null}, {null, null, null, null, null}, {null, null, null, null, null} }, new String [] { "Title 1", "Title 2", "Title 3", "Title 4", "Title 5" } ) { boolean[] canEdit = new boolean [] { true, true, true, true, false };
  • 116. HND in Computing & Programming in System Development Java 116 public boolean isCellEditable(int rowIndex, int columnIndex) { return canEdit [columnIndex]; } }); jScrollPane1.setViewportView(tblBatch); if (tblBatch.getColumnModel().getColumnCount() > 0) { tblBatch.getColumnModel().getColumn(4).setResizable(false); } btnEdit.setBackground(new java.awt.Color(0, 0, 0)); btnEdit.setForeground(new java.awt.Color(255, 255, 255)); btnEdit.setText("Edit Batch"); btnEdit.setHorizontalTextPosition(javax.swing.SwingConstant s.RIGHT); btnEdit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnEditActionPerformed(evt); } }); btnAdd.setBackground(new java.awt.Color(0, 0, 0)); btnAdd.setForeground(new java.awt.Color(255, 255, 255)); btnAdd.setText("Add Batch"); btnAdd.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnAddActionPerformed(evt); } }); btnCancel.setBackground(new java.awt.Color(255, 153, 0)); btnCancel.setText("OK"); btnCancel.setHorizontalTextPosition(javax.swing.SwingConstant s.RIGHT); btnCancel.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnCancelActionPerformed(evt); } });
  • 117. HND in Computing & Programming in System Development Java 117 txtbatchID.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { txtbatchIDActionPerformed(evt); } }); jLabel1.setText("Batch ID"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI NG) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(layout.createSequentialGroup() .addContainerGap(568, Short.MAX_VALUE) .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(txtbatchID, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addGap(12, 12, 12) .addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap()) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 729, Short.MAX_VALUE) .addContainerGap())) );
  • 118. HND in Computing & Programming in System Development Java 118 layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(txtbatchID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel1)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 310, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap()) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(48, 48, 48) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(52, Short.MAX_VALUE))) ); pack(); }// </editor-fold> public void setUserLimitations(boolean prm){ btnEdit.setEnabled(prm); btnAdd.setEnabled(prm); } public void findBatchDetails() throws ClassNotFoundException, SQLException{ // connect to the sql server Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String connectionUrl = "jdbc:sqlserver://Anushka- PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;";
  • 119. HND in Computing & Programming in System Development Java 119 Connection connection = DriverManager.getConnection(connectionUrl); System.out.println("Successfuly Connected "); String SQL1 = " SELECT * FROM Batch_Details WHERE Batch_ID LIKE" + "'%"+ txtbatchID.getText() +"%'"; Statement stmt2 = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet rls1 = stmt2.executeQuery(SQL1); this.refreshTable(); while(rls1.next()) { model.addRow(new Object[]{ rls1.getString(1), rls1.getString(2), rls1.getString(3), rls1.getString(4), rls1.getString(5), } ); } } // public void refreshTable(){ while (model.getRowCount() > 0) { model.removeRow(0); } } private void btnEditActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: if (tblBatch.getSelectedRow() <0 || tblBatch.getSelectedColumn()<0) {
  • 120. HND in Computing & Programming in System Development Java 120 JOptionPane.showMessageDialog(this, "Please Select record to edit!", "NO RECORD SELECTED", JOptionPane.WARNING_MESSAGE); } else { String s = (String) tblBatch.getValueAt(tblBatch.getSelectedRow(), 0); EditBatchDetails editbatch = new EditBatchDetails(s); editbatch.setVisible(true); editbatch.setLocationRelativeTo(null); editbatch.setResizable(false); } } private void btnAddActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: AddBatch addbatch = new AddBatch(); addbatch.setVisible(true); addbatch.setVisible(true); addbatch.setResizable(false); addbatch.setLocationRelativeTo(null); } private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: this.setVisible(false); } private void txtbatchIDActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: try{ if (txtbatchID.getText().equals("")){ //txtcake empty JOptionPane.showMessageDialog(null, "The Cake Name Field Is Empty,Enter Cake Name And Search", "EMPTY FIELD", JOptionPane.ERROR_MESSAGE); } else{//txtxCake not empty this.findBatchDetails(); if(model.getRowCount()==0){
  • 121. HND in Computing & Programming in System Development Java 121 JOptionPane.showMessageDialog(this, "No Data Found", "NO DATA", JOptionPane.ERROR_MESSAGE); } } }//end try catch (ClassNotFoundException e){ System.out.println(e); } catch (SQLException e){ JOptionPane.showMessageDialog(null, e, "ERROR IN CONNECTION", JOptionPane.ERROR_MESSAGE); } } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) {
  • 122. HND in Computing & Programming in System Development Java 122 java.util.logging.Logger.getLogger(BatchDetails.class.getName()).log(java.util.logging.Level.S EVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(BatchDetails.class.getName()).log(java.util.logging.Level.S EVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(BatchDetails.class.getName()).log(java.util.logging.Level.S EVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(BatchDetails.class.getName()).log(java.util.logging.Level.S EVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { //new BatchDetails().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton btnAdd; private javax.swing.JButton btnCancel; private javax.swing.JButton btnEdit; private javax.swing.JLabel jLabel1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTable tblBatch; private javax.swing.JTextField txtbatchID; // End of variables declaration }
  • 123. HND in Computing & Programming in System Development Java 123 Add New Batch Details Form Program Code: /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package heinstitute; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.swing.JOptionPane; /** *
  • 124. HND in Computing & Programming in System Development Java 124 * @author Anushka */ public class AddBatch extends javax.swing.JFrame { /** * Creates new form AddBatch */ public AddBatch() { initComponents(); try{ this.generateID(); }//end try catch (ClassNotFoundException e){ System.out.println(e); } catch (SQLException e){ System.out.println(e); } } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jPanel1 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); txtStartDate = new javax.swing.JFormattedTextField(); txtEndDate = new
  • 125. HND in Computing & Programming in System Development Java 125 javax.swing.JFormattedTextField(); txtNumofst = new javax.swing.JTextField(); txtCsID = new javax.swing.JTextField(); txtBatchID = new javax.swing.JTextField(); btnClear = new javax.swing.JButton(); btnAdd = new javax.swing.JButton(); btnOk = new javax.swing.JButton(); jSeparator2 = new javax.swing.JSeparator(); jLabel6 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Add New Batch", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 0, 12), new java.awt.Color(255, 153, 0))); // NOI18N jLabel1.setText("Batch ID :"); jLabel2.setText("Start Date :"); jLabel3.setText("End Date :"); jLabel4.setText("Number of students :"); jLabel5.setText("Course ID :"); txtStartDate.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("dd/mm/yyyy")))); txtStartDate.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR)); txtEndDate.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("dd/mm/yyyy")))); txtEndDate.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR)); txtBatchID.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N txtBatchID.setForeground(new java.awt.Color(240, 240, 240)); txtBatchID.setDisabledTextColor(new java.awt.Color(255, 0, 0)); txtBatchID.setEnabled(false);
  • 126. HND in Computing & Programming in System Development Java 126 javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING ) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(txtNumofst, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILIN G) .addComponent(txtStartDate, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING ) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(59, 59, 59) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING )
  • 127. HND in Computing & Programming in System Development Java 127 .addComponent(txtEndDate, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(txtCsID, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(txtBatchID, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(81, 81, 81)))))) .addContainerGap(74, Short.MAX_VALUE)) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN E) .addComponent(jLabel1) .addComponent(txtBatchID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(14, 14, 14) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING ) .addComponent(jLabel2) .addComponent(txtStartDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN E) .addComponent(jLabel3) .addComponent(txtEndDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(14, 14, 14) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN E) .addComponent(jLabel4)
  • 128. HND in Computing & Programming in System Development Java 128 .addComponent(txtNumofst, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN E) .addComponent(jLabel5) .addComponent(txtCsID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(37, Short.MAX_VALUE)) ); btnClear.setBackground(new java.awt.Color(0, 0, 0)); btnClear.setForeground(new java.awt.Color(255, 255, 255)); btnClear.setText("CLEAR"); btnClear.setPreferredSize(new java.awt.Dimension(70, 33)); btnClear.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnClearActionPerformed(evt); } }); btnAdd.setBackground(new java.awt.Color(0, 0, 0)); btnAdd.setForeground(new java.awt.Color(255, 255, 255)); btnAdd.setText("Save"); btnAdd.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnAddActionPerformed(evt); } }); btnOk.setBackground(new java.awt.Color(255, 153, 0)); btnOk.setText("Ok"); btnOk.setHorizontalTextPosition(javax.swing.SwingConstants. CENTER); btnOk.setPreferredSize(new java.awt.Dimension(70, 33)); btnOk.addActionListener(new java.awt.event.ActionListener() { public void
  • 129. HND in Computing & Programming in System Development Java 129 actionPerformed(java.awt.event.ActionEvent evt) { btnOkActionPerformed(evt); } }); jLabel6.setText("He Institute "); jLabel6.setEnabled(false); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(layout.createSequentialGroup() .addComponent(btnClear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(btnOk, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createSequentialGroup() .addGap(6, 6, 6) .addComponent(jLabel6) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 316, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnAdd, btnClear}); layout.setVerticalGroup(
  • 130. HND in Computing & Programming in System Development Java 130 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(13, 13, 13) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(btnAdd) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnClear, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnOk, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(19, 19, 19)) ); layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btnAdd, btnClear, btnOk}); pack(); }// </editor-fold> public void generateID()throws ClassNotFoundException, SQLException{ Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String connectionUrl = "jdbc:sqlserver://Anushka- PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;"; Connection connection = DriverManager.getConnection(connectionUrl); System.out.println("The database is Connected "); String sql="SELECT * FROM Batch_Details ORDER BY Batch_ID ASC;"; Statement stmt2 = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
  • 131. HND in Computing & Programming in System Development Java 131 ResultSet rs = stmt2.executeQuery(sql); int id; if(!rs.next()){ id=1; txtBatchID.setText(Integer.toString(id)); } else{ rs.last() ; id=Inte ger.valu eOf(rs. getStrin g(1)); txtBatchID.setText(Integer.toString(id+1)); } } public void AddBatch() throws ClassNotFoundException, SQLException{ // connection to the sql server Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String connectionUrl = "jdbc:sqlserver://Anushka- PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;"; Connection connection = DriverManager.getConnection(connectionUrl); System.out.println("The database is Connected "); Statement statement = connection.createStatement(); if(txtStartDate.getText().equals("")){ JOptionPane.showMessageDialog(null, "The Start Date Field Is Empty,Enter the Start Date ", "EMPTY FIELD", JOptionPane.ERROR_MESSAGE); } else if(txtNumofst.getText().equals("")){ JOptionPane.showMessageDialog(null, "The Number of student Field Is Empty,Enter the Number of student ", "EMPTY FIELD", JOptionPane.ERROR_MESSAGE); } else if(txtCsID.getText().equals("")){ JOptionPane.showMessageDialog(null, "The Course ID Field Is Empty,Enter a Course ID", "EMPTY FIELD", JOptionPane.ERROR_MESSAGE);
  • 132. HND in Computing & Programming in System Development Java 132 } else { statement.executeUpdate("INSERT INTO Batch_Details VALUES " +"( '" + txtBatchID.getText() + "'" + "," + "'" + txtStartDate.getText() + "'" + "," + "'" + txtEndDate.getText()+ "'" + "," + "'" + txtNumofst.getText()+ "'" + "," + "'" + txtCsID.getText() + "'" + ")" + ""); JOptionPane.showMessageDialog(null, "Record has been saved in the database", "INSERT SUCCESSFUL", JOptionPane.INFORMATION_MESSAGE); } } public void clearAll(){ txtStartDate.setText(""); txtBatchID.setText(""); txtEndDate.setText(""); txtNumofst.setText(""); txtCsID.setText(""); } private void btnClearActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: this.clearAll(); } private void btnAddActionPerforme d(java.awt.event.Actio nEvent evt) { // TODO add your handling code here: try{
  • 133. HND in Computing & Programming in System Development Java 133 this.AddBatch();//add new user to database this.clearAll(); this.generateID(); }//end try catch (ClassNotFoundException e){ System.out.println(e); } catch (SQLException e){ JOptionPane.showMessageDialog(this, e, "ERROR", JOptionPane.ERROR_MESSAGE); } } private void btnOkActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: this.setVisible(false); } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) {
  • 134. HND in Computing & Programming in System Development Java 134 java.util.logging.Logger.getLogger(AddBatch.class.getName()).log(java.util.logging.Level.SEV ERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(AddBatch.class.getName()).log(java.util.logging.Level.SEV ERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(AddBatch.class.getName()).log(java.util.logging.Level.SEV ERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(AddBatch.class.getName()).log(java.util.logging.Level.SEV ERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new AddBatch().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton btnAdd; private javax.swing.JButton btnClear; private javax.swing.JButton btnOk; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JPanel jPanel1; private javax.swing.JSeparator jSeparator2; private javax.swing.JTextField txtBatchID; private javax.swing.JTextField
  • 135. HND in Computing & Programming in System Development Java 135 txtCsID; private javax.swing.JFormattedTextField txtEndDate; private javax.swing.JTextField txtNumofst; private javax.swing.JFormattedTextField txtStartDate; // End of variables declaration } Edit Batch Detail Form Figure 41 Program Code: /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package heinstitute; import java.sql.Connection; import
  • 136. HND in Computing & Programming in System Development Java 136 java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.swing.JOptionPane; /** * * @author Anushka */ public class EditBatchDetails extends javax.swing.JFrame { /** * Creates new form EditBatchDetails */ public EditBatchDetails(String id) { initComponents(); txtBatchID.setText(id); try{ this.setBatchDetails(id); }//end try catch (ClassNotFoundException e){ System.out.println(e); } catch (SQLException e){ JOptionPane.showMessageDialog(null, e, "ERROR", JOptionPane.ERROR_MESSAGE); } } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked")
  • 137. HND in Computing & Programming in System Development Java 137 public void clearAll(){ txtBatchID.setText(""); txtStartDate.setText(""); txtEndDate.setText(""); txtNumofst.setText(""); txtCsID.setText(""); }// end of clear all public void updateBatchDetails() throws ClassNotFoundException, SQLException { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String connectionUrl = "jdbc:sqlserver://Anushka- PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;"; Connection connection = DriverManager.getConnection(connectionUrl); System.out.println("The database is Connected "); Statement statement = connection.createStatement(); String sql = "UPDATE Batch_Details SET Start_date='"+ txtStartDate.getText()+ "'" + ","+ "End_date='"+ txtEndDate.getText()+ "'" + ","+ "number_Of_Student='"+ txtNumofst.getText() + "'" + "Course_ID='"+ txtCsID.getText() + "'" + "WHERE Batch_ID='"+ txtBatchID.getText() +"'"; statement.executeUpdate(sql); } //-------------------------end update method--------------------------- public void setBatchDetails(String Bt_id) throws ClassNotFoundException, SQLException{ // connection to the sql server Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  • 138. HND in Computing & Programming in System Development Java 138 String connectionUrl = "jdbc:sqlserver://Anushka- PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;"; Connection connection = DriverManager.getConnection(connectionUrl); System.out.println("The database is Connected "); // connection ok String SQL3 = " SELECT * FROM Batch_Details WHERE Batch_ID='"+ Bt_id +"'"; Statement stmt2 = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet rslst = stmt2.executeQuery(SQL3); rslst.first(); txtBatchID.setText(rslst.getString(1)); txtStartDate.setText(rslst.getString(2)); txtEndDate.setText(rslst.getString(3)); txtNumofst.setText(rslst.getString(4)); txtCsID.setText(rslst.getString(5)); } // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jPanel1 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); txtStartDate = new javax.swing.JFormattedTextField(); txtEndDate = new javax.swing.JFormattedTextField(); txtNumofst = new javax.swing.JTextField(); txtCsID = new javax.swing.JTextField(); txtBatchID = new javax.swing.JTextField(); btnClear = new javax.swing.JButton(); btnEdit1 = new javax.swing.JButton(); btnCancel = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  • 139. HND in Computing & Programming in System Development Java 139 jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Edit Batch Details", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 0, 12), new java.awt.Color(255, 153, 0))); // NOI18N jLabel1.setText("Batch ID :"); jLabel2.setText("Start Date :"); jLabel3.setText("End Date :"); jLabel4.setText("Number of students :"); jLabel5.setText("Course ID :"); txtStartDate.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("dd/mm/yyyy")))); txtStartDate.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR)); txtEndDate.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("dd/mm/yyyy")))); txtEndDate.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR)); txtBatchID.setDisabledTextColor(new java.awt.Color(255, 51, 51)); txtBatchID.setEnabled(false); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING ) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  • 140. HND in Computing & Programming in System Development Java 140 .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING ) .addComponent(txtStartDate, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(txtEndDate, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(txtCsID, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(txtBatchID, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(txtNumofst, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(67, Short.MAX_VALUE)) ); jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel4, jLabel5}); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN E) .addComponent(jLabel1) .addComponent(txtBatchID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  • 141. HND in Computing & Programming in System Development Java 141 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN E) .addComponent(jLabel2) .addComponent(txtStartDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING ) .addComponent(jLabel3) .addComponent(txtEndDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN E) .addComponent(jLabel4) .addComponent(txtNumofst, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN E) .addComponent(jLabel5) .addComponent(txtCsID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(37, Short.MAX_VALUE)) ); btnClear.setBackground(new java.awt.Color(0, 0, 0)); btnClear.setForeground(new java.awt.Color(255, 255, 255)); btnClear.setText("Clear"); btnClear.setHorizontalTextPosition(javax.swing.SwingConstant s.RIGHT); btnClear.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnClearActionPerformed(evt); } });
  • 142. HND in Computing & Programming in System Development Java 142 btnEdit1.setBackground(new java.awt.Color(0, 0, 0)); btnEdit1.setForeground(new java.awt.Color(255, 255, 255)); btnEdit1.setText("Update "); btnEdit1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnEdit1ActionPerformed(evt); } }); btnCancel.setBackground(new java.awt.Color(255, 153, 0)); btnCancel.setText("OK"); btnCancel.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnCancelActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(0, 0, Short.MAX_VALUE) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addComponent(btnClear) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnEdit1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(btnCancel))) .addContainerGap()) );
  • 143. HND in Computing & Programming in System Development Java 143 layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnCancel, btnClear, btnEdit1}); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(49, 49, 49) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnEdit1, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE) .addComponent(btnCancel)) .addComponent(btnClear, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btnCancel, btnClear, btnEdit1}); pack(); }// </editor-fold> private void btnEdit1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: try{ int respond =JOptionPane.showConfirmDialog(this, "Do You Want To Update This record?","Confirm Update",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTIO N_MESSAGE); if (respond==0){ this.updateBatchDetails(); this.setVisible(false); } else{ } }//end try
  • 144. HND in Computing & Programming in System Development Java 144 catch (ClassNotFoundException e){ System.out.println(e); } catch (SQLException e){ JOptionPane.showMessageDialog(null, e, "ERROR", JOptionPane.ERROR_MESSAGE); } } private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: this.setVisible(false); } private void btnClearActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: this.clearAll(); } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) {
  • 145. HND in Computing & Programming in System Development Java 145 java.util.logging.Logger.getLogger(EditBatchDetails.class.getName()).log(java.util.logging.Lev el.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(EditBatchDetails.class.getName()).log(java.util.logging.Lev el.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(EditBatchDetails.class.getName()).log(java.util.logging.Lev el.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(EditBatchDetails.class.getName()).log(java.util.logging.Lev el.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new EditBatchDetails("Bt").setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton btnCancel; private javax.swing.JButton btnClear; private javax.swing.JButton btnEdit1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JPanel jPanel1; private javax.swing.JTextField txtBatchID; private javax.swing.JTextField txtCsID; private javax.swing.JFormattedTextField txtEndDate; private javax.swing.JTextField txtNumofst; private javax.swing.JFormattedTextField txtStartDate;
  • 146. HND in Computing & Programming in System Development Java 146 // End of variables declaration } Create New User Account Form Figure 42 Program Code: /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package heinstitute; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import
  • 147. HND in Computing & Programming in System Development Java 147 javax.swing.JOptionPane; import javax.swing.table.DefaultTableMod el; /** * * @author Anushka */ public class AddUser extends javax.swing.JFrame { /** * Creates new form AddUser */ DefaultTableModel model; public AddUser() { initComponents(); try{ this.generateID(); }//end try catch (ClassNotFoundException e){ System.out.println(e); } catch (SQLException e){ System.out.println(e); } } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() {
  • 148. HND in Computing & Programming in System Development Java 148 btnOk = new javax.swing.JButton(); btnAdd = new javax.swing.JButton(); txtStaffid = new javax.swing.JTextField(); jLabel2 = new javax.swing.JLabel(); btnClear = new javax.swing.JButton(); jLabel5 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); txtStaffName = new javax.swing.JTextField(); txtPassword = new javax.swing.JPasswordField(); jLabel7 = new javax.swing.JLabel(); txtUserName = new javax.swing.JTextField(); jPanel1 = new javax.swing.JPanel(); rbStaff = new javax.swing.JRadioButton(); rbAdmin = new javax.swing.JRadioButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); btnOk.setText("Ok"); btnOk.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); btnOk.setPreferredSize(new java.awt.Dimension(70, 33)); btnOk.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnOkActionPerformed(evt); } }); btnAdd.setText("Save"); btnAdd.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnAddActionPerformed(evt); } }); txtStaffid.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N txtStaffid.setForeground(new java.awt.Color(240, 240, 240)); txtStaffid.setDisabledTextColor(new java.awt.Color(255, 0, 0)); txtStaffid.setEnabled(false); jLabel2.setText("Password");
  • 149. HND in Computing & Programming in System Development Java 149 btnClear.setText("CLEAR"); btnClear.setPreferredSize(new java.awt.Dimension(70, 33)); btnClear.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnClearActionPerformed(evt); } }); jLabel5.setText(" Staff Name"); jLabel3.setText("User Name"); jLabel7.setText(" Staff ID"); jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Account Type", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12))); // NOI18N rbStaff.setSelected(true); rbStaff.setText("Staff Account"); rbAdmin.setText("Admin Account"); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addGap(21, 21, 21) .addComponent(rbAdmin) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE) .addComponent(rbStaff) .addGap(18, 18, 18)) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(16, 16, 16)
  • 150. HND in Computing & Programming in System Development Java 150 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN E) .addComponent(rbStaff) .addComponent(rbAdmin)) .addContainerGap(20, Short.MAX_VALUE)) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(txtStaffid, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(txtStaffName))) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 6, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  • 151. HND in Computing & Programming in System Development Java 151 .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addGap(6, 6, 6) .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(txtUserName) .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(layout.createSequentialGroup() .addGap(30, 30, 30) .addComponent(btnClear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(btnOk, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(0, 117, Short.MAX_VALUE))) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap(36, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(txtStaffid, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel7)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel5)
  • 152. HND in Computing & Programming in System Development Java 152 .addComponent(txtStaffName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(txtUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2) .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(28, 28, 28) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnOk, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnClear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(20, 20, 20)) ); pack(); }// </editor-fold> public void generateID()throws ClassNotFoundException, SQLException{ Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String connectionUrl = "jdbc:sqlserver://Anushka- PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;"; Connection connection = DriverManager.getConnection(connectionUrl); System.out.println("The database is Connected "); // connection ok String sql="SELECT * FROM Staff ORDER BY Staff_ID ASC;"; Statement stmt2 = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
  • 153. HND in Computing & Programming in System Development Java 153 ResultSet.CONCUR_READ_ONLY); ResultSet rs = stmt2.executeQuery(sql); int id; if(!rs.next()){ id=1; txtStaffid.setText(Integer.toString(id)); } else{ rs.last() ; id=Integer.valueOf(rs.getString(1)); txtStaffid.setText(Integer.toString(id+1)); } }// end of generateID public void clearAll(){ txtStaffName.setText(""); txtUserName.setText(""); txtPassword.setText(""); }// end ********************************** public void add_User() throws ClassNotFoundException, SQLException{ // connection to the sql server Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String connectionUrl = "jdbc:sqlserver://Anushka- PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;"; Connection connection = DriverManager.getConnection(connectionUrl); System.out.println("Successfuly Connected to MS SQL 2005 using windows authentication 1"); // connection ok // inserting new user Statement statement = connection.createStatement(); if(txtStaffName.getText().equals("")){
  • 154. HND in Computing & Programming in System Development Java 154 JOptionPane.showMessageDialog(null, "The Staff Name Field Is Empty,Enter Staff Name", "EMPTY FIELD", JOptionPane.ERROR_MESSAGE); } else if(txtUserName.getText().equals("")){ JOptionPane.showMessageDialog(null, "The User Name Field Is Empty,Enter User Name", "EMPTY FIELD", JOptionPane.ERROR_MESSAGE); } else if(txtPassword.getText().equals("")){ JOptionPane.showMessageDialog(null, "The Password Field Is Empty,Enter Password", "EMPTY FIELD", JOptionPane.ERROR_MESSAGE); } else { statement.executeUpdate("INSERT INTO Staff VALUES " +"( '" + txtStaffid.getText() + "'" + "," + "'" + txtStaffName.getText() + "'" + "," + "'" + txtUserName.getText() + "'" + "," + "'" + txtPassword.getText() + "'" + "," + "'" + this.getAccType() + "'" + ")" +" "); JOptionPane.showMessageDialog(null, "Record are saved in the database", "INSERT SUCCESSFUL", JOptionPane.INFORMATION_MESSAGE); } } public boolean getAccType(){ if(rbStaff.isSelected()==true){ return false; } else if(rbAdmin.isSelected()==true){ return true; } else{ return false; } }
  • 155. HND in Computing & Programming in System Development Java 155 private void btnOkActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: this.setVisible(false); } private void btnAddActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: try{ this.add_User();//add new user to database this.clearAll(); this.generateID(); }//end try catch (ClassNotFoundException e){ System.out.println(e); } catch (SQLException e){ JOptionPane.showMessageDialog(this, e, "ERROR", JOptionPane.ERROR_MESSAGE); } } private void btnClearActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: this.clearAll(); } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try {
  • 156. HND in Computing & Programming in System Development Java 156 for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(AddUser.class.getName()).log(java.util.logging.Level.SEVE RE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(AddUser.class.getName()).log(java.util.logging.Level.SEVE RE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(AddUser.class.getName()).log(java.util.logging.Level.SEVE RE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(AddUser.class.getName()).log(java.util.logging.Level.SEVE RE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new AddUser().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton btnAdd; private javax.swing.JButton btnClear; private javax.swing.JButton btnOk; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3;
  • 157. HND in Computing & Programming in System Development Java 157 private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel7; private javax.swing.JPanel jPanel1; private javax.swing.JRadioButton rbAdmin; private javax.swing.JRadioButton rbStaff; private javax.swing.JPasswordField txtPassword; private javax.swing.JTextField txtStaffName; private javax.swing.JTextField txtStaffid; private javax.swing.JTextField txtUserName; // End of variables declaration } Edit User Account Form Figure 43 Program Code: /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */
  • 158. HND in Computing & Programming in System Development Java 158 package heinstitute; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.swing.JOptionPane; /** * * @author Anushka */ public class EditMyUserAcc extends javax.swing.JFrame { /** * Creates new form EditMyUserAcc */ public EditMyUserAcc(int staffid) { initComponents(); try{ this.getDetails(staffid); }//end try catch (ClassNotFoundException e){ System.out.println(e); } catch (SQLException e){ JOptionPane.showMessageDialog(null, e, "ERROR", JOptionPane.ERROR_MESSAGE); } } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked")
  • 159. HND in Computing & Programming in System Development Java 159 // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { btnClear = new javax.swing.JButton(); btnOk = new javax.swing.JButton(); btnUpdate = new javax.swing.JButton(); jPanel1 = new javax.swing.JPanel(); txtStaffid = new javax.swing.JTextField(); txtUserName = new javax.swing.JTextField(); jLabel2 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jLabel7 = new javax.swing.JLabel(); txtPassword = new javax.swing.JTextField(); txtStaffName = new javax.swing.JTextField(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); btnClear.setText("Clear"); btnClear.setPreferredSize(new java.awt.Dimension(70, 33)); btnClear.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnClearActionPerformed(evt); } }); btnOk.setText("Ok"); btnOk.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR)); btnOk.setHorizontalTextPosition(javax.swing.SwingConstants.C ENTER); btnOk.setPreferredSize(new java.awt.Dimension(70, 33)); btnOk.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnOkActionPerformed(evt); }
  • 160. HND in Computing & Programming in System Development Java 160 }); btnUpdate.setText("Save"); btnUpdate.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnUpdateActionPerformed(evt); } }); jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Edit User Account", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 0, 12), new java.awt.Color(255, 153, 0))); // NOI18N txtStaffid.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N txtStaffid.setForeground(new java.awt.Color(240, 240, 240)); txtStaffid.setDisabledTextColor(new java.awt.Color(255, 51, 51)); txtStaffid.setEnabled(false); jLabel2.setText("Password"); jLabel5.setText(" Staff Name"); jLabel3.setText("User Name"); jLabel7.setText(" Staff ID"); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING ) .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
  • 161. HND in Computing & Programming in System Development Java 161 .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING ) .addComponent(txtStaffid, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(txtPassword) .addComponent(txtUserName) .addComponent(txtStaffName, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(150, Short.MAX_VALUE)) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(28, 28, 28) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILIN G) .addComponent(txtStaffid, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel7)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN E) .addComponent(jLabel5) .addComponent(txtStaffName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN E) .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
  • 162. HND in Computing & Programming in System Development Java 162 .addComponent(txtUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN E) .addComponent(jLabel2) .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(29, Short.MAX_VALUE)) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup() .addComponent(btnUpdate) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnClear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(btnOk, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(24, 24, 24)))) ); layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnClear, btnUpdate}); layout.setVerticalGroup(
  • 163. HND in Computing & Programming in System Development Java 163 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(36, 36, 36) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, 24, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnOk, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnClear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap()) ); pack(); }// </editor-fold> public void getDetails(int Staffid)throws ClassNotFoundException, SQLException { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String connectionUrl = "jdbc:sqlserver://Anushka- PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;"; Connection connection = DriverManager.getConnection(connectionUrl); System.out.println("Successfuly Connected"); String SQL1 = " SELECT * FROM Staff WHERE Staff_ID=" + "'"+Staffid+"'"; Statement stmt2 = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet rs = stmt2.executeQuery(SQL1); rs.first(); txtStaffid.setText(rs.getString(1)); txtStaffName.setText(rs.getString(2)); txtUserName.setText(rs.getString(3)); txtPassword.setText(rs.getString(4));
  • 164. HND in Computing & Programming in System Development Java 164 } public void update_user() throws ClassNotFoundException, SQLException { // connection to the sql server Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String connectionUrl = "jdbc:sqlserver://Anushka- PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;"; Connection connection = DriverManager.getConnection(connectionUrl); // System.out.println("Successfuly Connected to MS SQL 2005 using windows authentication 1"); // command Statement statement = connection.createStatement(); String sql = "UPDATE Staff SET Staff_name='"+ txtStaffName.getText()+ "'" + ","+ "User_name='"+ txtUserName.getText() + "'" + ","+ "Password='" + txtPassword.getText() + "'" + "WHERE Staff_ID='" + txtStaffid.getText() +"'"; int updateCount = statement.executeUpdate(sql); } public void clearAll(){ txtStaffName.setText(""); txtUserName.setText(""); txtPassword.setText(""); } private void btnClearActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: this.clearAll(); }
  • 165. HND in Computing & Programming in System Development Java 165 private void btnOkActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: this.setVisible(false); } private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: try{ int respond =JOptionPane.showConfirmDialog(this, "Do You Want To Update This Record?","Confirm Update",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION _MESSAGE); if (respond==0){ this.update_user(); } else{ } }//end try catch (ClassNotFoundException e){ System.out.println(e); } catch (SQLException e){ JOptionPane.showMessageDialog(null, e, "ERROR", JOptionPane.ERROR_MESSAGE); } } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try {
  • 166. HND in Computing & Programming in System Development Java 166 for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(EditMyUserAcc.class.getName()).log(java.util.logging.Leve l.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(EditMyUserAcc.class.getName()).log(java.util.logging.Leve l.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(EditMyUserAcc.class.getName()).log(java.util.logging.Leve l.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(EditMyUserAcc.class.getName()).log(java.util.logging.Leve l.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { //new EditMyUserAcc().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton btnClear; private javax.swing.JButton btnOk; private javax.swing.JButton btnUpdate; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3;
  • 167. HND in Computing & Programming in System Development Java 167 private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel7; private javax.swing.JPanel jPanel1; private javax.swing.JTextField txtPassword; private javax.swing.JTextField txtStaffName; private javax.swing.JTextField txtStaffid; private javax.swing.JTextField txtUserName; // End of variables declaration } Payment Details Form Figure 44 Code : /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package heinstitute; import java.sql.Connection; import
  • 168. HND in Computing & Programming in System Development Java 168 java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.swing.JOptionPane; import javax.swing.table.DefaultTableModel; /** * * @author Anushka */ public class frmPaymentDetails extends javax.swing.JFrame { /** * Creates new form frmPaymentDetails */ DefaultTableModel model; public frmPaymentDetails(boolean z) { initComponents(); model = new DefaultTableModel(); model.addColumn("Payment_code"); model.addColumn("Payment_Method") ; model.addColumn("Credit_Card_Num ber"); model.addColumn("Bank_Name"); model.addColumn("Total_Amount"); model.addColumn("Course_ID"); model.addColumn("Student_ID"); tblPayments.setModel(model); this.setUserLimitations(z); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked")
  • 169. HND in Computing & Programming in System Development Java 169 // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane(); tblPayments = new javax.swing.JTable(); btnAddPay = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); txtPaycode = new javax.swing.JTextField(); btnDelete = new javax.swing.JButton(); jButton1 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); tblPayments.setAutoCreateRowSorter(true); tblPayments.setModel(new javax.swing.table.DefaultTableModel( new Object [][] { {null, null, null, null, null, null, null}, {null, null, null, null, null, null, null}, {null, null, null, null, null, null, null}, {null, null, null, null, null, null, null}, {null, null, null, null, null, null, null}, {null, null, null, null, null, null, null}, {null, null, null, null, null, null, null}, {null, null, null, null, null, null, null}, {null, null, null, null, null, null, null}, {null, null, null, null, null, null, null}, {null, null, null, null, null, null, null}, {null, null, null, null, null, null, null}, {null, null, null, null, null, null, null}, {null, null, null, null, null, null, null}, {null, null, null, null, null, null, null}, {null, null, null, null, null, null, null} }, new String [] { "Title1", "Title2", "Title3", "Title4", "Title5", "Title6", "Title7" } )); jScrollPane1.setViewportView(tblPayments);
  • 170. HND in Computing & Programming in System Development Java 170 btnAddPay.setBackground(new java.awt.Color(0, 0, 0)); btnAddPay.setForeground(new java.awt.Color(255, 255, 255)); btnAddPay.setText("Receive Payment"); btnAddPay.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnAddPayActionPerformed(evt); } }); jLabel1.setText("Payment Code :"); txtPaycode.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { txtPaycodeActionPerformed(evt); } }); btnDelete.setBackground(new java.awt.Color(0, 0, 0)); btnDelete.setForeground(new java.awt.Color(255, 255, 255)); btnDelete.setText("DeletePayment Detail"); btnDelete.setHorizontalTextPosition(javax.swing.SwingConstant s.RIGHT); btnDelete.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnDeleteActionPerformed(evt); } }); jButton1.setBackground(new java.awt.Color(255, 153, 0)); jButton1.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N jButton1.setText("OK"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } });
  • 171. HND in Computing & Programming in System Development Java 171 javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 824, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addComponent(btnAddPay) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGap(0, 0, Short.MAX_VALUE) .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(txtPaycode, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap(18, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(txtPaycode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel1)) .addGap(18, 18, 18) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(26, 26, 26) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  • 172. HND in Computing & Programming in System Development Java 172 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnAddPay, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(21, 21, 21)) ); pack(); }// </editor-fold> public void setUserLimitations(boolean prm){ btnDelete.setEnabled(prm); } public void findPaymentDetails() throws ClassNotFoundException, SQLException{ // connect to the sql server Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String connectionUrl = "jdbc:sqlserver://Anushka- PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;"; Connection connection = DriverManager.getConnection(connectionUrl); System.out.println("Successfuly Connected "); String SQL1 = " SELECT * FROM Student_Details WHERE Student_ID LIKE" + "'%"+ txtPaycode.getText() +"%'"; Statement stmt2 = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet rls = stmt2.executeQuery(SQL1); this.refreshTable(); while(rls.next()) { model.addRow(new Object[]{
  • 173. HND in Computing & Programming in System Development Java 173 rls.getString(1), rls.getString(2), rls.getString(3), rls.getString(4), } ); } } //--------refreshthe table------- ---- public void refreshTable(){ while (model.getRowCount() > 0) { model.removeRow(0); } } // Delete payment details------------------- public void deletePaymentDetail(String selRow) throws ClassNotFoundException, SQLException { // connect to the sql server Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String connectionUrl = "jdbc:sqlserver://Anushka- PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;"; Connection connection = DriverManager.getConnection(connectionUrl); System.out.println("Successfuly Connected "); Statement statement = connection.createStatement(); statement.executeUpdate("DELETE FROM Payment_Details WHERE Payment_code='" + selRow + "'"); } private void btnAddPayActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: Addpay addpa = new Addpay ();
  • 174. HND in Computing & Programming in System Development Java 174 addpa.setVisible(true); addpa.setResizable(false); addpa.setLocationRelativeTo(null); } private void txtPaycodeActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: try{ if (txtPaycode.getText().equals("")){ //txtStudentID empty JOptionPane.showMessageDialog(null, "The Payment Code Field Is Empty,Enter Payment Code And Search", "EMPTY FIELD", JOptionPane.ERROR_MESSAGE); } else{//txtxCake not empty this.findPaymentDetails(); if(model.getRowCount()==0){ JOptionPane.showMessageDialog(this, "No Data Found", "NO DATA", JOptionPane.ERROR_MESSAGE); } } }//end try catch (ClassNotFoundException e){ System.out.println(e); } catch (SQLException e){ JOptionPane.showMessageDialog(null, e, "ERROR IN CONNECTION", JOptionPane.ERROR_MESSAGE); } } private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: try{ if (tblPayments.getSelectedRow() < 0 || tblPayments.getSelectedColumn() < 0) {
  • 175. HND in Computing & Programming in System Development Java 175 JOptionPane.showMessageDialog(this, "Select a Record From Table To Delete","NO RECORD SELECTED",JOptionPane.WARNING_MESSAGE); } else { int respond =JOptionPane.showConfirmDialog(this, "Are You Sure You Want To Delete This?","Confirm Deletion",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTIO N_MESSAGE); if (respond==0){ String selectedRec = (String)tblPayments.getValueAt(tblPayments.getSelectedRow(), 0); this.deletePaymentDetail(selectedRec); // deleting a payment from Db this.refreshTable(); this.findPaymentDetails(); } else{ } } }//end try catch (ClassNotFoundException e){ System.out.println(e); } catch (SQLException e){ JOptionPane.showMessageDialog(null, e, "ERROR", JOptionPane.ERROR_MESSAGE); } } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: this.setVisible(false); } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  • 176. HND in Computing & Programming in System Development Java 176 * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(frmPaymentDetails.class.getName()).log(java.util.logging.L evel.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(frmPaymentDetails.class.getName()).log(java.util.logging.L evel.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(frmPaymentDetails.class.getName()).log(java.util.logging.L evel.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(frmPaymentDetails.class.getName()).log(java.util.logging.L evel.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { // new frmPaymentDetails().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton btnAddPay; private javax.swing.JButton btnDelete;
  • 177. HND in Computing & Programming in System Development Java 177 private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTable tblPayments; private javax.swing.JTextField txtPaycode; // End of variables declaration } Receive payment detail form. Figure 45 Code : /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package heinstitute; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import
  • 178. HND in Computing & Programming in System Development Java 178 javax.swing.JOptionPane; import javax.swing.table.DefaultTableMod el; /** * * @author Anushka */ public class Addpay extends javax.swing.JFrame { /** * Creates new form Add pay */ DefaultTableModel model; public Addpay() { initComponents(); try{ this.generateID(); }//end try catch (ClassNotFoundException e){ System.out.println(e); } catch (SQLException e){ System.out.println(e); } } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jPanel2 = new javax.swing.JPanel(); txtcridetNO = new
  • 179. HND in Computing & Programming in System Development Java 179 javax.swing.JTextField(); jLabel6 = new javax.swing.JLabel(); txtCiD = new javax.swing.JPasswordField(); jLabel4 = new javax.swing.JLabel(); txtSiD = new javax.swing.JPasswordField(); jLabel3 = new javax.swing.JLabel(); txtPaycode = new javax.swing.JTextField(); jLabel2 = new javax.swing.JLabel(); jComboBox1 = new javax.swing.JComboBox(); jLabel8 = new javax.swing.JLabel(); jPanel1 = new javax.swing.JPanel(); rbCashe = new javax.swing.JRadioButton(); rdbCcard = new javax.swing.JRadioButton(); jLabel5 = new javax.swing.JLabel(); jLabel7 = new javax.swing.JLabel(); btnAdd = new javax.swing.JButton(); txtTot = new javax.swing.JTextField(); btnClear = new javax.swing.JButton(); btnOk = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Recive Payment", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12), new java.awt.Color(255, 102, 0))); // NOI18N jLabel6.setText("Credit Card Number"); jLabel4.setText("Course ID"); jLabel3.setText("Total Ammount"); txtPaycode.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N txtPaycode.setForeground(new java.awt.Color(240, 240, 240)); txtPaycode.setDisabledTextColor(new java.awt.Color(255, 0, 0)); txtPaycode.setEnabled(false); jLabel2.setText("Stuent ID");
  • 180. HND in Computing & Programming in System Development Java 180 jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select One......", "Sampath Bank", "Bank of ceylone", "NSB Bank", "Hattern National Bank", "Comercial Bank" })); jComboBox1.setName("com"); // NOI18N jLabel8.setText("Bank Name"); jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Payment Method", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12))); // NOI18N rbCashe.setSelected(true); rbCashe.setText("Cashe"); rbCashe.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { rbCasheActionPerformed(evt); } }); rdbCcard.setText("Credit Card"); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addContainerGap() .addComponent(rdbCcard) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(rbCashe) .addContainerGap()) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.Grou
  • 181. HND in Computing & Programming in System Development Java 181 pLayout.Alignment.BASELINE) .addComponent(rbCashe) .addComponent(rdbCcard)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); jLabel5.setText("Payment Method"); jLabel7.setText("Payment Code"); btnAdd.setText("Save"); btnAdd.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnAddActionPerformed(evt); } }); btnClear.setText("CLEAR"); btnClear.setPreferredSize(new java.awt.Dimension(70, 33)); btnClear.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnClearActionPerformed(evt); } }); btnOk.setText("Ok"); btnOk.setHorizontalTextPosition(javax.swing.SwingConstants. CENTER); btnOk.setPreferredSize(new java.awt.Dimension(70, 33)); btnOk.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnOkActionPerformed(evt); } }); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
  • 182. HND in Computing & Programming in System Development Java 182 jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING ) .addGroup(jPanel2Layout.createSequentialGroup() .addComponent(jLabel8) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel2Layout.createSequentialGroup() .addComponent(btnClear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(btnOk, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel2Layout.createSequentialGroup() .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING ) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILIN G, false) .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel5)) .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING ) .addComponent(txtcridetNO, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(txtPaycode, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
  • 183. HND in Computing & Programming in System Development Java 183 .addGroup(jPanel2Layout.createSequentialGroup() .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING ) .addComponent(jLabel3) .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING ) .addComponent(txtSiD, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(txtCiD, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(txtTot, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addContainerGap(66, Short.MAX_VALUE)) ); jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel2, jLabel3, jLabel4, jLabel5, jLabel6, jLabel7, jLabel8}); jPanel2Layout.setVerticalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addGap(31, 31, 31) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN E) .addComponent(txtPaycode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel7)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING ) .addGroup(jPanel2Layout.createSequentialGroup() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  • 184. HND in Computing & Programming in System Development Java 184 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN E) .addComponent(txtcridetNO, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))) .addComponent(jLabel5)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN E) .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN E) .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(txtTot, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN E) .addComponent(jLabel2) .addComponent(txtSiD, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN E) .addComponent(jLabel4) .addComponent(txtCiD, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
  • 185. HND in Computing & Programming in System Development Java 185 .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN E) .addComponent(btnOk, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnClear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(24, 24, 24)) ); jPanel1.getAccessibleContext().setAccessibleName("Payment method"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI NG) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(27, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(24, Short.MAX_VALUE) .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) ); pack(); }// </editor-fold> public void generateID()throws ClassNotFoundException, SQLException{ Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String connectionUrl = "jdbc:sqlserver://Anushka- PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;"; Connection connection = DriverManager.getConnection(connectionUrl); System.out.println("The database is Connected "); // connection ok
  • 186. HND in Computing & Programming in System Development Java 186 String sql="SELECT * FROM Payment_Details ORDER BY Payment_code ASC;"; Statement stmt2 = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet rs = stmt2.executeQuery(sql); int id; if(!rs.next()){ id=1; txtPaycode.setText(Integer.toString(id)); } else{ rs.last() ; id=Integer.valueOf(rs.getString(1)); txtPaycode.setText(Integer.toString(id+1)); } }// end of generateID public void clearAll(){ txtPaycode.setText(""); txtTot.setText(""); txtSiD.setText(""); txtCiD.setText(""); } public void add_payment() throws ClassNotFoundException, SQLException{ // connection to the sql server Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String connectionUrl = "jdbc:sqlserver://Anushka- PCSQLEXPRESS;database=He_Institute;IntegratedSecurity=true;"; Connection connection = DriverManager.getConnection(connectionUrl); System.out.println("Successfuly Connected to MS SQL 2005 using windows authentication 1"); // connection ok Statement statement = connection.createStatement(); if(txtTot.getText().equals("")){ JOptionPane.showMessageDialog(null, "The Total Amount Field Is Empty,Enter Total Amount", "EMPTY FIELD", JOptionPane.ERROR_MESSAGE); }
  • 187. HND in Computing & Programming in System Development Java 187 else { if(rb Cas he.i sSel ecte d()= =tru e){ statement.executeUpdate("INSERT INTO Payment_Details VALUES " +"( '" + txtPaycode.getText() + "'" + "," + "'" + this.getPayMethod() + "'" + ","+ "'" + txtTot.getText() + "'" + "," + "'" + txtCiD.getText() + "'" + "," + "'" + txtSiD.getText() + "'" + ")" +" "); JOptionPane.showMessageDialog(null, "Record are saved in the database", "INSERT SUCCESSFUL", JOptionPane.INFORMATION_MESSAGE); } else if(rdbCcard.isSelected()==true){ statement.executeUpdate("INSERT INTO Payment_Details VALUES " +"( '" + txtPaycode.getText() + "'" + "," + "'" + this.getPayMethod() + "'" + ","+ "'" + txtTot.getText() + "'" + "," + "'" + txtCiD.getText() + "'" + "," + "'" + txtSiD.getText() + "'" + ","+ "'" + txtcridetNO.getText() + "'" + ","+ "'" + jComboBox1.getToolTipText()+ "'" + " )" +" "); } else{ } } } public boolean getPayMethod(){ if(rbCashe.isSelected()==true) { return false;
  • 188. HND in Computing & Programming in System Development Java 188 } else if(rdbCcard.isSelected()==true){ return true; } else{ return false; } } private void btnAddActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: try{ this.add_payment();//add new Payment to database this.clearAll(); this.generateID(); }//end try catch (ClassNotFoundException e){ System.out.println(e); } catch (SQLException e){ JOptionPane.showMessageDialog(this, e, "ERROR", JOptionPane.ERROR_MESSAGE); } } private void btnOkActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: this.setVisible(false); } private void btnClearActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: this.clearAll(); } private void rbCasheActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: }
  • 189. HND in Computing & Programming in System Development Java 189 /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(Addpay.class.getName()).log(java.util.logging.Level.SEVE RE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Addpay.class.getName()).log(java.util.logging.Level.SEVE RE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Addpay.class.getName()).log(java.util.logging.Level.SEVE RE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Addpay.class.getName()).log(java.util.logging.Level.SEVE RE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Addpay().setVisible(true);
  • 190. HND in Computing & Programming in System Development Java 190 } }); } // Variables declaration - do not modify private javax.swing.JButton btnAdd; private javax.swing.JButton btnClear; private javax.swing.JButton btnOk; private javax.swing.JComboBox jComboBox1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel7; private javax.swing.JLabel jLabel8; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JRadioButton rbCashe; private javax.swing.JRadioButton rdbCcard; private javax.swing.JPasswordField txtCiD; private javax.swing.JTextField txtPaycode; private javax.swing.JPasswordField txtSiD; private javax.swing.JTextField txtTot; private javax.swing.JTextField txtcridetNO; // End of variables declaration }
  • 191. HND in Computing & Programming in System Development Java 191 4.2. Provide evidence and solutions for error handling during software implementation. In Software programming, a development error is one that can be prevented. Such an error can occur in syntax or logic. • Syntax errors, which are typographical mistakes or improper use of special characters, are handled by rigorous proofreading. • Logic errors, also called bugs, occur when executed code does not produce the expected or desired result. Logic errors are best handled by meticulous program debugging. This can be an ongoing process that involves, in addition to the traditional debugging routine, beta testing prior to official release and customer feedback after official release. There are several techniques that I used to handle above mentioned errors. Those are given below. Use “Try catch” method Aa try statementa is used to catcha exceptionsa thata mighta be throwna as programa executesa. Should use a try statement wherevera use a statement that might throw an exception that way, program won‟t crash if the exception occurs. This is the code of this method. I used this method in my all application forms. try{ this.add_payment();//add new Payment to database this.clearAll(); this.generateID(); //end try} catch (ClassNotFoundException e){ System.out.println(e); } catch (SQLException e){ JOptionPane.showMessageDialog(this,e, "ERROR", JOptionPane.ERROR_MESSAGE); }
  • 192. HND in Computing & Programming in System Development Java 192 Validate text fields This is the second method that I used to handle errors. If any user will press a button without filling the required data it will show the error message As the following picture. Figure 46 Enable/ Disable buttons. Figure 47 private voidbtnAddActionPerformed(java.awt.event.ActionEventevt) { if(txtstudName.getText().equals("")){ JOptionPane.showMessageDialog(null,"The Student name FieldIsEmpty,Enterthe Studentname Name", "EMPTY FIELD", JOptionPane.ERROR_MESSAGE); } }elseif { ……..
  • 193. HND in Computing & Programming in System Development Java 193 This is the third method that I used to handle the errors. In here I disable some buttons when window appear.as an example in student details interface the delete student detail button is disabled because this User haven‟t permission to use it. public void setUserLimitations(boolean prm){ btnDelete.setEnabled(prm); }
  • 194. HND in Computing & Programming in System Development Java 194 Task 5 5.1. Carry out complete system testing and provide user guide. System testing and user guide must include suitable screen shots. Testyour complete project with suitable data. First I‟m tried to login to the system by using default User Name and password ( user Name=Admin password=admin) Figure 48 Thenit continuedto themain Interface.
  • 195. HND in Computing & Programming in System Development Java 195 Then I‟m tried to create personal account to an user , to create new account first click on the Configuration in the menu bar and expand it. After that click on the Create new User Figure 49 So it continued to an interface as the following picture.(I early day created some user accounts so the User ID is Start With the number 4) I filled the blanks that what they need to know about the new user in above form and I clicked on Save button. Blank s
  • 196. HND in Computing & Programming in System Development Java 196 Figure 50 To return to the Main interface I click on the ok button in the Add user details form. As the final of Adding new User I tried to login to the system by using the new Account If filled data is correct, it will show a message as the following picture. then I clicked on the ok button
  • 197. HND in Computing & Programming in System Development Java 197 (user Name = Rajapaksha, Password = 11 ). Figure 51 Then it continued to the Main interface. to View the Course details Interface I clicked on the Course details button ( I used an Administrator Account to use the all the Figure 52 functions ).
  • 198. HND in Computing & Programming in System Development Java 198 As my need it continued to the Course details Interface Figure 53 Figure 54 To Add a new Course detail i clicked on Add New Course details button then it showed the Add new Course interface as the following picture. Blanks
  • 199. HND in Computing & Programming in System Development Java 199 So I Filled the blanks and I clicked on save button course interface then it return to the Course details interface…. Then it showed the following message after that I clicked on ok button. Finally return to the Course details interface I clicked on ok button in the Add new
  • 200. HND in Computing & Programming in System Development Java 200 figure 55 To Edit a Course detail, i entered a course id on the text box and pressed enter button.as the final I was selected that Course id from the table and clicked on Edit Course details button . Edit Course details button Course details Table Text Box
  • 201. HND in Computing & Programming in System Development Java 201 Then it showed the Edit Course interface as the following picture…. Figure 55 I done some modifications to the information.(as a modification I changed the course fee to $200) Figure 56
  • 202. HND in Computing & Programming in System Development Java 202 As the final I clicked on Update button .after that it showed the following message then I clicked on yes button. Figure 57 For return to the Course details Interface I clicked on Ok button in the Edit course details interface. Figure 58
  • 203. HND in Computing & Programming in System Development Java 203 Then it was returned to Course Details Interface.to return from this interface to the Main interface I clicked on the ok button …. Figure 59 Then it was returned to the Main interface. To view Batch details interface I clicked on Figure 60 the Batch details Button.
  • 204. HND in Computing & Programming in System Development Java 204 Then it was continued to the batch details Interface. Figure 61 So first I tried to add a batch detail.to view Add batch interface I was clicked on Add batch button. Then it was showed the Add batch Interface. Figure 62 Blanks
  • 205. HND in Computing & Programming in System Development Java 205 So I was filled the blanks and I clicked on ok button Figure 63 It was showed the following message. then i clicked on ok button in the message Figure 64
  • 206. HND in Computing & Programming in System Development Java 206 For return to the Batch details I clicked on the ok button in the Add New Batch Interface. Figure 65 Then it returned to the Batch details interface. To edit a batch detail I was Entered a batch id on the text box and I pressed enter button then the detail was showed on the table finally I was selected that record and clicked on the Edit batch detail button. Figure 66
  • 207. HND in Computing & Programming in System Development Java 207 Then it was viewed the Edit Batch detail Form with the Batch information…. Figure 67 In the above interface I had done a modification to the information (as a modification I was changed the Number of students up to 70 ) then I clicked on Update button. Figure 68
  • 208. HND in Computing & Programming in System Development Java 208 Finally it was showed a message as below. Then I clicked on yes button. Figure 69 Then the record was updated. To return from the Edit batch details interface to the Batch details interface I was clicked on the ok button. Figure 70
  • 209. HND in Computing & Programming in System Development Java 209 Finally it returned to the Batch details Interface .to return to the Main interface I was clicked on the Ok button under the Batch details interface… Figure 71 To view the Student details I was clicked on the Student details button. Figure 72 After that it was returned …
  • 210. HND in Computing & Programming in System Development Java 210 Then it continued to Student details Interface. Figure 73 Figure 74 To Add a new student detail i clicked on Add New student details button then it showed the Add new student interface as the following picture. Blanks
  • 211. HND in Computing & Programming in System Development Java 211 So I Filled that Blanks and I clicked on Save button . Figure 75 To return to the student details interface I clicked on ok button in the Add new student interface .then it was returned… To view edit student detail interface first I was entered a user ID on the text box and pressed the enter button.as the second step I was selected that user id from the table and clicked on Edit Student Details button. To confirm it showed a message as below then i clicked on OK button.
  • 212. HND in Computing & Programming in System Development Java 212 Figure 76 Figure 77 Then it was viewed as below …
  • 213. HND in Computing & Programming in System Development Java 213 As a modification I was changed telephone number to 0774455754… then I clicked on Update button Figure 78 Finally it showed a message as below ,then I was clicked on Yes button. Figure 79
  • 214. HND in Computing & Programming in System Development Java 214 To return to the Student details interface I was clicked on ok button in the Edit student details interface. Figure 80 Then it returned to the Student detail Interface.to return from this to the Min interface I was clicked on ok button. Figure 81
  • 215. HND in Computing & Programming in System Development Java 215 Then it was return to the Main interface.to View the payment detail interface I was clicked on the payment details button in the Main interface.. Figure 82 Then it was continued to the Payment details form. Figure 83
  • 216. HND in Computing & Programming in System Development Java 216 To receive a payment I was clicked on Receive payment button. Then It was showed the following form Figure 84 I was filled that form and I clicked on Save button then it was showed the following message then I was clicked on the ok button . Figure 85 According to customer requirements the above interfaces are I had implemented to the new system.so one by one i was tested in this software and there is no any errors.
  • 217. HND in Computing & Programming in System Development Java 217 5.2. Producesuitable screenshots and solutions for error handling during testing. When creating new user account When creating a new account customer must enter the valid data to the system. If Customer didn‟t enter the correct data there will some Error messages. If User click Save button without fills the data, it will show a message as the following picture. Figure 86
  • 218. HND in Computing & Programming in System Development Java 218 Figure 87 When Login to system When log in to the system the user must enter their correct user name and password. Otherwise there will be an error message as following picture. For this User should enter the correct user name and password. Figure 88
  • 219. HND in Computing & Programming in System Development Java 219 When Updating a Student detail… Figure 89 When updating a student detail the user must confirm it. Else it wont to be update.
  • 220. HND in Computing & Programming in System Development Java 220 5.3. Include feedbacks on provided java solution (surveys, questionnaire, analyze feedback and present results), evaluate all and provide summery report Questionnaire for Administrator. 1) As an administrator what do you thinking about the new Software? Excellent Very good Good Poor 2) Is this system easy to Handel? Yes No May be No idea 3) Are you satisfied with the new System? Satisfied Fairly Satisfied Dissatisfied 4) What accept were you mostly satisfied? When entering a new student When make a payment When adding a course detail When adding a batch detail No idea 5) What you accept were you mostly dissatisfied? When entering a new student When make a payment When adding a course detail When deleting the student details No idea
  • 221. HND in Computing & Programming in System Development Java 221 6) Do you have any comments ? This software is easy to understand any one when it operate first time. There is a little issue when back to previous interface by using close button. But in this software it approaches to a new method to prevent the above problem by giving the close command to ok button. That is good and deviation from other softwares. When we‟re doing payments, this software gives us better help to increase accuracy of that step. This software is easy to handle and anyone can get an idea by operate this once. because of that, this software is really successful and good solution to the given problem. Questionnaire For Staff Members 2) As a Staff member what do you thinking about the new Software? Excellent Very good Good Poor 7) Is this system easy to Handel? Yes No May be No idea 8) Are you satisfied with the new System? Satisfied Fairly Satisfied Dissatisfied 9) What accept were you mostly satisfied? When entering a new student When make a payment When adding a course detail When adding a batch detail No idea
  • 222. HND in Computing & Programming in System Development Java 222 10)What you accept were you mostly dissatisfied? When entering a new student When make a payment When adding a course detail When adding a batch detail When deleting students details No idea 11)Do you have any comments ? This software gives some benefits to staff members as well as administrators. Because staff members also allowed to keep user accounts to their own. This benefit is useful to decrease errors when operate the software by difference users. Summary Report of the User feedback How many customers are satisfied with about the system : 05 How many customers are dis-satisfied about the system : 03 How many customers said the system is difficult to handle : 02 How many customers said the system is easy to handle : 03 How many customers said „No idea‟ about the system : 03 *According to above feedback many Users are satisfied with the system* Required Improvements So according above answers to the above Questionnaires, the followings are the some required improvements to the software. • IF the User wants to delete a specific record about the student from the database then it can be achieve without using many interfaces. • The program won‟t to be fully terminate by Clicking on the close button. • A profile picture can be added to the user profile.
  • 223. HND in Computing & Programming in System Development Java 223 Task 06 6.1. Producea publishable working copya compiled version of the completed assignment together with software installation notes, recommendations and future improvements. The installation note should include the system requirements Software Installation System requirements for the software installation Hardware requirements PIV or Higher Computer 1.6GHz or faster processor 40GB Hard disk Minimum 512 MB RAM Free Disk Space 11 MB Software Requirements Any kind of an Operating system form this : Windows XP/ Windows 8 (Desktop) / Windows 7/ Windows Vista SP2/ Mac OS/ Oracle Linux 5.5+/ Ubuntu Linux* 10.04 and above. Java Platform, Standard Edition Development Kit (Java SE 6 Update 27). Installation Note • Double click on the He institute Folder Figure 90 • Then it will show an icon and a folder. click on the folder. Figure 91
  • 224. HND in Computing & Programming in System Development Java 224 An user can see the following setup file then double click on it Figure 92 • After that the installation will start as the following picture. Then click Next button to go to the next step. Figure 93
  • 225. HND in Computing & Programming in System Development Java 225 In here ,Click on next button… Figure 94
  • 226. HND in Computing & Programming in System Development Java 226 Select the installation Path and click on next button Figure 95 In here Click on Start button Figure 96
  • 227. HND in Computing & Programming in System Development Java 227 • After that the software will be installing. When it complete can see the below window. Then simply click Next button and then click finish. Figure 97
  • 228. HND in Computing & Programming in System Development Java 228 Figure 98 • After completing those things the user must restore the Database to the system. then the user must follow the following steps. 1. Double click on the He institute folder. Figure 99 Finally click on the Exit button…
  • 229. HND in Computing & Programming in System Development Java 229 2. Then Copy the database backup folder to the system. Figure 100 3. After that double click on the SQL server management studio Express… Figure 101
  • 230. HND in Computing & Programming in System Development Java 230 4. Then it will continue to the following interface. Then click on the Connect button. Figure 102
  • 231. HND in Computing & Programming in System Development Java 231 5. So it will continue ... then right click on the database tab and click on Restore database as the following picture. Figure 103
  • 232. HND in Computing & Programming in System Development Java 232 6. Then it will show an interface as below.. then type the name of a new database as mention in the following picture and specify the source and location of backup sets to restore. Figure 104 Database Name Clickon here
  • 233. HND in Computing & Programming in System Development Java 233 7. When click on as it mention in the above picture then it will show a interface as below. Then click on Add button. Figure 105
  • 234. HND in Computing & Programming in System Development Java 234 8. Then give the path as below. And click ok Figure 106
  • 235. HND in Computing & Programming in System Development Java 235 9. After that you can see the following specify backup interface as below. Click on ok button. Figure 107
  • 236. HND in Computing & Programming in System Development Java 236 10.Finally it will return to the following Interface then Select the backup sets to restore as below picture and click on ok button. Figure 108 Figure 109 To start the software click on the icon as the following picture.
  • 237. HND in Computing & Programming in System Development Java 237 Reference Sheet http://www.webopedia.com/TERM/J/Java.html https://www.bsi.bund.de/EN/Topics/OtherTopics/Java/javaprinciples.html http://www.cs.armstrong.edu/liang/JavaCharacteristics.pdf http://www.makeuseof.com/tag/java- virtual-machine-work-makeuseof-explains/ http://searchcrm.techtarget.com/definition/entity- relationship-diagram http://msdn.microsoft.com/en-us/library/vstudio/dd409437.aspx http://www.visual-paradigm.com/VPGallery/diagrams/UseCase.html http://www.techterms.com/definition/user_interface