【发布时间】:2020-07-21 19:38:37
【问题描述】:
我的代码有问题,我正在使用 eclipse 和 mysql 创建登录,基本上我正在尝试连接到数据库,我用一些 ligne 填充了我的表,当我尝试连接帐户时异常'java.sql.SQLException: 非法连接端口值'3306:etude'' 我不确定我在哪里搞砸了 ps:我确实为mysql添加了一个library,但我不知道如何导入它也许这就是理由
有代码
登录控制器
package application;
import java.awt.TextField;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.PasswordField;
import javafx.scene.layout.AnchorPane;
public class login_controller {
Connection cnn= null;
PreparedStatement pst =null;
ResultSet rs = null;
@FXML
private AnchorPane panel_signup;
@FXML
private AnchorPane panel_login;
@FXML
private PasswordField password;
@FXML
private TextField username;
public void LoginShow()
{
panel_login.setVisible(true);
panel_signup.setVisible(false);
}
public void SignupShow()
{
panel_login.setVisible(false);
panel_signup.setVisible(true);
}
@FXML
public void login(ActionEvent event)throws Exception
{
cnn=sql_connection.connecter();
try
{
pst=cnn.prepareStatement("select * from etudiant where userName=? and password=?");
pst.setString(1, username.getText());
pst.setString(2, password.getText());
rs= pst.executeQuery();
if(rs.next()) {JOptionPane.showMessageDialog(null, "your username and password are correct!");}
else {JOptionPane.showMessageDialog(null, "your username and password are not correct!");}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, e);
}
}
}
sql_connection
package application;
import java.sql.Connection;
import java.sql.DriverManager;
import javax.swing.JOptionPane;
public class sql_connection {
Connection cnn = null;
public static Connection connecter()
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection cnn= DriverManager.getConnection("jdbc:mysql://localhost:3306:etude","root","");
JOptionPane.showMessageDialog(null, "connection à la base de donnée etablished");
return cnn;
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, e);
return null;
}
}
}
主要
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(Main.class.getResource("/application/login.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Application.launch(Main.class, new String[0]);
}
}
登录 (fxml)
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: #cbf2ee;" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.login_controller">
<children>
<Button layoutX="112.0" layoutY="42.0" mnemonicParsing="false" onAction="#LoginShow" text="LOGIN" />
<Button layoutX="310.0" layoutY="42.0" mnemonicParsing="false" onAction="#SignupShow" text="SUGN UP" />
<AnchorPane fx:id="panel_signup" layoutX="10.0" layoutY="119.0" prefHeight="267.0" prefWidth="580.0" visible="false" AnchorPane.bottomAnchor="14.0" AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="119.0">
<children>
<ChoiceBox layoutX="334.0" layoutY="34.0" prefHeight="25.0" prefWidth="188.0" AnchorPane.rightAnchor="58.0" />
<TextField layoutX="373.0" layoutY="80.0" />
<TextField layoutX="334.0" layoutY="121.0" prefHeight="25.0" prefWidth="188.0" promptText="email" />
<TextField layoutX="334.0" layoutY="159.0" prefHeight="25.0" prefWidth="188.0" promptText="userName" AnchorPane.topAnchor="80.0" />
<ImageView fitHeight="150.0" fitWidth="200.0" layoutX="32.0" layoutY="30.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@téléchargement.png" />
</image>
</ImageView>
<Button layoutX="246.0" layoutY="209.0" mnemonicParsing="false" text="SUGN UP" />
<PasswordField layoutX="334.0" layoutY="168.0" prefHeight="25.0" prefWidth="187.0" promptText="password" />
</children>
</AnchorPane>
<AnchorPane fx:id="panel_login" layoutX="10.0" layoutY="119.0" prefHeight="267.0" prefWidth="580.0" AnchorPane.bottomAnchor="14.0" AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="119.0">
<children>
<ChoiceBox layoutX="183.0" layoutY="33.0" prefHeight="30.0" prefWidth="188.0" AnchorPane.rightAnchor="209.0" />
<PasswordField fx:id="password" layoutX="142.0" layoutY="141.0" prefHeight="40.0" prefWidth="250.0" promptText="password" />
<Button layoutX="256.0" layoutY="205.0" mnemonicParsing="false" onAction="#login" text="LOGIN" />
<TextField layoutX="142.0" layoutY="83.0" prefHeight="40.0" prefWidth="250.0" promptText="username" />
</children>
</AnchorPane>
</children>
</AnchorPane>
【问题讨论】: