0

Here is the main class in which I want to use the insertData() method. This method is from a different class InsertOperation, both of the classes are in the same package but still I can't use the method. I am new to java and I am still struggling to grasp it all.

package crudoperations;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.*;

public class CRUDOperations {
    /*
    private static final String CREATE_TABLE = "CREATE TABLE employee "
                                    + "(" + "user_id INT(100) NOT NULL PRIMARY KEY," + 
                                        "first_name VARCHAR(100)," +
                                        "last_name VARCHAR(100)," +
                                        "contact_number INT(10)," +
                                        "email_id VARCHAR(100))";
*/
    public static void main(String[] args) throws SQLException {
        Connection con = null;
        PreparedStatement stmt = nulll;
        try{
            con = // File enter code here Connection.getConnection();
            InsertOperation iop = InsertOperation(); //Showing error in this line
            iop.insertData();
            
            //System.out.println("Connected");
            //stmt = con.prepareStatement(CREATE_TABLE);
            //stmt.executeUpdate();
            
            //System.out.println("Table Created");
            
        }
        catch(SQLException e){
            System.err.print(e);
        }
        finally{
            if(con != null){
                con.close();
            }
        }
    }  
}

Here is the code for InsertOperation class:

package crudoperations;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class InsertOperation {
    public void insertData() throws SQLException {
        Scanner sc = new Scanner(System.in);
        
        System.out.println("Enter the First Name: ");
        String user_firstname = sc.next();
        
        System.out.println("Enter the Last Name: ");
        String user_lastname = sc.next();
        
        System.out.println("Enter the Contact Number: ");
        String user_contact = sc.next();
        
        System.out.println("Enter the Email ID: ");
        String user_email = sc.next();
        
        Connection con = null;
        con = FileConnection.getConnection();
        PreparedStatement pst = con.prepareStatement(
                "INSERT INTO employees "
             + " (user_id, first_name, last_name, contact_number, email_id)"
             + " VALUES (?,?,?,?)");
        
        pst.setString(2, user_firstname);
        pst.setString(3, user_lastname);
        pst.setString(4, user_contact);
        pst.setString(5, user_email);
        
        int i = pst.executeUpdate();
        if(i != 0){
            System.out.print("success");       
        }
        else{
            System.out.print("error");
        }     
    }
}

Here is the error :

error: cannot find symbol
            InsertOperation iop = InsertOperation();
  symbol:   method InsertOperation()
  location: class CRUDOperations
Anthony Accioly
  • 21,918
  • 9
  • 70
  • 118
  • 2
    Presumably you want to create an instance of the class, which you do with a constructor call: `InsertOperation iop = new InsertOperation();` - note the `new` part which your code is missing. – Jon Skeet Mar 06 '21 at 10:36

2 Answers2

0

Missing "new" keyword for object creation.

```
   InsertOperation iop = InsertOperation(); //Showing error in this line
```

Should be

InsertOperation iop = new InsertOperation();
0

You need to create a instance if you want to access the instance method.

InsertOperation iop = new InsertOperation();

I think you may forget the new key word here.

Else you may use static key word when declaring the insertData() method like below.

public static void insertData() throws SQLException

and then just access that method calling just method name.

Ex: insertData();