细节决定成败----连接池多了空格应发的错误

package com.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class JDBCConnection {
    private  String url="jbdc:mysql://localhost:3306/helloworld";
    private  String username="root";
    private  String password="";
    public  static Connection  conn=null;
    private static String className="com.mysql.jdbc.Driver";
    
    public  void connect(){
        try {
            Class.forName(className);
            
        } catch (ClassNotFoundException e) {
            System.out.println("加载驱动失败!");
            e.printStackTrace();
        }
        try {
            conn=DriverManager.getConnection(url, username, password);
        } catch (SQLException e) {
            System.out.println("连接数据库失败");
            e.printStackTrace();
        }
    }

}
package com.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class Connect {
    
    private  String url="jdbc:mysql://localhost:3306/helloworld";
    private  String username="root";
    private  String password="";
    private  String driver="com.mysql.jdbc.Driver";
   
    public Connection conn=null;
    public void getConnect(){
        try {
            Class.forName(driver);
            //System.out.println("加载驱动成功!");
            conn=DriverManager.getConnection(url,username,password);
            //System.out.println("数据库连接成功!");
        } catch (ClassNotFoundException e1) {
            e1.printStackTrace();
        } catch (SQLException e2) {
            e2.printStackTrace();
        }
    }
     /*public String getConnectMessage() {//获取此连接池基本信息
            String message="连接基本信息:\n"
                          +"driver: "+driver+",\n"
                          +"url: "+url+",\n"
                          +"username: "+username+",\n"
                          +"password: "+password;
            return message;
        }*/

}

上面两个连接池,我使用第一个的时候一直报错,连接数据库失败。no suitable jdbc Driver,conn=null,报空指针错误,然而我使用第二个连接池,同样的条件却完全可行,咋眼一看,没发现两者的不同之处,调试,对比,也没发现什么异常,最后才发现,前者在conn=DriverManager.getConnection(url,username,password);这里多了两个空格。

 

细节决定成败,小小的空格引发莫名的错误,检查错误的时候要根据异常信息,不应当胡乱从头检查,浪费时间

posted @ 2016-04-06 08:18  guodaxia  阅读(209)  评论(0编辑  收藏  举报