0% found this document useful (0 votes)
6 views

DBMS EXP 9 Theory

Dbms

Uploaded by

fclick717
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

DBMS EXP 9 Theory

Dbms

Uploaded by

fclick717
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Database Management Systems Laboratory Class: TE(Computer Engineering)

Group A

Assignment No 9

Title of the Assignment: Database Connectivity:


Write a program to implement MySQL/Oracle database connectivity with any front end
language to implementDatabase navigation operations (add, delete, edit etc.)

Objective of the Assignment: To understand the concept of MySQL/Oracle database


Connectivity.

Outcome: Students will be able to learn and understand MySQL/Oracle database Connectivity.

Theory:
In Java, we can connect to our database(MySQL) with JDBC(Java Database Connectivity)
through the Javacode. JDBC is one of the standard APIs for database connectivity, using it
we can easily run our query, statement, and also fetch data from the database.

❖ Prerequisite to understand Java Database Connectivity with MySQL:-


1. You have MySQL on your System.
2. You have JDK on your System.
3. To set up the connectivity user should have MySQL Connector to the Java (JAR file),
the ‘JAR’ file must bein classpath while compiling and running the code of JDBC.

❖ Steps to download MySQL Connector:

Search for MySQL community downloads.


• Then, go to the Connector/J.
• Then, select the Operating System platform-independent.
• Then, download the zip file Platform Independent (Architecture Independent), ZIP
Archive.
• Then, extract the zip file.
• Get the mysql-connector-java-8.0.20.jar file from the folder.

Department of Computer Engineering, ZCOER, Narhe, Pune-41 Page 59


Database Management Systems Laboratory Class: TE(Computer Engineering)

❖ Java Database Connectivity with MySQL.

To connect Java application with the MySQL database, we need to follow steps.
In this example we are using MySql as the database. So we need to know following
information’s for themysql database:

1. Driver class: The driver class for the mysql database is com.mysql.jdbc.Driver.
2. Connection URL: The connection URL for the mysql database is
jdbc:mysql://localhost:3306/sonoo where jdbc is the API, mysql is the database, localhost is
the server name on which mysql is running, wemay also use IP address, 3306 is the port
number and sonoo is the database name. We may use any database, in such case, we need to
replace the sonoo with our database name.
3. Username: The default username for the mysql database is root.
4. Password: It is the password given by the user at the time of installing the mysql database. In
this example,we are going to use root as the password.

Let's first create a table in the mysql database, but before creating table, we need to create

database first.create database sonoo;


use sonoo;
create table emp(id int(10),name varchar(40),age int(3));

Example to Connect Java Application with mysql database

In this example, sonoo is the database name, root is the username and password both.

1. Connection program.
import java.sql.*;
class MysqlCon{
public static void main(String args[]){ try{ Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/sonoo","root","root");
//here sonoo is database name, root is username and passwordStatement
stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from emp");while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));con.close();
}catch(Exception e){ System.out.println(e);}
}
}

Department of Computer Engineering, ZCOER, Narhe, Pune-41 Page 60


Database Management Systems Laboratory Class: TE(Computer Engineering)

2. Insert record
package simple;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class insert {

public static void main(String[] args) {


// TODO Auto-generated
method stubtry{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/TE","root","123");
Statement stmt=con.createStatement();
// Execute a query
System.out.println("Inserting records into the
table..."); String sql = "INSERT INTO student
VALUES (100, 'Zara')";stmt.executeUpdate(sql);
sql = "INSERT INTO student VALUES (101, 'Mahnaz')";
stmt.executeUpdate(sql);
sql = "INSERT INTO student VALUES (102, 'Zaid')";
stmt.executeUpdate(sql);
sql = "INSERT INTO student VALUES(103,
'Sumit')";stmt.executeUpdate(sql);
System.out.println("Inserted records into the table...");
}
catch(Exception e){ System.out.println(e);
}
}
}

Department of Computer Engineering, ZCOER, Narhe, Pune-41 Page 61


Database Management Systems Laboratory Class: TE(Computer Engineering)

3. Update Record

package simple;
import java.sql.Connection; import
java.sql.ResultSet; import
java.sql.DriverManager;import
java.sql.Statement;

public class update


{
static final String QUERY = "SELECT rollno,name FROM student"; public static void main(String[]
args)
{
// TODO Auto-generated method stubtry{
Class.forName("com.mysql.cj.jdbc.Driver"); Connection
con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/TE","root","Root@1234");
//here sonoo is database name, root is username and password
Statement stmt=con.createStatement();
String sql = "UPDATE student " + "SET name = 'om' WHERE rollno in (100)";
stmt.executeUpdate(sql);
ResultSet rs = stmt.executeQuery(QUERY);while(rs.next()){
//Display values
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}
}
catch(Exception e){ System.out.println(e);}
}
}

Department of Computer Engineering, ZCOER, Narhe, Pune-41 Page 62


Database Management Systems Laboratory Class: TE(Computer Engineering)

4. Delete record

import java.sql.Connection; import


java.sql.DriverManager;import
java.sql.ResultSet; import
java.sql.SQLException;import
java.sql.Statement;

public class JDBCExample


{
static final String DB_URL = "jdbc:mysql://localhost/TUTORIALSPOINT";
static final String USER = "guest";
static final String PASS = "guest123";
static final String QUERY = "SELECT id, first, last, age FROM Registration";

public static void main(String[] args)


{
// Open a connection
try(Connection conn = DriverManager.getConnection(DB_URL, USER,
PASS);Statement stmt = conn.createStatement();
)
{
String sql = "DELETE FROM Registration " +"WHERE id = 101";
stmt.executeUpdate(sql);
ResultSet rs = stmt.executeQuery(QUERY);while(rs.next())
{
//Display values
System.out.print("ID: " + rs.getInt("id"));
System.out.print(", Age: " + rs.getInt("age"));
}
rs.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}

Department of Computer Engineering, ZCOER, Narhe, Pune-41 Page 63


Database Management Systems Laboratory Class: TE(Computer Engineering)

Conclusion: Performed implementation of MySQL/Oracle database connectivity with java


platform.

Viva Question:
• What is database connectivity?
• Write is JDBC?
• Write is JDBC Driver?
• What are the steps to connect to the database in java?
• What is the return type of Class.forName() method?

Date:
Marks obtained:
Sign of course coordinator:
Name of course Coordinator:

Department of Computer Engineering, ZCOER, Narhe, Pune-41 Page 64

You might also like