0% found this document useful (0 votes)
39 views10 pages

Database Connectivity - Netbeans

Uploaded by

devmith2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views10 pages

Database Connectivity - Netbeans

Uploaded by

devmith2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Object Oriented Programming – Database Connectivity

Performing CRUD operations on a MySQL database using JDBC

Step 1: Create Database and the Table

• Open MySQL Workbench.


• Connect to your MySQL Server instance.
• Create a new database called ‘company’.
• Create a table called ‘Employee’ within the database.

EmpId INTEGER
Name VARCHAR(100)
ContactNo VARCHAR(10)

Step 2: Configure MySQL Connector/J:

• Download the MySQL Connector/J JAR file from the MySQL website. https://dev.mysql.com/downloads/connector/j/
• Create a project in NetBeans IDE.
• Right click on the project’s libraries, go to Add JAR/Folder.
• Add MySQL Connector/J JAR file to the libraries.
• Expand Databases. Right click on Driver. Select New Driver. Then add the MySQL Connector/J JAR file to the drivers using Add button.
• Then add the database connection to Services in IDE.
• Go to the Services tab, then right click on the Databases. Select New Connection.
• Select (MySQL Connector/J driver).
• Click Next.
• Fill Password field. Click Test Connection.
• If the database is connected, you will get the following result.

• Click Finish button.


• You will see the database connection and the database in the explorer as below.
Step 2: Establish Connection

• Create a package called controller.


• Inside of the package, create a Java main class.
java.sql.DriverManager;
• This import statement imports the DriverManager class from the java.sql package.
• Purpose: The DriverManager class manages a list of database drivers. It is responsible for establishing a connection to a database using the
appropriate driver.
import java.sql.SQLException;
• This import statement imports the SQLException class from the java.sql package.
• Purpose: SQLException is an exception class used to handle errors related to database operations. It provides information about database
access errors or other errors related to the database.
import java.sql.Connection;
• This import statement imports the Connection interface from the java.sql package.
• Purpose: The Connection interface represents a connection to a database. It provides methods for creating statements, committing transactions,
and managing the connection's properties.

You might also like