如何将另一个软件包中的类连接到单独的软件包



我正在使用NetBean中使用Javafx开发一个项目。我在不同的包裹上做了许多课程。现在,我想在不同的软件包中连接两个类。怎么做 ?以下代码是主要类:

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);
}
}

以下2类用于连接到数据库:

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();
    }
}
}

现在您看到了代码。我想要的是从主代码中读取值并通过数据库代码存储它。我只想知道如何使这些代码相互联系或连接。有人请帮助...

两个软件包都在同一项目中吗?如果是这样,那么您应该能够链接它们(可以从任何地方访问public关键字)

如果没有,然后将项目导出为.jar文件,然后将其添加到另一个项目的依赖性中,那么您应该可以罚款。

edit :(发送给询问者的私人电子邮件的内容)

您可以将包装的内容移至您的另一个项目,也可以按照以下步骤链接它们:

  1. 右键单击项目,然后单击"属性"
  2. 单击"库"选项卡(左侧)
  3. 单击"添加项目"按钮
  4. 选择它在
  5. 中的项目
  6. 按"添加项目jar文件",该窗口将关闭
  7. 按'确定'
  8. 完成,然后可以使用导入Javasql.connect导入所需的类。

从项目中创建一个jar文件,并将JAR文件添加为依赖项,即在类路径中,然后导入该软件包并使用所需的类。

相关内容

最新更新