CSL 210 Lab12 Database Connectivity 12122024 095736pm
CSL 210 Lab12 Database Connectivity 12122024 095736pm
Semester BS CE – 03
JDBC
Java Database Connectivity (JDBC) provides a standard library for accessing databases. The JDBC
API contains number of interfaces and classes that are extensively helpful while communicating with
a database.
The java.sql package contains basic & most of the interfaces and classes. You automatically get this
package when you download the J2SE™. You have to import this package whenever you want to
interact with a relational database.
In this handout, we will learn how to connect & communicate with Microsoft Access Database.
Create Database
In start create a database “PasswordDB” using Microsoft Access. Create one table named
“Password”. The schema of the table is shown in the picture.
The following example demonstrates the usage of all above explained steps. In this code example,
we connect with the PasswordDB database, the one we have created earlier, and then execute the
simple SQL SELECT query on Password table, and then process the query results. This example also
demonstrates how we can connect multiple forms with each other.
Main Form:
Double-click on the Change Password Menu—this should take you in the Source code
menuChgPasswordActionPerformed method. Under the comment line, type the following code:
Double-click on the Logout Menu—this should take you in the Source code
menuLogoutActionPerformed method. Under the comment line, type the following code:
c.updatePassword(userName,String.valueOf(newPassword.getPassword()));
}
else
System.out.println("Not matched..\nTry again");
Double-click on the Enter JButton—this should take you in the Source code
btnEnterActionPerformed method. Delete the comment line and type the following code:
dispose();
Note:
We can use the following hard coded Database path but it makes it difficult to run application on
different machines unless the path to the database is modified according the location of the
database file.
String url =
"jdbc:ucanaccess://C:/Users/INTEL/Documents/NetBeansProjects/prjPasswor
d/database/PasswordDB.accdb";
Exercise:
Create an application for the student enrollment system where student is enrolled in the university.
Create proper dataset to store information for a student an apply CRUD oerations over the students
enrillement application. Also have the appropriate designing and login system for admin.