exception: java.sql.SQLException: 非法的连接端口值 ‘3306:etude’

huangapple go评论91阅读模式
英文:

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(&quot;select * from etudiant where userName=? and password=?&quot;);
pst.setString(1, username.getText());
pst.setString(2, password.getText());
rs= pst.executeQuery();
if(rs.next()) {JOptionPane.showMessageDialog(null, &quot;your username and password are correct!&quot;);}
else {JOptionPane.showMessageDialog(null, &quot;your username and password are not correct!&quot;);}
}
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(&quot;com.mysql.jdbc.Driver&quot;);
Connection cnn= DriverManager.getConnection(&quot;jdbc:mysql://localhost:3306:etude&quot;,&quot;root&quot;,&quot;&quot;);
JOptionPane.showMessageDialog(null, &quot;connection &#224; la base de donn&#233;e etablished&quot;);
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(&quot;/application/login.fxml&quot;));
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)

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;?import javafx.scene.control.Button?&gt;
&lt;?import javafx.scene.control.ChoiceBox?&gt;
&lt;?import javafx.scene.control.PasswordField?&gt;
&lt;?import javafx.scene.control.TextField?&gt;
&lt;?import javafx.scene.image.Image?&gt;
&lt;?import javafx.scene.image.ImageView?&gt;
&lt;?import javafx.scene.layout.AnchorPane?&gt;
&lt;AnchorPane prefHeight=&quot;400.0&quot; prefWidth=&quot;600.0&quot; style=&quot;-fx-background-color: #cbf2ee;&quot; xmlns=&quot;http://javafx.com/javafx/11.0.1&quot; xmlns:fx=&quot;http://javafx.com/fxml/1&quot; fx:controller=&quot;application.login_controller&quot;&gt;
&lt;children&gt;
&lt;Button layoutX=&quot;112.0&quot; layoutY=&quot;42.0&quot; mnemonicParsing=&quot;false&quot; onAction=&quot;#LoginShow&quot; text=&quot;LOGIN&quot; /&gt;
&lt;Button layoutX=&quot;310.0&quot; layoutY=&quot;42.0&quot; mnemonicParsing=&quot;false&quot; onAction=&quot;#SignupShow&quot; text=&quot;SUGN UP&quot; /&gt;
&lt;AnchorPane fx:id=&quot;panel_signup&quot; layoutX=&quot;10.0&quot; layoutY=&quot;119.0&quot; prefHeight=&quot;267.0&quot; prefWidth=&quot;580.0&quot; visible=&quot;false&quot; AnchorPane.bottomAnchor=&quot;14.0&quot; AnchorPane.leftAnchor=&quot;10.0&quot; AnchorPane.rightAnchor=&quot;10.0&quot; AnchorPane.topAnchor=&quot;119.0&quot;&gt;
&lt;children&gt;
&lt;ChoiceBox layoutX=&quot;334.0&quot; layoutY=&quot;34.0&quot; prefHeight=&quot;25.0&quot; prefWidth=&quot;188.0&quot; AnchorPane.rightAnchor=&quot;58.0&quot; /&gt;
&lt;TextField layoutX=&quot;373.0&quot; layoutY=&quot;80.0&quot; /&gt;
&lt;TextField layoutX=&quot;334.0&quot; layoutY=&quot;121.0&quot; prefHeight=&quot;25.0&quot; prefWidth=&quot;188.0&quot; promptText=&quot;email&quot; /&gt;
&lt;TextField layoutX=&quot;334.0&quot; layoutY=&quot;159.0&quot; prefHeight=&quot;25.0&quot; prefWidth=&quot;188.0&quot; promptText=&quot;userName&quot; AnchorPane.topAnchor=&quot;80.0&quot; /&gt;
&lt;ImageView fitHeight=&quot;150.0&quot; fitWidth=&quot;200.0&quot; layoutX=&quot;32.0&quot; layoutY=&quot;30.0&quot; pickOnBounds=&quot;true&quot; preserveRatio=&quot;true&quot;&gt;
&lt;image&gt;
&lt;Image url=&quot;@t&#233;l&#233;chargement.png&quot; /&gt;
&lt;/image&gt;
&lt;/ImageView&gt;
&lt;Button layoutX=&quot;246.0&quot; layoutY=&quot;209.0&quot; mnemonicParsing=&quot;false&quot; text=&quot;SUGN UP&quot; /&gt;
&lt;PasswordField layoutX=&quot;334.0&quot; layoutY=&quot;168.0&quot; prefHeight=&quot;25.0&quot; prefWidth=&quot;187.0&quot; promptText=&quot;password&quot; /&gt;
&lt;/children&gt;
&lt;/AnchorPane&gt;
&lt;AnchorPane fx:id=&quot;panel_login&quot; layoutX=&quot;10.0&quot; layoutY=&quot;119.0&quot; prefHeight=&quot;267.0&quot; prefWidth=&quot;580.0&quot; AnchorPane.bottomAnchor=&quot;14.0&quot; AnchorPane.leftAnchor=&quot;10.0&quot; AnchorPane.rightAnchor=&quot;10.0&quot; AnchorPane.topAnchor=&quot;119.0&quot;&gt;
&lt;children&gt;
&lt;ChoiceBox layoutX=&quot;183.0&quot; layoutY=&quot;33.0&quot; prefHeight=&quot;30.0&quot; prefWidth=&quot;188.0&quot; AnchorPane.rightAnchor=&quot;209.0&quot; /&gt;
&lt;PasswordField fx:id=&quot;password&quot; layoutX=&quot;142.0&quot; layoutY=&quot;141.0&quot; prefHeight=&quot;40.0&quot; prefWidth=&quot;250.0&quot; promptText=&quot;password&quot; /&gt;
&lt;Button layoutX=&quot;256.0&quot; layoutY=&quot;205.0&quot; mnemonicParsing=&quot;false&quot; onAction=&quot;#login&quot; text=&quot;LOGIN&quot; /&gt;
&lt;TextField layoutX=&quot;142.0&quot; layoutY=&quot;83.0&quot; prefHeight=&quot;40.0&quot; prefWidth=&quot;250.0&quot; promptText=&quot;username&quot; /&gt;
&lt;/children&gt;
&lt;/AnchorPane&gt;
&lt;/children&gt;
&lt;/AnchorPane&gt;

答案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(&quot;jdbc:mysql://localhost:3306/etude&quot;,&quot;root&quot;,&quot;&quot;);

The syntax is jdbc:mysql://hostname:port/databasename

huangapple
  • 本文由 发表于 2020年7月22日 03:38:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63021909.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定