DBMS EXP 9 Theory
DBMS EXP 9 Theory
Group A
Assignment No 9
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.
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
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);}
}
}
2. Insert record
package simple;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
3. Update Record
package simple;
import java.sql.Connection; import
java.sql.ResultSet; import
java.sql.DriverManager;import
java.sql.Statement;
4. Delete record
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: