NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

package com;

import java.sql.*;
import java.util.*;

public class crudjdbc {

static Statement st = null;
static int id;
static Scanner scan = new Scanner(System.in);
public void addCustomer(int id, String name, String addr, int age) throws SQLException {
String s = "Select * from Customer where Cust_Id="+id;
ResultSet rs = st.executeQuery(s);
if(rs.next()) {
System.out.println("Duplicate ID! n Enter Another ID");
}else {
String add = "Insert into Customer values("+id+","+"'"+name+"'"+","+"'"+addr+"'"+","+age+")";
st.execute(add);
System.out.println("Customer Added Successfully");
}
}

public void searchCustomer() throws SQLException {
String s = "Select * from Customer where Cust_id="+id;
ResultSet rs = st.executeQuery(s);
if(rs.next()) {
System.out.println("Customer ID: "+rs.getInt("Cust_Id")+"n"+"Customer Name: "+rs.getString("Cust_Name")
+"n"+"Customer Address: "+rs.getString("Cust_Addr")+"n"+"Customer Age: "+rs.getInt("Cust_Age"));
}else {
System.out.println("Customer ID Not Exist!");
}
}

public void modifyCustomer() throws SQLException {
String s = "Select * from Customer where Cust_Id="+id;
ResultSet rs = st.executeQuery(s);
if(rs.next()) {
System.out.print("Enter Name for Update: ");
String uname = scan.next();
System.out.print("Enter Address for Update: ");
String uaddr = scan.next();
System.out.print("Enter Age for Update: ");
int uage = scan.nextInt();
String update = "Update Customer SET Cust_Name="+"'"+uname+"'"+", Cust_Addr="+"'"+uaddr+"'"+", Cust_Age="+uage+" where Cust_Id="+id;
try {
st.execute(update);
System.out.println("All Column Updated Successfully");
}catch(Exception e) {
System.out.println("Some error: "+ e);
}
}else {
System.out.println("Customer ID Not Exist");
}
}

public void deleteCustomer() throws SQLException {
String s = "Select * from Customer where Cust_Id="+id;
ResultSet rs = st.executeQuery(s);
if(rs.next()) {
String del = "Delete from Customer where Cust_Id="+id;
st.execute(del);
System.out.println("Customer Removed From Database.");
}else {
System.out.println("ID Error or it may not exists");
}
}

public void showAllCustomers() throws SQLException {
System.out.println("Showing Data............");
String show = "Select * from customer";
ResultSet rs1 = st.executeQuery(show);
System.out.println("Cust_ID"+" "+"Cust_Name"+" "+"Cust_Addr"+" "+"Cust_Age");
while(rs1.next()) {
System.out.println(rs1.getInt("Cust_Id")+" "+rs1.getString("Cust_Name")+" "+rs1.getString("Cust_Addr")+" "
+rs1.getInt("Cust_Age"));
}
}
public static void main(String[] args) {
CMSClass obj1 = new CMSClass();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/customers", "root", "");
st = con.createStatement();
while(true) {
System.out.println("Choose among the following: n 1: For Add Customer n "
+ "2: For Search Customer n 3: For Modify Customer n 4: For Delete Customer n "
+ "5: For Show Customer n 6: For Exit");
int a = scan.nextInt();
if(a==1) {
System.out.print("Enter Customer ID: ");
id = scan.nextInt();
System.out.print("Enter Name of Customer: ");
String name = scan.next();
System.out.print("Enter Address of Customer: ");
String addr = scan.next();
System.out.print("Enter Age of Customer: ");
int age = scan.nextInt();
obj1.addCustomer(id, name, addr, age);
}else if(a==2) {
System.out.println("Enter Customer ID: ");
id = scan.nextInt();
obj1.searchCustomer();
}else if(a==3) {
System.out.println("Enter Customer ID: ");
id = scan.nextInt();
obj1.modifyCustomer();
}else if(a==4) {
System.out.println("Enter Customer ID: ");
id = scan.nextInt();
obj1.deleteCustomer();
}else if(a==5) {
obj1.showAllCustomers();
}else if(a==6) {
System.out.println("Thanks For using!");
System.out.println("Byy.......Meet u Next time");
break;
}else {
System.out.println("Wrong Choice");
}
}
}catch(Exception e) {
System.out.println("Some Error "+e);
}

}

}

     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.