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