Monday, February 3, 2025

To Connect Java program with the MYSQL Database


To connect java application with the MYSQL database, first of all to install MYSQL database in your machine.

To install MYSQL in your machine then follow the below steps:

1) To download MYSQL database on its official website : https://dev.mysql.com/downloads/installer/

2) After that double click on this exe file and run the installer. Choose Full setup option and click next.

3) Execute the entire file and wait to complete to install.

4) Next to check product configuration.

5) In this step choose port number. Default port number is 3306. If it was already acquired by other application like WAMP server or apache server, then change its value like 3307 or 3308.

6)  After that select authentication method. Select recommended method.

7) Choose password and user account as per your choice.

8) Follow next step and finish the installation process.


Watch this video to install MYSQL installation process:  https://youtu.be/QqFWLDSgedg?si=4yvPUXTUQI0DRX2P

 

To connect java application with the MYSQL Database:

1) Create database in MYSQL database using the following SQL Query. 

2) Write JAVA application to connect MYSQL Database and save using “MyDatabase.java” name.

import java.sql.*; 

class MyDatabase{ 

        public static void main(String args[]){ 

            try{

                Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3309/emp?autoReconnect=true&useSSL=false", "root", "root"); 

                //here emp is database name, root is username and password

                if(con!= null){

                    Statement stmt=con.createStatement(); 

                    ResultSet rs=stmt.executeQuery("select * from employee"); 

                    while(rs.next()) 

                        System.out.println(rs.getInt(1) +"  "+ rs.getString(2) +"  "+ rs.getInt(3)); 

                }

                con.close();

        }

        catch(Exception e){

            System.out.println(e);

        }

    

}

 3) Compile this java program using : javac MyDatabase.java



4) After that run this program with the JDBC connector file download from  https://dev.mysql.com/downloads/connector/j/

    To run this file using : java -cp mysql-connector-java-8.0.11.jar;. MyDatabase


All right. Your database is connected successfully. If your like this post then share your friends.


No comments:

Post a Comment

To Connect Java program with the MYSQL Database

To connect java application with the MYSQL database, first of all to install MYSQL database in your machine. To install MYSQL in your mach...