1) JDBC Crud Operations: Code

You might also like

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

GMCA03_SHLOK SHARMA JAVA WEB TECHNOLOGIES (629408)

1) JDBC CRUD OPERATIONS

CODE:
import java.sql.*;
public class CRUDJDBC {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL =
"jdbc:mysql://localhost:3306/crud";

static final String USER = "root";


static final String PASS = "";

public static void main(String[] args) {


Connection conn = null;
Statement stmt = null;

//ESTABLISHING CONNECTION................
try
{
Class.forName("com.mysql.jdbc.Driver");

conn =
DriverManager.getConnection(DB_URL,USER,PASS);

if (conn != null) {
System.out.println("Connected");
System.out.println("");
}
else {
System.out.println("Not Connected");}

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

//DISPLAYING RECORDS................
try {
stmt = conn.createStatement();
String q2 = "SELECT * from employee" ;
ResultSet rs = stmt.executeQuery(q2);
while(rs.next())
{
System.out.println("User-Id : " + rs.getString(1));
1
GMCA03_SHLOK SHARMA JAVA WEB TECHNOLOGIES (629408)

System.out.println("Full Name : " + rs.getString(2));


System.out.println("Designation : " +
rs.getString(3));
System.out.println("Date Of Birth : " +
rs.getString(4));
System.out.println("Salary : " + rs.getString(5));
System.out.println("");

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

//INSERTING NEW DATA...................


try
{
stmt = conn.createStatement();

// Inserting data in database


String q1 = "insert into employee
values(1,'Uttam','Manager','2000-06-26',35000)";
int x = stmt.executeUpdate(q1);
if (x > 0)
System.out.println("Successfully Inserted");
else
System.out.println("Insert Failed");

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

//UPDATING DATA........................
try {
stmt = conn.createStatement();
String q2 = "UPDATE employee set e_name='ROMAN' where
id = 1" ;
int x1 = stmt.executeUpdate(q2);
if (x1 > 0)
System.out.println("Successfully Updated");
else
System.out.println("Update Failed");

2
GMCA03_SHLOK SHARMA JAVA WEB TECHNOLOGIES (629408)

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

//DELETING DATA.........................
try {
stmt = conn.createStatement();
String q2 = "DELETE from employee where id = 1" ;
int x1 = stmt.executeUpdate(q2);
if (x1 > 0)
System.out.println("Successfully Deleted");
else
System.out.println("Deletion Failed");

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

}
}

3
GMCA03_SHLOK SHARMA JAVA WEB TECHNOLOGIES (629408)

OUTPUT:
Establishing Connection:

Displaying All Records:

Inserting New Record:

Updating a Record:

Deleting a Record:

4
GMCA03_SHLOK SHARMA JAVA WEB TECHNOLOGIES (629408)

2) INSERT USING CALLABLE STATEMENT


CODE:
import java.sql.*;
import java.util.*;
public class CALLABLE{
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/crud";
static final String USER = "root";
static final String PASS = "";
public static void main(String args[]) throws Exception {
Connection conn = null;
CallableStatement cstmt = null;

Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Creating statement...");
cstmt = conn.prepareCall("call addemp(?,?,?,?,?)");
cstmt.setInt(1,5);
cstmt.setString(2, "Kevin");
cstmt.setString(3, "Assistant");
cstmt.setString(4, "1998-7-17");
cstmt.setInt(5, 17000);
cstmt.execute();
System.out.println("***Procedure called****");
System.out.println("Record Sucessfully Inserted");
conn.close();
}
}

OUTPUT:

5
GMCA03_SHLOK SHARMA JAVA WEB TECHNOLOGIES (629408)

3) INSERT USING PREPARED STATEMENT

CODE:
import java.sql.*;
public class PREPARESTMT {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/crud";
static final String USER = "root";
static final String PASS = "";
public static void main(String[] args) {
Connection conn = null;
PreparedStatement stmt = null;
PreparedStatement stmtInsert = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Creating statement...");
String sqlInsert = "insert into employee (id, e_name,
e_deg, e_dob, e_sal) values(?,?,?,?,?)";
stmtInsert.setInt(1,7);
stmtInsert.setString(2, "John");
stmtInsert.setString(3, "CEO");
stmtInsert.setString(4, "1998-7-17");
stmtInsert.setInt(5, 90000);
stmtInsert.executeUpdate();
System.out.println("Data of id 7 is inserted...");
}
catch(Exception e) {
System.out.println(e);
}
}
}
OUTPUT:

You might also like