将SQL Server与Java(javafx)连接

将SQL Server与Java(javafx)连接,java,javafx,Java,Javafx,下面是SQL Server与Java的连接设置。我也在使用javafx。我对使用Java进行开发非常陌生。注意:我添加了sqljdbc驱动程序。我不想将main添加到DBConnection,因为我正在控制器中使用Connection方法。有没有办法解决这个问题,或者如何在不更改连接方法的情况下添加main方法?我收到错误消息: 错误消息 Error: Main method not found in class application.ConnectionDB, please define t

下面是SQL Server与Java的连接设置。我也在使用javafx。我对使用Java进行开发非常陌生。注意:我添加了sqljdbc驱动程序。我不想将main添加到DBConnection,因为我正在控制器中使用Connection方法。有没有办法解决这个问题,或者如何在不更改连接方法的情况下添加main方法?我收到错误消息:

错误消息

Error: Main method not found in class application.ConnectionDB, please define the main method as:
 public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
FXML

 <?xml version="1.0" encoding="UTF-8"?>
 <?import javafx.scene.control.Button?>
 <?import javafx.scene.control.Label?>
 <?import javafx.scene.control.TextField?>
 <?import javafx.scene.layout.AnchorPane?>

 <AnchorPane fx:controller="application.MainController" prefHeight="407.0"  prefWidth="578.0" xmlns="http://javafx.com/javafx/10.0.1"   xmlns:fx="http://javafx.com/fxml/1">
 <children>
  <Label layoutX="123.0" layoutY="65.0" prefHeight="31.0" prefWidth="79.0"  text="Date:" />
  <Label layoutX="123.0" layoutY="138.0" prefHeight="25.0" prefWidth="127.0" text="Rim Current Value:" />
  <Label layoutX="125.0" layoutY="210.0" text="Rim Sales Value for the  Month:" />
  <Label layoutX="123.0" layoutY="283.0" text="Rim Sold Cost Bought:" />
  <TextField fx:id="txtdate" layoutX="123.0" layoutY="97.0" />
  <TextField fx:id="txtcurvalue" layoutX="123.0" layoutY="163.0" />
  <TextField fx:id="txtsalesvalue" layoutX="123.0" layoutY="234.0" />
  <TextField fx:id="txtsoldcost" layoutX="123.0" layoutY="300.0" />
  <Button layoutX="246.0" layoutY="340.0" mnemonicParsing="false"      onAction="#Rmsubmit" prefHeight="25.0" prefWidth="103.0" text="Submit" />
  </children>
 </AnchorPane>
控制器

 package application;

 import javafx.event.ActionEvent;
 import javafx.fxml.FXML;


 import javafx.scene.control.TextField;
 import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.SQLException;
 import java.util.ResourceBundle;
 import javax.print.DocFlavor.URL;

 public class MainController  {
@FXML
public TextField txtdate;
@FXML
public TextField txtcurvalue;
@FXML
public TextField txtsalesvalue;
@FXML
public TextField txtsoldcost;

public Connection conn =null;
public PreparedStatement pat = null;

@FXML
public void Rmsubmit(ActionEvent actionEvent) {



    String sqla = "Insert into RimCalc(Date, Rim_Vale,Rim_Sales,Rim_Cost) Values (?,?,?,?)";
    String date = txtdate.getText();
    String rim_value = txtcurvalue.getText();
    String Rim_Sales = txtsalesvalue.getText();
    String rim_cost = txtsoldcost.getText();



    try {
        pat = conn.prepareStatement(sqla);
        pat.setString(1, date);
        pat.setString(2, rim_value);
        pat.setString(3, Rim_Sales);
        pat.setString(4, rim_cost);

        int i = pat.executeUpdate();
        if(i==1) {
            System.out.println("Insert Successfully");
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



}

public void initializer(URL url, ResourceBundle rb) {
    conn = application.ConnectionDB.dbConn();
}

}

Java程序需要一个
main
方法作为入口点才能运行。
main
方法的标准定义如下:

public static void main(String[] args) {
  // First code of your program goes here
}
main()
方法应包括启动应用程序并加载任何界面元素以显示的代码

JavaFX应用程序还需要有一个类来扩展
应用程序
并重写其
start()
方法

下面是一个此类课程的快速而肮脏的示例:

import javafx.application.Application;
import javafx.stage.Stage;

class Main extends Application {
    public static void main(String[] args) {
        launch(args);   // Starts the JavaFX application and calls the start() method
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        // Here is where you'll initialize your views and such
    }
}
我建议在尝试连接数据库等更复杂的任务之前,先学习一些Java和JavaFX教程,了解一些基本知识

import javafx.application.Application;
import javafx.stage.Stage;

class Main extends Application {
    public static void main(String[] args) {
        launch(args);   // Starts the JavaFX application and calls the start() method
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        // Here is where you'll initialize your views and such
    }
}