2014-06-17 2 views
0

도와 주실 수 있습니까? 이미 jdbc 빌드 경로를 수행하고 DB savetime이 존재합니다.데이터베이스 서버에 연결할 수 없습니다 - 이클립스 MySQL 오류

내 수업 :

package br.com.savetime; 

    import java.sql.*; 

    public class CriarConexao { 

    private static Connection con = null; 

    public static Connection abrirBanco(){ 
     Connection con; 
     try{ 
      Class.forName("com.mysql.jdbc.Driver"); 
      con = DriverManager.getConnection("jdbc:mysql://localhost:3306/savetime", "root", "root"); 
      System.out.println("conectando"); 
      return con; 
     } 
     catch(ClassNotFoundException cnfe){ 
      System.out.println("driver nao encontrado: " + cnfe.getMessage()); 
      return null; 
     } 
     catch(SQLException sql){ 
      System.out.println("SQLException: " + sql.getMessage()); 
      System.out.println("SQLState: " + sql.getSQLState()); 
      System.out.println("Erro: " + sql.getErrorCode()); 
      System.out.println("StackTrace: " + sql.getStackTrace()); 
      return null; 
     } 
     catch(Exception e){ 
      System.out.println(e.getMessage()); 
      return null; 
     } 
    } 

    public static void fecharBDcon(){ 
     try{ 
      con.close(); 
     } 
     catch(Exception e){ 
      System.out.println("erro ao fechar o banco" + e.getMessage()); 
     } 
    } 

} 

내 오류 : 코드가 잘 작동

06-17 03:55:07.139: I/System.out(1367): SQLException: Could not create connection to database server. 

06-17 03:55:07.139: I/System.out(1367): SQLState: 08001 

06-17 03:55:07.139: I/System.out(1367): Erro: 0 

06-17 03:55:07.149: I/System.out(1367): StackTrace: [Ljava.lang.StackTraceElement;@41bc1af0 
+0

"SQLException : 데이터베이스에 연결할 수 없습니다"- 관련이있는 것 같습니다. vant – user2864740

+1

당신의 classpath에 당신의 운전병 단지가 있습니까? – NaaN

+0

읽기 : [* Common Problems and Solutions *] (http://dev.mysql.com/doc/refman/5.0/es/connector-j-usagenotes-troubleshooting.html#qandaitem-25-4-5-3 -1-1) –

답변

0

바로 찾을 수 있습니다 classpath.We의 mysql-connector.jar을 제공하는 것이 here 내가 시도 당신의 코드 is :

import java.sql.*; 

public class ConnectionTest { 

private static Connection con = null; 

public static Connection abrirBanco(){ 
    Connection con; 
    try{ 
     Class.forName("com.mysql.jdbc.Driver"); 
     con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "welcome"); 
     System.out.println("conectando"); 
     return con; 
    } 
    catch(ClassNotFoundException cnfe){ 
     System.out.println("driver nao encontrado: " + cnfe.getMessage()); 
     return null; 
    } 
    catch(SQLException sql){ 
     System.out.println("SQLException: " + sql.getMessage()); 
     System.out.println("SQLState: " + sql.getSQLState()); 
     System.out.println("Erro: " + sql.getErrorCode()); 
     System.out.println("StackTrace: " + sql.getStackTrace()); 
     return null; 
    } 
    catch(Exception e){ 
     System.out.println(e.getMessage()); 
     return null; 
    } 
} 

public static void fecharBDcon(){ 
    try{ 
     con.close(); 
    } 
    catch(Exception e){ 
     System.out.println("erro ao fechar o banco" + e.getMessage()); 
    } 
} 
public static void main(String arr[]) 
{ 
    System.out.println("Making connection.."); 
    Connection connection = ConnectionTest.abrirBanco(); 
    System.out.println("Connection made..."); 
} 
} 
+0

고마워, 내 커넥터가 잘못 했어. 그러나 이제는이 오류가 발생합니다. SQLException : 기본 예외로 인해 통신 링크가 실패했습니다. I/System.out (1153) : ** 중첩 예외 발생 ** I/System.out (1153) : android.os.NetworkOnMainThreadException I/System.out (1153) : STACKTRACE : android.os.NetworkOnMainThreadException android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork (StrictMode.java:1117) I/System.out (1153) : \t at java.net. InetAddress.lookupHostByName (InetAddress.java:385) 다시 도와 주실 수 있습니까? –

+0

전체 스택 트레이스와 실패한 클래스를 게시하는 경우 매우 유용합니다 – SparkOn

+0

여기를 보는 것이 더 좋습니다 http://stackoverflow.com/questions/24276366/connection-mysql-eclipse-error/24277595#24277595 –

관련 문제