出现这个异常原因可能很多:
1.编码问题
2.没有加载类驱动:换一种数据库的连接方式,就忘了这个:Class.forName(driverclass);
1 import java.io.IOException; 2 import java.io.InputStream; 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8 import java.util.Properties; 9 10 public class JdbcUtil { 11 private static String driverclass; 12 private static String url; 13 private static String user; 14 private static String password; 15 static{ 16 try{ 17 ClassLoader cl=JdbcUtil.class.getClassLoader(); 18 19 InputStream in=cl.getResourceAsStream("dbbcfg.properties"); 20 Properties props=new Properties(); 21 props.load(in); 22 23 driverclass=props.getProperty("driverClass"); 24 url=props.getProperty("url"); 25 user=props.getProperty("user"); 26 password=props.getProperty("password"); 27 28 29 in.close(); 30 }catch (Exception e){ 31 e.printStackTrace(); 32 } 33 try { 34 Class.forName(driverclass); 35 }catch (ClassNotFoundException e){ 36 e.printStackTrace(); 37 } 38 } 39 public static Connection getConnection(){ 40 try { 41 Connection conn = DriverManager.getConnection(url,user,password); 42 return conn; 43 } catch (Exception e) { 44 throw new RuntimeException("链接数据库的url或用户名密码错误,请检查您的配置文件"); 45 } 46 } 47 public static void release(ResultSet rs,Statement stmt,Connection conn){ 48 if(rs!=null){ 49 try { 50 rs.close(); 51 } catch (SQLException e) { 52 e.printStackTrace(); 53 } 54 rs = null; 55 } 56 if(stmt!=null){ 57 try { 58 stmt.close(); 59 } catch (SQLException e) { 60 e.printStackTrace(); 61 } 62 stmt = null; 63 } 64 if(conn!=null){ 65 try { 66 conn.close(); 67 } catch (SQLException e) { 68 e.printStackTrace(); 69 } 70 conn = null; 71 } 72 } 73 public static void main(String[] args){ 74 System.out.println("Test connection"); 75 try{ 76 Connection conn=JdbcUtil.getConnection(); 77 System.out.println(conn); 78 79 }catch (Exception e){ 80 e.printStackTrace(); 81 } 82 83 } 84 }