import [Link].
*;
import [Link].*;
/*CREATE TABLE crudstudent (
rollno int NOT NULL PRIMARY KEY,
name varchar(40), mobile varchar(10)
)
*/
class DAOHandler {
private int mrollno;
private String mname, mmobile;
Scanner scn = new Scanner([Link]);
private Connection getDBConnection() {
Connection conn = null;
try {
[Link](new [Link]());
String mysqlUrl = "jdbc:mysql://localhost:3307/classicmodels";
conn = [Link](mysqlUrl, "root", "password");
}
catch (Exception e) {
[Link]("Connection Error :" + [Link]());
}
return conn;
}
private void getData() {
[Link]("Enter the valid rollno :");
mrollno = [Link]();
[Link]("Enter the valid name :");
mname = [Link]();
[Link]("Enter the valid mobile :");
mmobile = [Link]();
}
private void getrollno() {
[Link]("Enter the valid rollno :");
mrollno = [Link]();
}
public int addRecord() throws Exception {
int result = 0;
Connection conn = null;
try {
conn = getDBConnection();
getData();
String SQL = "insert into CRUDStudent values(?,?,?)";
PreparedStatement pstmt = [Link](SQL);
[Link](1, mrollno);
[Link](2, mname);
[Link](3, mmobile);
result = [Link]();
}
catch (Exception e) {
[Link]("Insert Error :" + [Link]());
}
[Link]();
return result;
}
public void editRecord() throws Exception {
getrollno();
Connection conn = null;
PreparedStatement pstmt;
boolean flag = false;
try {
conn = getDBConnection();
pstmt = [Link](
"select * from CRUDStudent where rollno = ?");
[Link](1, mrollno);
ResultSet result = [Link]();
while([Link]()) {
flag= true;
[Link]("Student Name :" + [Link](2));
[Link]("Student Mobile:" + [Link](3));
}
if(flag) {
[Link]("Enter data to be update");
[Link]("-----------------------");
[Link]("Enter the valid name :");
mname = [Link]();
[Link]("Enter the valid mobile :");
mmobile = [Link]();
pstmt = [Link]("update crudStudent set name=?,mobile=? where
rollno=?");
[Link](1, mname);
[Link](2, mmobile);
[Link](3, mrollno);
[Link]();
[Link]("Record updated !");
}
else
{
[Link]("Record not found, Try later !");
}
}
catch (Exception e) {
[Link]("Edit error :" + [Link]());
}
}
public void deleteRecord() throws Exception {
getrollno();
Connection conn = null;
PreparedStatement pstmt = null;
boolean flag = false;
try {
conn = getDBConnection();
pstmt = [Link](
"select * from CRUDStudent where rollno = ?");
[Link](1, mrollno);
ResultSet result = [Link]();
while([Link]()) {
flag= true;
pstmt = [Link](
"delete from CRUDStudent where rollno = ?");
[Link](1, mrollno);
[Link]();
[Link]("Record Deleted !");
}
if(!flag)
[Link]("Record not found, Try later !");
}
catch (Exception e) {
[Link]("Delete :" + [Link]());
}
[Link]();
}
public void searchRecord() throws Exception{
getrollno();
Connection conn = null;
boolean flag = false;
try {
conn = getDBConnection();
PreparedStatement pstmt = [Link](
"select * from CRUDStudent where rollno = ?");
[Link](1, mrollno);
ResultSet result = [Link]();
while([Link]()) {
flag= true;
[Link]("Student Name :" + [Link](2));
[Link]("Student Mobile:" + [Link](3));
}
if(!flag)
[Link]("Record not found, Try later !");
}
catch (Exception e) {
[Link]("Insert Error :" + [Link]());
}
[Link]();
}
public void showAll() throws SQLException {
Connection conn = null;
try {
conn = getDBConnection();
PreparedStatement pstmt =
[Link]("select * from CRUDStudent");
ResultSet result = [Link]();
[Link]("Rollno\tStudent Name\tMobile");
while([Link]()) {
[Link]([Link](1) + "\t" +
[Link](2) + "\t\t" + [Link](3));
}
}
catch (Exception e) {
[Link]([Link]());
}
[Link]();
}
}
public class CRUDMenu {
public static void main(String[] args) {
int choice = 0;
Scanner getch = new Scanner([Link]);
DAOHandler crudobj = new DAOHandler();
while(choice != 6) {
[Link]("1 - Add Record");
[Link]("2 - Edit Record");
[Link]("3 - Delete Record");
[Link]("4 - Search Record");
[Link]("5 - View All Records");
[Link]("6 - Exit");
[Link]("Enter the valid choice :");
choice = [Link]();
switch (choice) {
case 1: {
try {
int retval = [Link]();
if(retval > 0 )
[Link]("Record inserted !");
else
[Link]("Error in insert operation !");
}
catch (Exception e) {
[Link]("Error :" + [Link]());
}
break;
}
case 2:
try {
[Link]();
}
catch (Exception e) {
[Link]("Edit :" + [Link]());
}
break;
case 3:
try {
[Link]();
}
catch (Exception e) {
[Link]("Delete :" +
[Link]());
}
break;
case 4:
try {
[Link]();
}
catch (Exception e) {
[Link]("Search :" +
[Link]());
}
break;
case 5:
try {
[Link]();
}
catch (Exception e) {
[Link]("Show All:" +
[Link]());
}
break;
case 6:
[Link]("Thank you for using CRUD
operation !");
return;
default:
[Link]("Invalid choice !"); break;
}
}
}
}