How to connect a class in another package to a separate package


Question

I am developing a project using javafx in netbeans. I have made many classes in different packages. Now I want to connect 2 classes in different packages. How to do it ? The following code is the main class :

package createaccount;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;

public class CreateAccount extends Application {

@Override
public void start(Stage stage) {

 // Use a border pane as the root for scene
    BorderPane border = new BorderPane();

    border.setTop(addHBox());
    border.setLeft(addVBox());
    border.setCenter(addVBox1());
    border.setBottom(addHBox1());

    Scene scene = new Scene(border,700,400,Color.OLDLACE);
    stage.setScene(scene);
    stage.setTitle("Create Account");
    stage.setResizable(false);
    scene.getStylesheets().add
    (CreateAccount.class.getResource("CreateAccount.css").toExternalForm());
    stage.show();
}

private HBox addHBox() {

    HBox hbox = new HBox();
    hbox.setPadding(new Insets(15, 12, 15, 230));
    hbox.setSpacing(10);   // Gap between nodes

    Label lb1=new Label("CREATE YOUR NEW ACCOUNT");
    lb1.setAlignment(Pos.CENTER);
    lb1.setFont(Font.font("Trebuchet MS",FontWeight.BOLD,20));

    hbox.getChildren().addAll(lb1);

    return hbox;
}

private VBox addVBox() {

    VBox vbox = new VBox();
    vbox.setPadding(new Insets(50,10,20,100)); // Set all sides to 10
    vbox.setSpacing(10);              // Gap between nodes

    Label lb1=new Label("Full Name ");
    lb1.setFont(Font.font("Calibri",FontWeight.BOLD,18));
    lb1.setPrefSize(150, 30);
    lb1.setTextFill(Color.WHITE);

    Label lb2=new Label("User Name ");
    lb2.setFont(Font.font("Calibri",FontWeight.BOLD,18));
    lb2.setPrefSize(150, 30);
    lb2.setTextFill(Color.WHITE);

    Label lb3=new Label("Password ");
    lb3.setFont(Font.font("Calibri",FontWeight.BOLD,18));
    lb3.setPrefSize(150, 30);
    lb3.setTextFill(Color.WHITE);

    Label lb4=new Label("Subject ");
    lb4.setFont(Font.font("Calibri",FontWeight.BOLD,18));
    lb4.setPrefSize(150, 30);
    lb4.setTextFill(Color.WHITE);

    Label lb5=new Label("Semester ");
    lb5.setFont(Font.font("Calibri",FontWeight.BOLD,18));
    lb5.setPrefSize(150, 30);
    lb5.setTextFill(Color.WHITE);

    vbox.getChildren().addAll(lb1,lb2,lb3,lb4,lb5);

    return vbox;
}

private VBox addVBox1() {

    VBox vbox = new VBox();
    vbox.setPadding(new Insets(50,200,20,50)); // Set all sides to 10
    vbox.setSpacing(10);              // Gap between nodes

    TextField t1=new TextField();
    t1.setPrefSize(150,30);

    TextField t2=new TextField();
    t2.setPrefSize(150,30);

    PasswordField t3=new PasswordField();
    t3.setPrefSize(150,30);

    PasswordField t4=new PasswordField();
    t4.setPrefSize(150,30);

    ObservableList<String> options2 = 
    FXCollections.observableArrayList(
    "Semester 1","Semester 2","Semester 3","Semester 4",
    "Semester 5","Semester 6");
    final ComboBox comboBox2 = new ComboBox(options2);
    comboBox2.setPrefSize(200,30);

    vbox.getChildren().addAll(t1,t2,t3,t4,comboBox2);

    return vbox;
}

private HBox addHBox1() {

    HBox hbox = new HBox();
    hbox.setPadding(new Insets(15, 12, 15, 200));
    hbox.setSpacing(10);

    Button b1=new Button("SUBMIT");
    b1.setFont(Font.font("Calibri",FontWeight.BOLD,20));
    b1.setPrefSize(130,30);

    b1.setOnAction(new EventHandler<ActionEvent>() {
     @Override
     public void handle(ActionEvent e) {

     }
     });


    Button b2=new Button("CANCEL");
    b2.setFont(Font.font("Calibri",FontWeight.BOLD,20));
    b2.setPrefSize(130,30);


    hbox.getChildren().addAll(b1,b2);

    return hbox;
}

/**
 * The main() method is ignored in correctly deployed JavaFX application.
 * main() serves only as fallback in case the application can not be
 * launched through deployment artifacts, e.g., in IDEs with limited FX
 * support. NetBeans ignores main().
 *
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

}

The following 2 classes are made to connect to database :

package javasql;

import com.mysql.jdbc.Driver;
import java.sql.*;

public class Connect {
public Connect() throws SQLException{
    makeConnection();
} 

private Connection koneksi;  

 public  Connection makeConnection() throws SQLException {
    if (koneksi == null) {
         new Driver();
        // buat koneksi
         koneksi = DriverManager.getConnection(
                   "jdbc:mysql://localhost:3306/mysql","root","virus");
     }
     return koneksi;
 }  

 public static void main(String args[]) {
     try {
         Connect c = new Connect();
         System.out.println("Connection established");
     }
     catch (SQLException e) {
         e.printStackTrace();
         System.err.println("Connection Failure");
     }  

}
}

package javasql;

import java.sql.*;

public class SqlStatement {
private Statement statement;
public SqlStatement() throws SQLException{
    makeStatement();
}
public Statement makeStatement() throws SQLException{
    Connect c = new Connect();
    Connection conn = c.makeConnection();
    statement = conn.createStatement();
    return statement;
}
public void insert(String name,int npm)throws SQLException{
    statement.execute("insert into Student values(\""+name+"\","+npm+");");
}
public static void main(String arg[]){
    try {
        SqlStatement s = new SqlStatement();
        s.insert("Ferdi",1);
        s.insert("Anca",2);
        System.out.println("Success");
    }
    catch(SQLException e){
        System.out.println("Failed");
        e.printStackTrace();
    }
}
}

Now you see the codes. What I want is to read the values from the main code and store it via the database codes. I just want to know how to make these codes interlinked or connected. Someone please help...

1
0
2/5/2014 5:53:41 AM

Accepted Answer

Are both packages in the same project? If so, then you should be able to link them fine (the public keyword can be accessed from anywhere)

If not, then export the project as a .jar file, and add it to the dependency of your other project, then you should be able to import it in fine.

EDIT: (Contents of private email sent to the asker)

You could move the contents of the package to your other project, or you can follow these steps to link them:

  1. Right click the project, and click 'Properties'
  2. Click the 'Libraries' tab (to the left)
  3. Click the 'Add Project' button
  4. Select the project which it is in
  5. Press 'Add Project JAR Files', that window will close
  6. Press 'OK'
  7. Done, you can then import the classes you need, using import javasql.Connect.
4
2/5/2014 6:58:29 AM

Create a jar file from the Project and add that jar file as dependency i.e in your class path , then import that package and use required classes.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon