我改成jdbc:mysql://localhost//test也是不行
test是我建立的一个数据库

解决方案 »

  1.   

    借问一句,你是在什么环境中执行的?另外,你可以在catch中使用e.printStackTrace()看是Driver Class没有加载还是URL错误,然后再下结论。
      

  2.   

    try{
    String DBUser="  ";
    String DBPassword="  ";
    String DBServer="  ";
        String DBName="  ";
    Class.forName("org.gjt.mm.mysql.Driver").newInstance();
    con=java.sql.DriverManager.getConnection("jdbc:mysql://"+DBServer+":3306/"+DBName+"?useUnicode=true&characterEncoding=gb2312", DBUser,DBPassword);
    }catch(ClassNotFoundException cnfe)
    {
    error="class no found exception ";
    throw new ClassNotFoundException(error);
    }catch(SQLException sqle)
    {
    error=" sql error in connection ";
    throw new SQLException(error);
    }catch(Exception e)
    {
    error="unknow exception in connection";
    throw new Exception(error);
    }