2013-05-21 11:41:48.0|分类: jvm|浏览量: 1416
Class.forName( )静态方法的目的是为了动态加载类。在加载完成后,一般还要调用Class下的newInstance( )静态方法来实例化对象以便操作。因此,Class.forName( )是动态加载类是没有用的,其最终目的是为了实例化对象。调用 forName("X") 将导致命名为 X 的类被初始化。 package com.java.jvm; public class ClassTest { public static void main(String[] arg){ try { Class userClass = Class.forName("com.java.jvm.User"); System.out.println("userClass="+userClass); User user = (User) userClass.newInstance(); System.out.println("user="+user); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } }打印结果: userClass=class com.java.jvm.User user=User [name=null, age=null, address=null]Class.forName("")返回的是类(Class) Class.forName("").newInstance()返回的是object 其实和new 的关键字作用相同。 调用 forName("X") 将导致命名为 X 的类被初始化,这句话是官网API上的一句话,很迷茫,有点不理解,所以在User类中,添加了static语句块和User构造函数 package com.java.jvm; public class User { static{ System.out.println("static ---"); } public User(){ System.out.println("name="+name); name="xiaotian"; } private String name; private Integer age; private String address; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "User [name=" + name + ", age=" + age + ", address=" + address + "]"; } }结果打印: static --- userClass=class com.java.jvm.User name=null user=User [name=xiaotian, age=null, address=null] forName("X") 执行的时候,调用了User中的静态语句块,newInstance()执行的时候调用了构造函数。 连接数据库的简单代码 // 驱动程序名 String driver = "com.mysql.jdbc.Driver"; // URL指向要访问的数据库名student String url = "jdbc:mysql://127.0.0.1:3306/student"; // MySQL配置时的用户名 String user = "root"; // MySQL配置时的密码 String password = "root"; try { // 加载驱动程序 Class.forName(driver); // 连续数据库 Connection conn = DriverManager.getConnection(url, user, password); if(!conn.isClosed()) System.out.println("Succeeded connecting to the Database!"); // statement用来执行SQL语句 Statement statement = conn.createStatement(); // 要执行的SQL语句 String sql = "select * from student"; // 结果集 ResultSet rs = statement.executeQuery(sql);其中加载数据库驱动 Class.forName(driver);加载mysql驱动,执行static语句块,使将Driver对象注册到DriverManager中,然后上面就获得了数据库连接
package com.mysql.jdbc public class Driver extends NonRegisteringDriver implements java.sql.Driver { // ~ Static fields/initializers // --------------------------------------------- // A // Register ourselves with the DriverManager // static { t ry { //使将Driver对象注册到DriverManager中 java.sql.DriverManager.registerDriver(new Driver()); } catch (SQLException E) { throw new RuntimeException("Can't register driver!"); } } // ~ Constructors // ----------------------------------------------------------- /** * Construct a new driver and register it with DriverManager * * @throws SQLException * if a database error occurs. */ public Driver() throws SQLException { // Required for Class.forName().newInstance() } } |