A java program that
queries for a customer information from a database.
Program:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import static java.lang.System.exit;
import java.sql.*;
import java.util.Scanner;
/**
*
* @author crsri
*/
public class Flash {
/**
* @param args the command line arguments
*/
static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Customer Information System");
while(true)
{
System.out.println("Select the option :\n1)Insertion\n2)Updation\n3)Deletion\n4)Display\n5)Exit");
int option = in.nextInt();
switch(option)
{
case 1:
insert();
display();
break;
case 2:
update();
display();
break;
case 3:
delete();
display();
break;
case 4:
display();
break;
case 5:
exit(0);
break;
}
}
}
public static void insert(){
PreparedStatement stmt = null;
System.out.println("Enter the data(Id,Name,Age,Email)");
int id = in.nextInt();
String name = in.next();
int age = in.nextInt();
String email = in.next();
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection sri = DriverManager.getConnection("jdbc:mysql://localhost:3306/sridhar","root","sridhar");
String query = "INSERT INTO emp VALUES(?,?,?,?)";
stmt = sri.prepareStatement(query);
stmt.setInt(1,id);
stmt.setString(2,name);
stmt.setInt(3, age);
stmt.setString(4,email);
stmt.executeUpdate();
sri.close();
stmt.close();
}
catch (Exception e)
{
System.out.println(e);
}
}
public static void update(){
PreparedStatement stmt = null;
System.out.println("Enter the data to be updated (Name,Age,Email)");
String name = in.next();
int age = in.nextInt();
String email = in.next();
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection sri = DriverManager.getConnection("jdbc:mysql://localhost:3306/sridhar","root","sridhar");
String query = "UPDATE emp SET Age=?,Email=? WHERE Name=?";
stmt = sri.prepareStatement(query);
stmt.setString(3,name);
stmt.setInt(1, age);
stmt.setString(2,email);
stmt.executeUpdate();
sri.close();
stmt.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void delete(){
PreparedStatement stmt = null;
System.out.println("Enter the data to be delete(Name)");
String name = in.next();
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection sri = DriverManager.getConnection("jdbc:mysql://localhost:3306/sridhar","root","sridhar");
String query = "DELETE FROM emp WHERE Name=?";
stmt = sri.prepareStatement(query);
stmt.setString(1,name);
stmt.executeUpdate();
sri.close();
stmt.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void display(){
PreparedStatement stmt = null;
System.out.println("Id\tName\tAge\tEmail");
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection sri = DriverManager.getConnection("jdbc:mysql://localhost:3306/sridhar","root","sridhar");
String query = "SELECT * FROM emp";
stmt = sri.prepareStatement(query);
ResultSet rs = stmt.executeQuery();
while(rs.next())
{
System.out.print(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getInt(3)+"\t"+rs.getString(4)+"\n");
}
rs.close();
sri.close();
stmt.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Output:
Table :
Comments
Post a Comment