我利用myeclipse连接mysql,但是怎么都要报以下错误,请问是怎么回事啊?数据库和驱动都是5.1*的啊!!java.sql.SQLException: No suitable driver found for jdbc:mysql//localhost/test
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.test.Test.test1(Test.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:79)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

解决方案 »

  1.   

    你为什么没有写端口号,是默认的吗 ??
    还有  JDBC:MySql://localhost:3306/test你的mysql后面少个冒号!!!
      

  2.   

    应该是你连接数据库的url出错了
      

  3.   

    package com.neusoft.test.m.db.tools;import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.Properties;public class DBUtils {
    private static DBUtils d = new DBUtils();
    private static Properties p = new Properties();
    static{
    try {
    p.load(d.getClass().getResourceAsStream("init.properties"));
    } catch (IOException e) {
    System.out.println("加载配置文件的时候出错了" + e.getMessage());
    }
    }
    /**
     * 与数据库建立链接
     */
    public static Connection getConnection(){
    Connection con = null;
    String dbtype= p.getProperty("dbtype");
    String dbname = p.getProperty("dbname");
    String username = p.getProperty("username");
    String password = p.getProperty("password");
    String ip = p.getProperty("ip");
    String port =p.getProperty("port");
    if(dbtype.equalsIgnoreCase("oracle")){
    try {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    } catch (ClassNotFoundException e) {
    System.out.println("驱动类未找到,注意:驱动包是否导入了" + e.getMessage());

    String url="jdbc:oracle:thin:@"+ip+":"+port+":"+dbname; 
    try {
    con = DriverManager.getConnection(url,username,password);
    } catch (SQLException e) {
    System.out.println("数据库链接失败,配置项有异常" + e.getMessage());

    }
    if(dbtype.equalsIgnoreCase("mysql")){
    try {
    Class.forName("org.gjt.mm.mysql.Driver");
    } catch (ClassNotFoundException e) {
    System.out.println("驱动类未找到,注意:驱动包是否导入了" + e.getMessage());

    String url ="jdbc:mysql://"+ip+":"+port+"/"+dbname+"?user="+username+"&password="+password+"&useUnicode=true&characterEncoding=utf-8" ;
    //myDB为数据库名 
    try {
    con = DriverManager.getConnection(url);
    } catch (SQLException e) {
    System.out.println("数据库链接失败,配置项有异常" + e.getMessage());

    }
    if(dbtype.equalsIgnoreCase("sqlserver")){
    try {
    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
    } catch (ClassNotFoundException e) {
    System.out.println("驱动类未找到,注意:驱动包是否导入了" + e.getMessage());

    String url="jdbc:microsoft:sqlserver://"+ip+":"+port+";DatabaseName="+dbname; 
    try {
    con= DriverManager.getConnection(url,username,password);
    } catch (SQLException e) {
    System.out.println("数据库链接失败,配置项有异常" + e.getMessage());

    }
    return con;

    }
    /**
     * 断开与数据库的链接
     */
    public static void close(Connection con,PreparedStatement pstam,ResultSet rs){
    if(con != null){
    try {
    con.close();
    } catch (SQLException e) {
    System.out.println(e.getMessage());
    }
    }
    if(pstam != null){
    try {
    pstam.close();
    } catch (SQLException e) {
    System.out.println(e.getMessage());
    }
    }
    if(rs != null){
    try {
    rs.close();
    } catch (SQLException e) {
    System.out.println(e.getMessage());
    }
    }
    }
    }
    init.properties
    dbtype = mysql  //数据库类型
    dbname = test  //数据库名
    username = root //数据库登录名
    password =root //数据库登录密码
    ip = localhost  //数据库所在机器的ip
    port = 3306  // 端口号
      

  4.   

    谢谢大家的回答,问题已经解决了,原因就是我在jdbc:mysql://localhost/test"中的mysql后面少了一个冒号,希望能给大家一个前车之鉴!!