我最近做了一个项目,涉及使用java中的swing在线连接mysql数据库。然后,我决定将该项目转换为javafx,并尝试复制代码以连接到mysql数据库。
这是我的代码:
package virtlib;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import java.sql.*;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author param
*/
public class FXMLDocumentController implements Initializable {
private Connection con;
private Statement st;
private ResultSet rs;
private ResultSet rs2;
private Stage stage;
@FXML
private Label label;
@FXML
private Button login;
@FXML
private PasswordField password;
@FXML
private TextField username;
@FXML
private void handleButtonAction(ActionEvent event) {
try{
Class.forName("com.mysql.cj.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://db4free.net:3306/parambase","theboss12k","Password");//Password has been changed
st=con.createStatement();
label.setText("Connection success !");
}
catch(Exception ae){
label.setText("We are unable to connect to our servers. Please check your internet connection and restart the app !");
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
这是fxml文件的代码
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>
<AnchorPane id="AnchorPane" prefHeight="494.0" prefWidth="409.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/16" fx:controller="virtlib.FXMLDocumentController">
<children>
<Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" prefHeight="18.0" prefWidth="276.0" />
<Pane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="297.0" prefWidth="410.0" style="-fx-background-color: white;" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<Button fx:id="login" layoutX="129.0" layoutY="331.0" onAction="#handleButtonAction" prefHeight="26.0" prefWidth="70.0" text="Login" />
<PasswordField fx:id="password" layoutX="129.0" layoutY="276.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="26.0" prefWidth="222.0" />
<TextField fx:id="username" layoutX="129.0" layoutY="229.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="26.0" prefWidth="222.0" />
<Label layoutX="40.0" layoutY="280.0" text="Password" />
<Label layoutX="38.0" layoutY="233.0" text="Username" />
<Label layoutX="129.0" layoutY="400.0" prefHeight="17.0" prefWidth="222.0" textFill="#e70d1b" />
</children>
</Pane>
</children>
</AnchorPane>
然而,当我点击run时,它只是崩溃了,我得到一个错误,“JavaSE二进制平台已经停止工作”。在我以前使用swing的应用程序中,它工作得非常好。在切换到javafx时,我所做的唯一更改是使用JDK1.8而不是JDK11,因为我被迫使用netbeans 8.2,而netbeans 8.2似乎不支持JDK11(我以前的项目就是使用JDK11开发的)。
暂无答案!
目前还没有任何答案,快来回答吧!