Java Tutorials
  • Introduction to Java
    • What is Java?
    • History and Features of Java
    • Java Virtual Machine (JVM) and Bytecode
    • Why Java?
  • Setting up Java Development Environment
    • Installing Java Development Kit (JDK)
    • JDK vs JRE
    • Setting up IDE (Eclipse, IntelliJ, NetBeans) or Text Editor (VS Code, Sublime Text)
  • Basic Java
    • First Java Program : Hello World
    • Variable
    • Data Type
    • Constant
    • Date and Format
    • Operator
    • Condition
    • Looping
    • Function
    • Variadic Function
    • Enums
    • Array
    • Collection
    • Exception and Exception Handling
    • Naming Convention
  • Object Oriented Programming (OOP)
    • Classes and Objects
    • Inheritance and Polymorphism
    • Encapsulation and Abstraction
  • File Handling
    • Reading and Writing Binary File
    • Reading and Writing Text File
    • Serialization and Deserialization
  • Multithreading
    • Creating and Running Threads
    • Synchronization
    • Thread Pools and Executors
  • Collections API
    • Sorting and Comparable
    • Searching and Comparator
  • Java Database Connectivity (JDBC)
    • Introduction and Life Cycle
    • Connection to Database (MySQL)
    • Downloading JDBC Drivers for Various Databases
    • Maven and Gradle JDBC Drivers for Various Databases
    • JDBC URL Formats
    • Statement and PreparedStatement
    • CallableStatement
    • Selecting Data using JDBC
    • Inserting Data using JDBC
    • Updating Data using JDBC
    • Deleting Data using JDBC
    • Invoking Function and Stored Procedure using JDBC
  • Lambda
    • Introduction to Lambda Expressions
    • Functional Interface
    • Filtering, Mapping, Reducing
    • Lambda Expressions in Collections
    • Method References
    • Functional Programming Concepts
    • Stream API
    • Error Handling in Lambda Expressions
    • Optional in Functional Programming
    • Parallel Processing with Lambda
    • Functional Programming Patterns
    • Advanced Topics in Lambda Expressions
    • Best Practices and Design Patterns
    • Real-World Use Cases and Examples
Powered by GitBook
On this page
  1. Java Database Connectivity (JDBC)

Connection to Database (MySQL)

PreviousIntroduction and Life CycleNextDownloading JDBC Drivers for Various Databases

Last updated 1 year ago

Here's how you can install JDBC and use it to connect to a database:

1. Installing JDBC Driver:

To use JDBC, you need to download and install the appropriate JDBC driver for your database. Each database vendor provides its JDBC driver.

  • Example: Installing MySQL JDBC Driver:

    • Download the "latest" MySQL JDBC driver from ⇒ "Connector/J" ⇒ Connector/J 8.0.{xx}, where {xx} is the latest update number ⇒ In "Select Operating System", choose "Platform Independent" ⇒ ZIP Archive (e.g., "mysql-connector-j-8.0.{xx}.zip" ⇒ "No thanks, just start my download".

    • Include the JAR file in your Java project's classpath. Or you can placed it to folder in your java file.

2. Connecting to Databases:

You can establish a connection to a database using the appropriate JDBC URL, username, and password.

Example Code: Connecting to MySQL Database

javaCopy codeimport java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBConnectionExample {
    public static void main(String[] args) {
        String jdbcUrl = "jdbc:mysql://localhost:3306/mydatabase";
        String username = "username";
        String password = "password";

        try {
            Connection connection = DriverManager.getConnection(jdbcUrl, username, password);
            System.out.println("Connected to the database");
            // Perform database operations here
            connection.close(); // Close the connection after use
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

You can compile Java database programs without the JDBC driver.

javac DBConnectionExample.java

But to run the JDBC programs, the JDBC driver's JAR-file must be included in the environment variable CLASSPATH, or in the java's command-line option -cp.

You can set the -cp option for Java runtime as follows:

// For windows
java -cp .;/path/to/mysql-connector-j-8.0.{xx}.jar DBConnectionExample 
// For macOS / Unixes
java -cp .:/path/to/mysql-connector-j-8.0.{xx}.jar DBConnectionExample
http://dev.mysql.com/downloads