Click here to Skip to main content
15,899,124 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This program is a menu driven program about ebook details. I have errors in almost all of the case and break statements. They are of syntax errors.

What I have tried:

Java
import java.sql.*;
import java.util.*;
import javax.swing.JOptionPane;
public class ebookshop{
	public static void main(String[]args){
		Connection c = null;
        Statement stmt = null;
        Class.forName("org.postgresql.Driver");
        c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/EbookManagement", "postgres", "Nandini");
		Scanner zz = new Scanner(System.in);
		System.out.println(
			"\n"
			+ "\n"
			+ "*********************"
			+ "******************** ");
		System.out.println(
			"** WELCOME TO ebookshop"
			+ " book SERVICES ** ");
		System.out.println(
			"*********************"
			+ "******************** ");

		System.out.println(
			"Here you have several"
			+ " tasks to perform -- "
			+ "\n"
			+ "\n");

		System.out.println(
			"Press 1 for displaying "
			+ "\n"
			+ "\n"
			+ "Press 2 for "
			+ "update"
			+ "\n"
			+ "\n"
			+ "Press 3 for "
			+ "search detail"
			+ "\n"
			+ "\n"
			+ "Press 4 to"
			+ "delete details"
			+ "\n"
			+ "\n"
			+ "Press 5 to insert "
			+ "details"
			+ "\n"
			+ "\n"
			+ "Press 6 to exit "
			);
		int mainCH = zz.nextInt();
		switch (mainCH) {
		case 1:
		    void display() { 	
		        try {
		    	Connection c = null;
		        Statement stmt = null;
		        c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/EbookManagement", "postgres", "Nandini");
			stmt = c.createStatement();
            ResultSet rs = stmt.executeQuery( "SELECT * FROM EBOOK;" );
            while ( rs.next() ) {
               int id = rs.getInt("ID");
               String  title = rs.getString("TITLE");
               String author  = rs.getString("AUTHOR");
               float price = rs.getFloat("PRICE");
               int quantity = rs.getInt("QUANTITY");
               System.out.println( "Id = " + id );
               System.out.println( "title = " + title);
               System.out.println( "author= " + author );
               System.out.println( "price = " + price);
               System.out.println( "quantity= " + quantity);
            }
            rs.close();
            stmt.close();
            c.close();
         }    
	      catch (Exception e)
			{
        	 e.printStackTrace();
            }
		    }
		break;
		case 2:
		public void update() {
		try {
			Connection c = null;
	        Statement stmt = null;
	        c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/EbookManagement", "postgres", "Nandini");
			String query="UPDATE COMPANY set PRICE = 66.66 where ID=1001;";
			PreparedStatement pst=c.prepareStatement(query);
			ResultSet rs=pst.executeQuery();
			System.out.println(rs);
		}
		catch (Exception e) {
       	 e.printStackTrace();
        }
	}
		case 3:
		public void search()
		{
			
		try {
			
			Connection c = null;
	        Statement stmt = null;
	        c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/EbookManagement", "postgres", "Nandini");
			String range;
			Scanner obj=new Scanner(System.in);
			range=obj.nextLine();
			if(range=="5-15")
			{
				ResultSet rs = stmt.executeQuery( "select * from EBOOK where price=11.11");
				System.out.println(rs);		
			}
			if(range=="16-25")
			{
				ResultSet rs = stmt.executeQuery( "select * from EBOOK where price=22.22");
				System.out.println(rs);		
			}
			if(range=="26-35")
			{
				ResultSet rs = stmt.executeQuery( "select * from EBOOK where price=33.33");
				System.out.println(rs);		
			}
			if(range=="36-45")
			{
				ResultSet rs = stmt.executeQuery( "select * from EBOOK where price=44.44");
				System.out.println(rs);		
			}
			if(range=="46-55")
			{
				ResultSet rs = stmt.executeQuery( "select * from EBOOK where price=55.55");
				System.out.println(rs);		
			}
		}
		catch (Exception e) {
	       	 e.printStackTrace();
	        }
		case 4:
		public void delete()
		{
			try
			{
				Connection c = null;
		        Statement stmt = null;
		        c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/EbookManagement", "postgres", "Nandini");
				String id;
				Scanner obj=new Scanner(System.in);
				id=obj.nextLine();
				if(id=="1001")
				{
					ResultSet rs = stmt.executeQuery( "delete * from EBOOK where id =1001");
					System.out.println(rs);		
				}
				if(id=="1002")
				{
					ResultSet rs = stmt.executeQuery( "delete * from EBOOK where id =1002");
					System.out.println(rs);		
				}
				if(id=="1003")
				{
					ResultSet rs = stmt.executeQuery( "delete * from EBOOK where id=1003");
					System.out.println(rs);		
				}
				if(id=="1004")
				{
					ResultSet rs = stmt.executeQuery( "delete* from EBOOK where id=1004");
					System.out.println(rs);		
				}
				if(id=="1005")
				{
					ResultSet rs = stmt.executeQuery( "delete * from EBOOK where id=1005");
					System.out.println(rs);		
				}	
			}
			catch (Exception e) {
		       	 e.printStackTrace();
		        }
		}
		case 5:
		public void insert()
		{
			try {
				Connection c = null;
		        Statement stmt = null;
		        c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/EbookManagement", "postgres", "Nandini");
		        stmt = c.createStatement();
	             String sql = "INSERT INTO EBOOK (ID,TITLE,AUTHOR,PRICE,QUANTITY) "
	                + "VALUES (1006, 'RobinHood', Paul, 88.88, 20 );";
	             stmt.executeUpdate(sql);
			}
			catch (Exception e) {
		       	 e.printStackTrace();
		        }
		}
	}
		case 6:
		System.out.println("Exited!");
Posted
Updated 1-May-21 19:10pm
v2

1 solution

You can't declare your functions there, and you don't call them anyway!
What you need to do is several things:
1) Declare the methods outside the main function
2) Make them static so they can be called from main - a static method - without needing a class instance.
3) Call them in your switch block:
Java
public static void main(String[] args) {
    ...
    switch (mainCH) {
        case 1:
            display();
            break;
        case 2:
            update();
            break;
        ...
    }
    ...
}
public static void display() {
    ...
}
public static void update() {
    ...
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900