英文:
exception : java.sql.SQLException:illegal connection port value '3306:etude'
问题
我有问题与我的代码,我正在使用eclipse和mysql创建登录,基本上我正在尝试连接到数据库,我用一些数据填充了我的表格,当我尝试使用帐户连接时,出现异常'java.sql.SQLException:illegal connection port value '3306:etude'',我不确定我哪里弄错了
ps: 我添加了一个mysql库,但我不知道如何导入它,也许这就是原因
以下是代码
login_controller
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ées etablished");
return cnn;
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, e);
return null;
}
}
}
main
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]);
}
}
login (fxml)(XML文件的内容)
<?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>
英文:
i have a problem with my code, i'm creating a login using eclipse and mysql ,basicaly i'm trying to connect to the DB , i filled my table with some ligne and when i try to connect with an acount the exception'java.sql.SQLException:illegal connection port value '3306:etude'' i'm not sure where i messed up
ps: i did add a llibrary for mysql but i didn't know how to import it maybe that's the raison
there is the code
login_controller
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;
}
}
}
main
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]);
}
}
login (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>
答案1
得分: 0
你的连接字符串错误。它应该是
Connection cnn= DriverManager.getConnection("jdbc:mysql://localhost:3306/etude","root","");
语法是jdbc:mysql://主机名:端口/数据库名。
英文:
Your connection string is wrong. It should be
Connection cnn= DriverManager.getConnection("jdbc:mysql://localhost:3306/etude","root","");
The syntax is jdbc:mysql://hostname:port/databasename
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论