Professional Documents
Culture Documents
1) JDBC Crud Operations: Code
1) JDBC Crud Operations: Code
1) JDBC Crud Operations: Code
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";
//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)
}
catch(Exception e) {
System.out.println(e);
}
// 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:
Updating a Record:
Deleting a Record:
4
GMCA03_SHLOK SHARMA JAVA WEB TECHNOLOGIES (629408)
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)
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: