2013-04-26 3 views
0

데이터베이스와 상호 작용하는 Java에 어떻게 연결할 수 있습니까? 나는 MySQL을위한 XAMPP를 사용하고있다 그리고 나는 내가 사용하고있는 포트를 아는 데 문제가있다. 나는 인터넷에서 다른 포트를 복사하고있다. 그러나 나는 정말로 데이터베이스의 포트 번호가 무엇인지 모른다. 또한 어떻게 데이터베이스로가는 포트를 확인합니까 ??Java를 사용하여 데이터베이스에 연결하는 방법

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"); 
    } 

답변

0

당신은 파일 '의 my.ini가' C에 위치를 확인할 수 있습니다, MySQL이 실행되는 포트를 확인하려면 : 여기

//localhost:3306 내 코드입니다 \ Program 파일 (x 86) \ MySQL은 \ MySQL 서버 5.1 (윈도우 8, 다를 수 있지만 설치 디렉토리).

기본적으로 내 지식은 3306입니다.

코드는 내가 보통

DriverManager.getConnection(url, username, password); 
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(); 
    } 
+0

난 여전히 예외가 말했다 무슨 짓을했는지 –

+0

를 연결하지 못할? – cakil

+0

stablishedjava.lang.ClassNotFoundException : com.mysql.jdbc.Driver –

관련 문제