Search code examples
javasqlsqlitewhile-loopreturn

JDBC SQLite: Retrieval Operation - loops without stopping?|


My program gives user control to do any CRUD Operations on a Sqlite database.

However,my method to "Search for one vehicle" outputs the query multiple times until I get an exception, which points at the return statement.

Connection

enter image description here

Return statement

enter image description here

---------------------Here below is what I have done/looked/attempted:

  1. While loop:I attempted to: "to set the condition false then true".
    "set condition with integer to loop limited number of times". They both output same exception.
  2. Return statement: There no other expression/variable i can put into the return statement. I have no idea because the method is based on a class, so it must return that objects class.

What I am doing wrong for it to loop continuously?

The method with exception DAO Class:

 import java.sql.SQLException;  
    import java.util.ArrayList;
    import java.sql.DriverManager;   
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.Statement;
    public class CasualDAO {

    private static Connection getDBConnection(){

            Connection dbConnection=null;
            try{
                Class.forName("org.sqlite.JDBC");
            }catch(ClassNotFoundException e){
                System.out.println(e.getMessage());}
            try{
                String dbURL="jdbc:sqlite:casual.sqlite";
                dbConnection=DriverManager.getConnection(dbURL);
                return dbConnection; 
            }catch(SQLException e){
                System.out.println(e.getMessage());
            }
            return dbConnection;

        }

    public Casual getCasual(int c) throws SQLException{

        Connection dbConnection=null;
        Statement statement=null;
        ResultSet result= null;
        String query="SELECT * FROM casual WHERE id="+c+";";

        try{
            dbConnection=getDBConnection();
            statement= dbConnection.createStatement();

            System.out.println(query);
            result=statement.executeQuery(query);

            while(result.next()){

            System.out.println(result.getInt("id")+result.getString("first_name")+result.getString("last_name")+result.getString("adress"));

            }}catch(SQLException e){
                   e.getMessage();}
           finally{
              if(result!=null){
                    result.close();
              }
              if(statement!=null){
                 statement.close();
              }
              if(dbConnection!=null){
                 dbConnection.close();
              } 
         }
        return getCasual(c);

    }



    }

Controller:

import java.util.Scanner;
import java.sql.SQLException;
import java.util.ArrayList;

public class Control {

    public static void main(String[] args) {

        CasualDAO thevehicle= new CasualDAO();

        Scanner in= new Scanner(System.in);
        System.out.println("Crud Operations");
        System.out.println( "Enter operation"); 
        System.out.println("Search = 1 "+"Insert = 2 "+"Delete = 3 "+"Update 
        = 4 ");
        String operation=in.next();

        if(operation.equals("1")){
            try{
            thevehicle.getCasual(1);
            }catch(SQLException e){
             e.printStackTrace();
            }

        }

Solution

  • Create an object from class in the method and store your parameters there.Do the same in the controller except adding the parameters

    I created an object in the method of type casual called ("Casual cass=null"). Its first set to null because its not used immediately. Then, in the "while loop", its overriden with "new Casual(id, firstname, lastname,adress)",this time containing the parameters.

    In the controller, I created another "Casual" object, this time containing the casualDao.getCasual(1). 1 stands for the particular id in the query.

    public Casual getCasual(int c) throws SQLException{
    
        Connection dbConnection=null;
        Statement statement=null;
        ResultSet result= null;
        Casual cass=null;
        String query="Select * from casual WHERE id="+c+";";
    
        try{
            dbConnection=getDBConnection();
            statement= dbConnection.createStatement();
            System.out.println(query);
            result=statement.executeQuery(query);
           // Boolean test=result.next();
            while(result.next()){
    
                          int id =result.getInt("id");
                          String first_name =result.getString("first_name");
                          String last_name=result.getString("last_name");
                          String adress =result.getString("adress");
                          cass= new Casual(id,first_name,last_name,adress);
                          System.out.println(cass);
    

    Controller:

    import java.util.Scanner;
    import java.sql.SQLException;
    import java.util.ArrayList;
    
    public class Control {
    
        public static void main(String[] args) {
    
            CasualDAO case= new CasualDAO();
            Casual cass=null;
    
            Scanner in= new Scanner(System.in);
            System.out.println("Crud Operations");
            System.out.println( "Enter operation"); 
            System.out.println("Search = 1 "+"Insert = 2 "+"Delete = 3 "+"Update = 4 ");
            String operation=in.next();
    
            if(operation.equals("1")){
                try{
                cass=case.getCasual(1);
                }catch(SQLException e){
                 e.printStackTrace();
                }
    
            }
    

    Mark's question,triggered this thought process in my mind, thanks.