CONNECT MYSQL DATABASE FROM JAVA

If you have any queries please leave a message here
Your Message
×


  1. Install Windows Operating System

  2. Install XAMPP

    If you want to access MySQL database from another computer, then follow the steps given below -
    1. Open phpmyadmin from a browser

    2. Click on User accounts option and then click on Add user account

    3. Give the User name and Password for new user. Do not change the Host name field. By default the Host name field contain % character which indicates all hosts.

    4. From Global privileges option select the privileges you want to give for new user and then click on Go button.

    You can get all users information from the user table located under the mysql database.

  3. Install Java and Set Environment Variables

  4. Copy mysqlconnector.jar file into the bin folder of JDK (C:\Program Files\Java\jdk1.8.0_112\bin) and then extract here.

    If you do not keep your *.java files into the bin folder of JDK (C:\Program Files\Java\jdk1.8.0_112\bin) then you must need to extract the mysqlconnector.jar file into the location where your *.java files are present.

Driver Class The driver class for the mysql database is com.mysql.jdbc.Driver
Connection URL

The connection URL for the mysql database is jdbc:mysql://localhost:3306/OfficeDatabase

  • jdbc is the API

  • mysql is the database

  • localhost is the server name on which mysql is running, we may also use IP address

  • 3306 is the port number

  • OfficeDatabase is the database name. We may use any database, in such case, you need to replace the OfficeDatabase with your database name

Username The default username for the mysql database is root.
Password Password is given by the user at the time of installing the mysql database. In this example, we are going to use root as the password.



Example
Write a program to create employee table and insert 3 records into it

//CreateEmployeeTable.java
import java.sql.*;
public class CreateEmployeeTable
{
public static void main(String args[])
{
try
{
// MySQL Database Connection
String url="jdbc:mysql://localhost:3306/OfficeDatabase";
String user="root";
String password="root";
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(url,user,password);

// Query Execution
Statement stmt=con.createStatement();
stmt.executeUpdate("create table employee(empid varchar(10),empname varchar(50),salary int,primary key(empid))");
stmt.executeUpdate("insert into employee values('E-1010','Mr. X',25000)");
stmt.executeUpdate("insert into employee values('E-1020','Mr. Y',50000)");
stmt.executeUpdate("insert into employee values('E-1030','Mr. Z',35000)");

// Close The Connection
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}


ABOUT US

QuestionSolves.com is an educational website that helps worldwide students in solving computer education related queries.

Also, different software like Visual Studio, SQL Server, Oracle etc. are available to download in different versions.

Moreover, QuestionSolves.com provides solutions to your questions and assignments also.


MORE TOPIC


Windows Command

UNIX Command

IGNOU Assignment Solution

IGNOU Question Paper Solution

Solutions of Different Questions


WHAT WE DO


Website Devlopment

Training

Home Learning

Provide BCA, MCA Projects

Provide Assignment & Question Paper Solution


CONTACT US


Follow Us