亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么類加載驅動失敗 conn為null , Class.forName(driverClassName);加載方式是否有問題?

為什么類加載驅動失敗 conn為null , Class.forName(driverClassName);加載方式是否有問題?

慕粉3430898 2018-01-21 17:22:50
package com.iok.jee.dbc;import com.mysql.jdbc.*;import java.sql.*;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.Statement;import java.util.Properties;import ?java.sql.DriverManager;/*** Created by Administrator on 2018/1/20 0020.*/public class Dbutils {? ?private static Properties ? properties;? ?private static String ?driverClassName;? ?private static String url;? ?private static String userName;? ?private static String password;? ?static {? ? ? ?try {? ? ? ? ? ?properties = new Properties();? ? ? ? ? ?properties.load(Dbutils.class.getClassLoader().getResourceAsStream("jdbc.properties"));? ? ? ? ? ?driverClassName = properties.getProperty("driverClassName");? ? ? ? ? ?url = properties.getProperty("url");? ? ? ? ? ?userName = properties.getProperty("userName");? ? ? ? ? ?password = properties.getProperty("password");? ? ? ? ? ?Class.forName(driverClassName);/*加載驅動*/? ? ? ? /* ? DriverManager.registerDriver(new com.mysql.jdbc.Driver());*/? ? ? ?}catch (Exception e){? ? ? ? ? ?throw ?new ExceptionInInitializerError("類加載驅動失??!");? ? ? ?}? ?}? ?/*連接驅動*/? ?public ?static Connection getConnection(){? ? ? ? ?Connection conn=null;? ? ? ?try{? ? ? ? conn = ? DriverManager.getConnection(url,userName,password);? ? ? ?}catch (SQLException e){? ? ? ? ? ?e.printStackTrace();;? ? ? ?}? ? ? ?return ?conn;? ?}? ?/*獲取語句對象*/? ?public ?static java.sql.Statement getStatment(java.sql.Connection ?conn){? ? ? ?java.sql.Statement stat=null;? ? ? ?if(conn==null){? ? ? ? ? ?throw ?new IllegalArgumentException("連接對象為空無法獲取語句對象");? ? ? ?}? ? ? ?try{? ? ? ? ? ?stat= conn.createStatement();? ? ? ?}catch (SQLException e){? ? ? ? ? ? ?e.printStackTrace();;? ? ? ?}? ? ? ? ? ? return ?stat;? ? }/*獲取預編譯語句對象*/? ?public ?static java.sql.PreparedStatement getPreparedStatement(java.sql.Connection ?conn,String sql){? ? ? ? PreparedStatement ?preparedStat=null;? ? ? ?if(conn==null){? ? ? ? ? ?throw ?new IllegalArgumentException("連接對象為空無法獲取預編譯語句對象");? ? ? ?}? ? ? ?if(sql==null||"".equals(sql)){? ? ? ? ? ?throw ?new IllegalArgumentException("sql語句為空無法獲取預編譯語句對象");? ? ? ?}? ? ? ?try{? ? ? ? ? ?preparedStat= conn.prepareStatement(sql);? ? ? ?}catch (SQLException e){? ? ? ? ? ?e.printStackTrace();;? ? ? ?}? ? ? ?return ?preparedStat;? ?}? ?public ?static ?void ?closeConnection ?(java.sql.Connection conn){? ? ? ? ? try{? ? ? ? ? ? ? if(conn!=null){/*如果連接不為空*/? ? ? ? ? ? ? ? ? if(!conn.isClosed()){/*如果連接未關閉*/? ? ? ? ? ? ? ? ? ? ? conn.close();/*關閉連接*/? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? }? ? ? ? ? }catch (SQLException e){? ? ? ? ? ? ? e.printStackTrace();? ? ? ? ? }? ? ? ? ? }? ?/*關閉語句對象*/? ?public ?static ?void ?closeStatement (java.sql.Statement stat){? ? ? ?try{? ? ? ? ? ?if(stat!=null){/*如果連接不為空*/? ? ? ? ? ? ? ?if(!stat.isClosed()){/*如果連接未關閉*/? ? ? ? ? ? ? ? ? ?stat.close();/*關閉連接*/? ? ? ? ? ? ? ?}? ? ? ? ? ?}? ? ? ?}catch (SQLException e){? ? ? ? ? ?e.printStackTrace();? ? ? ?}? ?}? ?/*關閉結果集*/? ?public ?static ?void ?closeResult (ResultSet rs){? ? ? ?try{? ? ? ? ? ?if(rs!=null){/*如果連接不為空*/? ? ? ? ? ? ? ?if(!rs.isClosed()){/*如果連接未關閉*/? ? ? ? ? ? ? ? ? ?rs.close();/*關閉連接*/? ? ? ? ? ? ? ?}? ? ? ? ? ?}? ? ? ?}catch (SQLException e){? ? ? ? ? ?e.printStackTrace();? ? ? ?}? ?}}添加回答2回答
查看完整描述

目前暫無任何回答

  • 0 回答
  • 0 關注
  • 1424 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號