- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Connecting to a MySQL database with Java
Use below URL to connect MySQL database. The syntax is as follows −
String MySQLURL="jdbc:mysql://localhost:3306/yourDatabaseName?useSSL=false"; String databseUserName="yourUserName"; String databasePassword="yourPassword";
Example
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; public class ConnectToDatabase { public static void main(String[] args) { String MySQLURL = "jdbc:mysql://localhost:3306/web?useSSL=false"; String databseUserName = "root"; String databasePassword = "123456"; Connection con = null; try { con = DriverManager.getConnection(MySQLURL, databseUserName, databasePassword); if (con != null) { System.out.println("Database connection is successful !!!!"); } } catch (Exception e) { e.printStackTrace(); } } }
Output
Database connection is successful !!!!
Here is the snapshot of the output −
- Related Articles
- Connecting to MySQL database from the command line?
- While connecting to one MySQL database, how can I see the list of tables of other MySQL database?
- After connecting to MySQL server how can we select a database from command prompt?
- How to update data in a MySQL database with Java?
- How to insert data into a MySQL database with Java?
- How to delete data in a MySQL database with Java?
- How to disable “Establishing SSL connection without server's identity verification is not recommended” warning when connecting to MySQL database in Java?
- Resolve Unknown database in JDBC error with Java-MySQL?
- Get the last record from a table in MySQL database with Java?
- Create a database in MySQL from Java?
- Java application to insert null value into a MySQL database?
- Create a new database with MySQL Workbench?
- How to connect hibernate with MySQL Database?
- How to work with one database connection object in the entire Java-MySQL application?
- Can we create a database with a numeric name with MySQL?

Advertisements