Java Program to Connect To Database.
- Download the Mysql connector from the internet.
- import it in your project.
- Right click on project->Properties->Java build path.
- In libraries select
Add external jars
on the right side menu.
- select and add mysql connector that you downloaded.
- Go to tab
order and export
and select the mysql connector checkbox.
import java.sql.*;
public class ConnectDatabase {
public static void main(String[] args) {
Class.forName("com.mysql.jdbc.Driver");
try
{
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root@123");
System.out.println("Connected");
}
catch (SQLException e)
{
System.out.println("Bad Connection Request");
}
}
}
public class ConnectDatabase {
public static void main(String[] args) {
Class.forName("com.mysql.jdbc.Driver");
try
{
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root@123");
System.out.println("Connected");
}
catch (SQLException e)
{
System.out.println("Bad Connection Request");
}
}
}
Connected.
- com.mysql.jdbc.Driver- driver class for Mysql;
- DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root@123");-
Java Program to Connect To Database.
Reviewed by LanguageExpert
on
August 25, 2017
Rating:
No comments