Java Tutorial 85 - JDBC 2/7 Connecting to a MySQL / MariaDB Database

preview_player
Показать описание
In this tutorial, I will show you how to develop database applications using SQL and Java for MariaDB or MySQL in NetBeans IDE.

1. Loading drivers

// Statement to load a driver:

A driver is a class. For example:

Database JDBC Driver Class

2. Establishing connections

// Establish a connection
 
Database URL Pattern
Access jdbc:odbc:dataSource
MySQL jdbc:mysql://hostname:port#/dbname
MariaDB jdbc:mariadb://localhost:oirt#/dbname
Java DB jdbc:derby://localhost:port#/dbname
 
Examples:
For Access:
("jdbc:odbc:ExampleMDBDataSource" "user1", "pass1");

For MySQL:
("jdbc:mysql://localhost:3306/test", "user1", "pass1");

For MariaDB
("jdbc:mariadb://localhost:3306/test", "user1", "pass1");

For Oracle:

For Java DB:
("jdbc:derby://localhost:1527/test", "user1", "pass1");

For PostgreSQL:
("jdbc:postgresql://localhost:5432/test", "user1", "pass1");

For SQL Server:
("jdbc:microsoft:sqlserver://localhost:1433/test", "user1", "pass1");

3. Creating and executing statements

// Create a statement

// Execute statement (for CREATE, DROP, UPDATE, DELETE or INSERT):

4. Processing ResultSet

Executing statement (for SELECT):
// Select the columns from the employee table

Processing ResultSet (for SELECT):
// Iterate through the result and print the employee data by the column index
}

// Iterate through the result and print the employee data by the column name
}

5. Closing the connection
// Close the connection
Рекомендации по теме
welcome to shbcf.ru