Creating Database and Connecting SQL with Java

Tanishq Rawat
2 min readMar 25, 2021

--

So everyone is familiar with term Database. Our data is stored in database in tabular form. Whether it is bank, Income Tax Department, Schools, Universities everywhere out data is saved in database. And it is very much important for programmers to save data in context to project developement.

How to create database in SQL?

So i will demonstrate the process using command line interface. I will be using mySql shell.

Step 1. type mysqlsh command in command prompt

Step 2. Now switch mode to SQL using command “\sql”.

Step 3. Now login as root user with command “\connect root@localhost:3306" and provide password for the session.

Step 4. Now create database using commandcreate database DATABASE_NAME ; ”

Step 5. Now create user for the database using command create user ‘USER_NAME’@’localhost’ identified by ‘USER_NAME’

Step 6. Now we have to grant permission to the user for using the database so that can be done using command grant all privileges on DATABASE_NAME.* to ‘USER_NAME’ @ ‘localhost’ ;

So now database is created and we should login and to login we should exit.

Step 7. Quit using command \quit

Step 8. Hit command mysqlsh and repeat step 2.

Step 9. Connect to database using command \connect USER_NAME@localhost:3306/DATABASE_NAME and enter the password

Step 10. Now create table using command
create table Candidate
(
candidate_id int primary key auto_increment,
Name char(35) not null unique
);

So now Database and table is created now user can perform all actions using SQL Statements.

Important information
If you are a python programmer, building projects which consist of RDBMS do not use higher version of python. Use python 3.8.1 not higher than that, most of the RDBMS doesn’t have support for python 3.9.0 and higher

Connecting SQL with JAVA (JDBC)

Example for establishing connection of SQL database with java.

import java.sql.*;
class jdbc1psp
{
public static void main(String gg[])
{
try
{
Class.forName(“com.mysql.cj.jdbc.Driver”);
Connection c;
c=DriverManager.getConnection(“jdbc:mysql://localhost:3306/DATABASE_NAME”,”USER_NAME”,”PASSWORD”);
Statement s;
s=c.createStatement();
s.executeUpdate(“insert into Candidate (Name) values(‘Tanishq’)”);
s.close();
c.close();
System.out.println(“DONE”);
}catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}

For Compiling command is javac jdbc1.java

For Executing command is java -classpath C:\mysqljar\mysql-connector-java-8.0.20.jar;. jdbc1psp
Since we need to include jar file of mysql and it is located at C:\mysqljar in my system i used this path.

--

--

Tanishq Rawat
Tanishq Rawat

Written by Tanishq Rawat

SDE @ Mobileum | Ex-SDE Intern at Cerebry

No responses yet