- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to establish connection with JDBC?
To connect with a database, you need to follow the steps given below:
Step1: Register the driver: To develop a basic JDBC application, first of all, you need to register the driver with the DriverManager.
You can register a driver in two ways, one is using registerDriver() method of the DriverManager class and, using forName() method of the class named Class.
The registerDriver() method accepts an object of the Driver class, it registers the specified Driver with the DriverManager.
Driver myDriver = new com.mysql.jdbc.Driver(); DriverManager.registerDriver(myDriver);
The forName() method loads the specified class into the memory and thus it automatically gets registered.
Class.forName("com.mysql.jdbc.Driver");
Step2: Get Connection: Get the Connection object using the getConnection() method. This method accepts the database URL (an address that points to your database), Username and, password as parameters and, returns a connection object.
Invoke this method by passing, the URL of the desired database, user name and, password as parameters to it.
String url = "jdbc:mysql://localhost/"; String user = "root"; String passwd = "password"; Connection conn = DriverManager.getConnection(url, root, passwd);
Example
Following is an example JDBC program which establishes a connection with a database.
import java.sql.*; public class JDBCExample { //JDBC driver name and database URL static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost/"; //Database credentials static final String USER = "root"; static final String PASS = "password"; public static void main(String[] args) { Connection conn = null; try{ //STEP 2: Register JDBC driver Class.forName("com.mysql.jdbc.Driver"); //STEP 3: Open a connection System.out.println("Connecting to database..."); conn = DriverManager.getConnection(DB_URL, USER, PASS); System.out.println("Connection established"); } catch(Exception e) { } System.out.println("Goodbye!"); } }
Output
Connecting to database... Connection established Goodbye!
- Related Articles
- How to establish a connection with the database using the properties file in JDBC?
- Write an example to establish MySQL database connection using PHP script?
- Which PHP function is used to establish MySQL database connection using PHP script?
- How to determine database type (name) for a given JDBC connection?
- How to display the storage engine while implementing JDBC - MySQL CONNECTION query?
- Setting up a JDBC connection to remote SAP HANA system
- What is the MySQL JDBC driver connection string?
- Where we should close a connection in JDBC and MySQL?
- How to use try-with-resources with JDBC?
- How to reconnect to websocket after close connection with HTML?
- How to escape backslashes in MySQL with JDBC?
- Guidelines to establish Defect Life Cycle
- How to process SQL statements with JDBC explain with an example?
- How to handle Null values while working with JDBC?
- How to handle Exceptions while working with JDBC applications?
