Java Database Connectivity (JDBC), which is a standard Java API for database-independent connectivity between the Java programming language and a wide range of databases.
It has four major parts:
- Making a connection to a database
- Creating SQL or MySQL statements
- Executing that SQL or MySQL queries in the database
- Viewing and modifying the resulting records
Open a new Java Application –> Right-click on the project –> select Properties. Then from the left menu select libraries.
Select Add JAR/Folder.
select the mysql-connector-java-5.1.6-bin.jar file. Which can be found in the Netbeans folder. For Mac, it is in /Application/Netbeans/Netbeans 6.8/contents/Resources/ide12/modules/ext/mysql-connector-java-5.1.6-bin.jar
Now are able to use the JDBC in your program frequently 🙂
Here, I’ll be talking about connecting to a MySQL database. But it can be used for other cases also.
Selecting a right driver will be another major task.
For ORACLE the driver will be oracle.jdbc.driver.OracleDriver
For DB2 the driver will be COM.ibm.db2.jdbc.net.DB2Driver
For Sybase the driver will becom.sybase.jdbc.SybDriver
Load the driver class using Class.forname(com.sybase.jdbc.SybDriver)
Define the data source for the driver String sourceURL = “jdbc:mysql://URL/database_name”
Create a connection through the DriverManager
Connection databaseConnection = DriverManager.getConnection(sourceURL)
All this should be in a try-catch block.