Professional Documents
Culture Documents
Import Import Public Class Public Static Void Int New New
Import Import Public Class Public Static Void Int New New
Import Import Public Class Public Static Void Int New New
*;
import java.io.*;
public class Javasql2
{
public static void main(String[] args)
{
int ch;
String q;
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String url = "jdbc:mysql://localhost/";
String driver = "com.mysql.jdbc.Driver";
String dbName = "company";
String userName = "root";
String password="root";
try{
Connection conn = DriverManager.getConnection(url+dbName,userName,password);
Statement st = conn.createStatement();
do
{
System.out.println("\n---------MENU---------");
System.out.println("\n1.Create Table");
System.out.println("\n2.Insert Records");
System.out.println("\n3.Select Table");
System.out.println("\n4.Update record");
System.out.println("\n5.Aggregation Function for Table");
System.out.println("\n6.Delete records");
System.out.println("\n7.Drop Table");
System.out.println("\n8.Exit");
System.out.println("\n\nEnter your choice:");
ch=Integer.parseInt(input.readLine());
switch(ch)
{
case 1:
q="CREATE TABLE products(productID INT PRIMARY KEY, Name VARCHAR(15),Debit
INT,Credit INT, account INT)";
st.executeUpdate(q);
System.out.println("\n'products' table Successfully created\n");
break;
case 2:
q = "INSERT INTO products"+" VALUES(1351,'Vishal',2000,10000,101)";
st.executeUpdate(q);
q = "INSERT INTO products"+" VALUES(1352,'Durgesh',2000,3000,102);";
st.executeUpdate(q);
q = "INSERT INTO products "+"VALUES(1353,'Anand',5600,8000,103);";
st.executeUpdate(q);
q = "INSERT INTO products "+"VALUES(1354,'pankaj',500,3000,104);";
st.executeUpdate(q);
q = "INSERT INTO products "+"VALUES(1355,'Altaf',1500,500,104);";
st.executeUpdate(q);
System.out.println("\nRecords inserted into the table 'products'...\n");
break;
case 3:
ResultSet res = st.executeQuery("SELECT * FROM products");
System.out.println("\n\nTable:product\n");
System.out.println("\n\t----------------------------------------------------------");
System.out.println("\n\t\t\t\tRECORDS");
System.out.println("\n\t--------------------------------------------------------\n");
while (res.next()) {
int id = res.getInt("productID");
String name = res.getString("Name");
int deb = res.getInt("Debit");
int cred = res.getInt("Credit");
System.out.println("\n\tAccount:"+id+"\t\tName:"+name+
"\tDebit:"+deb+"\tCredit:"+cred+"\n");
}
System.out.println("\n\t-------------------------------------------------------\n");
break;
case 4:
q ="UPDATE products SET Debit=10000 where Name='vishal'; ";
st.executeUpdate(q);
System.out.println("\nRecord is updated where Name='vishal'");
break;
case 5:
do{
System.out.println("\n\n------Aggregation Functions------\n");
System.out.println("\n1.count()");
System.out.println("\n2.sum()");
System.out.println("\n3.max()");
System.out.println("\n4.min()");
System.out.println("\n5.avg()");
System.out.println("\n6.Exit");
System.out.println("\n\nEnter your choice:");
ch=Integer.parseInt(input.readLine());
switch(ch)
{
case 1:
q="SELECT count(Name) as 'Total products' FROM products;";
res=st.executeQuery(q);
while (res.next())
{
int total = res.getInt("Total Products");
System.out.println("\n\tTotal Employee is:"+total+"\n");
}
break;
case 2:
q="SELECT sum(Debit)as 'Total Debit Amount' FROM products;";
res=st.executeQuery(q);
while (res.next())
{
int total = res.getInt("Total Debit Amount");
System.out.println("\n\tTotal Total Debit Amount:" +total+"\n");
}
break;
case 3:
q="SELECT * FROM products WHERE Debit= (SELECT max(Debit) FROM products);";
res=st.executeQuery(q);
System.out.println("\n\t---------------------------------------------------");
System.out.println("\n\t\t\t\tRECORDS");
System.out.println("\n\t------------------------------------------------\n");
while (res.next())
{
int id = res.getInt("productID");
String name = res.getString("Name");
int debi = res.getInt("Debit");
int credi = res.getInt("Credit");
System.out.println("\n\tAccount:"+id+"\t\tName:"
+name+"\tQuantity:"+debi+"\tPrice:"+credi+"\n");
}
System.out.println("\n\t-------------------------------------------------\n");
break;
case 4:
q="SELECT * FROM products WHERE Credit=(SELECT min(Credit) FROM products);";
res=st.executeQuery(q);
System.out.println("\n\t--------------------------------------------------");
System.out.println("\n\t\t\t\tRECORDS");
System.out.println("\n\t-------------------------------------------------\n");
while (res.next())
{
int id = res.getInt("productID");
String name = res.getString("Name");
int debit1 = res.getInt("Debit");
int cre = res.getInt("Credit");
System.out.println("\n\tAccount:"+id+"\t\tName:"+name+
"\tDebit:"+debit1+"\tCredit:"+cre+"\n");
}
System.out.println("\n\t-------------------------------------------------\n");
break;
case 5:
q="SELECT DISTINCT Name FROM products WHERE Debit>(SELECT AVG(Credit) FROM
products);";
res=st.executeQuery(q);
System.out.println("\n\t---------------------------------------------------");
System.out.println("\n\t\t\t\tRECORDS");
System.out.println("\n\t-------------------------------------------------\n");
while (res.next())
{
String name = res.getString("Name");
System.out.println("\n\tName:"+name+"\n");
}
System.out.println("\n\t-------------------------------------------------\n");
break;
case 6: break;
}
}while(ch!=6);
break;
case 6:
q="DELETE FROM products;";
st.executeUpdate(q);
System.out.println("\n'products' table is empty...\n");
break;
case 7:
q="DROP TABLE products;";
st.executeUpdate(q);
System.out.println("\n 'products' table are deleted successfully...\n");
break;
case 8: System.exit(0);
}
}while(ch!=8);
}
catch(SQLException se)
{
System.out.print("\nCreated error is:\n"+se+"\n");
}
catch (Exception e)
{
System.out.print("\nCreated error is:\n"+e+"\n");
}
}
}
/*
OUTPUT
---------MENU--------1.Create Table
2.Insert Records
3.Select Table
4.Update record
5.Aggregation Function for Table
6.Delete records
7.Drop Table
8.Exit
Enter your choice:
1
'products' table Successfully created
==============================================================
---------MENU--------1.Create Table
2.Insert Records
3.Select Table
4.Update record
==========================================================
---------MENU--------1.Create Table
2.Insert Records
3.Select Table
4.Update record
5.Aggregation Function for Table
6.Delete records
7.Drop Table
8.Exit
Enter your choice:
3
Table:product
----------------------------------------------------------RECORDS
--------------------------------------------------------Account:1351
Name:Vishal Debit:2000
Credit:10000
Account:1352
Name:Durgesh
Account:1353
Name:Anand
Account:1354
Name:pankaj Debit:500
Credit:3000
Account:1355
Name:Altaf
Credit:500
Debit:2000
Debit:5600
Debit:1500
Credit:3000
Credit:8000
--------------------------------------------------------
---------MENU--------1.Create Table
2.Insert Records
3.Select Table
4.Update record
5.Aggregation Function for Table
6.Delete records
7.Drop Table
8.Exit
Enter your choice:
4
Record is updated where Name='vishal'
=================================================== =
---------MENU--------1.Create Table
2.Insert Records
3.Select Table
4.Update record
5.Aggregation Function for Table
6.Delete records
7.Drop Table
8.Exit
Enter your choice:
5
------Aggregation Functions-----1.count()
2.sum()
3.max()
4.min()
5.avg()
6.Exit
Enter your choice:
1
Total Employee is:5
=========================================================== =
---------MENU--------1.Create Table
2.Insert Records
3.Select Table
4.Update record
5.Aggregation Function for Table
6.Delete records
7.Drop Table
8.Exit
Enter your choice:
7
'products' table are deleted successfully...
============================================================ =
---------MENU--------1.Create Table
2.Insert Records
3.Select Table
4.Update record
5.Aggregation Function for Table
6.Delete records
7.Drop Table
8.Exit
Enter your choice:
8
*/