這期內容當中小編將會給大家帶來有關java中Class.forName方法的作用是什么,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
Java的特點有哪些 1.Java語言作為靜態面向對象編程語言的代表,實現了面向對象理論,允許程序員以優雅的思維方式進行復雜的編程。 2.Java具有簡單性、面向對象、分布式、安全性、平臺獨立與可移植性、動態性等特點。 3.使用Java可以編寫桌面應用程序、Web應用程序、分布式系統和嵌入式系統應用程序等。
java中Class.forName的作用
連接數據庫幾大步.看以下代碼
import com.mysql.jdbc.Driver; import java.sql.*; public class JdbcDemo { public static void main(String[] args) throws SQLException, ClassNotFoundException { String url = "jdbc:mysql://127.0.0.1:3306/mydb"; String username = "root"; String password = "redhat"; Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection(url, username, password); String sql = "SELECT * FROM msg"; PreparedStatement prepareStatement = connection.prepareStatement(sql); ResultSet resultSet = prepareStatement.executeQuery(); resultSet.next(); String address = resultSet.getString("address"); System.out.println(address); } }
其中第一步,搞的我有點想不通.為啥Class.forName傳入了一段字符串之后,就知道我連接的數據庫是mysql? 有點不科學啊.Class.forName到底做了啥.下面就開始到源碼中,一探究竟.
@CallerSensitive public static Class forName(String className) throws ClassNotFoundException { Class caller = Reflection.getCallerClass(); return forName0(className, true, ClassLoader.getClassLoader(caller), caller); }
發現它調用了forName0方法,繼續跟蹤再看看
private static native Class forName0(String name, boolean initialize,
ClassLoader loader,
Class caller)
throws ClassNotFoundException;
native方法,源碼也只能到此結束了.看下官方文檔,怎么說吧.
發現官方文檔,還是描述的很清楚的.
Returns the Class object associated with the class or interface with the given string name,
using the given class loader.
Given the fully qualified name for a class or interface (in the same format returned by getName)
this method attempts to locate, load, and link the class or interface.
The specified class loader is used to load the class or interface.
If the parameter loader is null, the class is loaded through the bootstrap class loader.
The class is initialized only if the initialize parameter is true and if it has not been
initialized earlier.
嗯,描述的還算是很清楚.返回一個給定類或者接口的一個Class對象,如果沒有給定classloader,那么會使用根類加載器.如果initalize這個參數傳了true,那么給定的類如果之前沒有被初始化過,那么會被初始化.我們在JDBC第一步的時候,傳入的參數是com.mysql.jdbc.Driver. 也就是說這個類會被初始化.我們看一下這個類里面的內容.
public class Driver extends NonRegisteringDriver implements java.sql.Driver {
static {
try {
java.sql.DriverManager.registerDriver(new Driver());
} catch (SQLException E) {
throw new RuntimeException("Can't register driver!");
}
}
public Driver() throws SQLException {
// Required for Class.forName().newInstance()
}
}
我們發現這個類也是超級簡單的.一個構造函數和一個靜態代碼塊.我們知道,類在初始化的時候,靜態代碼塊的內容會被執行的.也就是說我們Class.forName和直接寫DriverManager.registerDriver(new Driver)兩者功能是等同的.我們換成這種寫法.再試試看.
public class JdbcDemo { public static void main(String[] args) throws SQLException, ClassNotFoundException { String url = "jdbc:mysql://127.0.0.1:3306/mydb"; String username = "root"; String password = "redhat"; //Class.forName("com.mysql.jdbc.Driver"); DriverManager.registerDriver(new Driver()); Connection connection = DriverManager.getConnection(url, username, password); String sql = "SELECT * FROM msg"; PreparedStatement prepareStatement = connection.prepareStatement(sql); ResultSet resultSet = prepareStatement.executeQuery(); resultSet.next(); String address = resultSet.getString("address"); System.out.println(address); } }
上述就是小編為大家分享的java中Class.forName方法的作用是什么了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。