Java Programming

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

CSE1007

Java Programming
Digital Assignment – 3

17BIT0294
Chandra Teja. P

Q) Explain briefly Java JDBC to connect to MySQL and perform SQL


queries, database insertion and deletion operations.

Java JDBC
JDBC stands for Java Database Connectivity. JDBC is a Java API to connect and execute the
query with the database. It is a part of JavaSE (Java Standard Edition). JDBC API uses JDBC
drivers to connect with the database.

There are four types of JDBC drivers:

o JDBC-ODBC Bridge Driver,


o Native Driver,
o Network Protocol Driver, and
o Thin Driver
We can use JDBC API to access tabular data stored in any relational database. By the help
of JDBC API, we can save, update, delete and fetch data from the database.

Java Database Connectivity with MySQL


To connect Java application with the MySQL database, we need to follow 5 following steps.

In this example we are using MySql as the database. So we need to know following
informations for the mysql 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/teja where 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 and teja is the database name. We may use any
database, in such case, we need to replace the teja 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.

Performing Database Operations in Java


 These basic operations are INSERT, SELECT, UPDATE and DELETE statements
in SQL language.

Connecting to the Database


import java.sql.*;

public class connect


{
public static void main(String args[])
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");

// Establishing Connection
Connection con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:orcl", "login1", "pwd1");

if (con != null)
System.out.println("Connected");
else
System.out.println("Not Connected");

con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

Output:
Connected

Implementing Insert Statement


import java.sql.*;

public class insert1


{
public static void main(String args[])
{
String id = "id1";
String pwd = "pwd1";
String fullname = "Teja";
String email = "[email protected]";

try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("
jdbc:oracle:thin:@localhost:1521:orcl", "login1", "pwd1");
Statement stmt = con.createStatement();

// Inserting data in database


String q1 = "insert into userid values('" +id+ "', '" +pwd+
"', '" +fullname+ "', '" +email+ "')";
int x = stmt.executeUpdate(q1);
if (x > 0)
System.out.println("Successfully Inserted");
else
System.out.println("Insert Failed");

con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

Output:
Successfully Registered

Implementing Update Statement


import java.sql.*;

public class update1


{
public static void main(String args[])
{
String id = "id1";
String pwd = "pwd1";
String newPwd = "newpwd";
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("
jdbc:oracle:thin:@localhost:1521:orcl", "login1", "pwd1");
Statement stmt = con.createStatement();

// Updating database
String q1 = "UPDATE userid set pwd = '" + newPwd +
"' WHERE id = '" +id+ "' AND pwd = '" + pwd + "'";
int x = stmt.executeUpdate(q1);

if (x > 0)
System.out.println("Password Successfully Updated");
else
System.out.println("ERROR OCCURED :(");

con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output:
Password Successfully Updated

Implementing Delete Statement


import java.sql.*;

public class delete


{
public static void main(String args[])
{
String id = "id2";
String pwd = "pwd2";
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("
jdbc:oracle:thin:@localhost:1521:orcl", "login1", "pwd1");
Statement stmt = con.createStatement();

// Deleting from database


String q1 = "DELETE from userid WHERE id = '" + id +
"' AND pwd = '" + pwd + "'";

int x = stmt.executeUpdate(q1);

if (x > 0)
System.out.println("One User Successfully Deleted");
else
System.out.println("ERROR OCCURED :(");

con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

Output:
One User Successfully Deleted

Implementing Select Statement


import java.sql.*;

public class select


{
public static void main(String args[])
{
String id = "id1";
String pwd = "pwd1";
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("
jdbc:oracle:thin:@localhost:1521:orcl", "login1", "pwd1");
Statement stmt = con.createStatement();

// SELECT query
String q1 = "select * from userid WHERE id = '" + id +
"' AND pwd = '" + pwd + "'";
ResultSet rs = stmt.executeQuery(q1);
if (rs.next())
{
System.out.println("User-Id : " + rs.getString(1));
System.out.println("Full Name :" + rs.getString(3));
System.out.println("E-mail :" + rs.getString(4));
}
else
{
System.out.println("No such user id is already registered");
}
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

Output :
User-Id : id1
Full Name : Teja
E-mail : [email protected]

You might also like