0

データベースと対話する Java に接続するにはどうすればよいですか? 私は mysql に XAMPP を使用しており、使用しているポートがわからないという問題があります。他のユーザーがインターネットから使用しているポートをコピーしたところですが、データベースのポート番号が何かわかりません。データベースへのポートを確認しますか??//localhost:3306

ここに私のコードがあります:

    try{
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    System.out.print("COnnection succesfull");
    }
    catch(Exception ex)
    {
        System.out.print("Unable to connect     ");
    }


    try
    {
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/test?user=root&password=";
        Connection con = DriverManager.getConnection(url);
        System.out.print("Connection Stablished");

    }
    catch(Exception ex)
    {
        System.out.print("Connect cannot stablished");
    }
4

2 に答える 2

0

MySQL が実行されているポートを確認するには、C:\Program Files (x86)\MySQL\MySQL Server 5.1 (Windows 8 は異なる場合がありますが、インストール ディレクトリ) にあるファイル 'my.ini' を確認できます。

私の知る限り、デフォルトは 3306 です。

コードは正しく見えますが、通常はユーザー名とパスワードを

DriverManager.getConnection(url, username, password);
于 2013-04-26T01:38:07.927 に答える
0

XAMPP の MySQL に接続する場合は、ポートを指定する必要はないと思います。これを試してください、これは私にとってはうまくいきます:

    String dbn = "yourdatabasename";
       String usr = "root";
       String pwd = "";

       String connectionURL = "jdbc:mysql://localhost/"+dbn;


       try {

                // Load the Driver class.
                Class.forName("com.mysql.jdbc.Driver");
                // If you are using any other database then load the right driver here.


                con = (Connection) DriverManager.getConnection (connectionURL, usr, pwd);


        }
        catch (SQLException e) {
            e.printStackTrace();
        }
        catch (Exception e) {
            e.printStackTrace();
    }
于 2013-04-26T01:42:07.993 に答える