Java获得指定ClassLoader所加载的类列表
一直想实现像DriverManager那样的getConnection方法,通过此方法就可以获得连接,而且还不用指定Driver,但前提是Driver已经通过Class.forName()或new Driver()进行了加载。今天终于得以实现。
package quiz;
import java.lang.reflect.Field;
public class ClassLoaderDriver {
/**
* @param args
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException, ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
Field field = ClassLoader.class.getDeclaredField("classes");
field.setAccessible(true);//设置该成员变量为可访问
System.out.println(field.get(ClassLoader.getSystemClassLoader()));
}
}
这样一来,我们就可以实现和DriverManager类似的功能了。
相关文档:
Java NIO API详解
在JDK
1.4以前,Java的IO操作集中在java.io这个包中,是基于流的阻塞(blocking)API。对于大多数应用来说,这样的API使用很方
便,然而,一些对性能要求较高的应用,尤其是服务端应用,往往需要一个更为有效的方式来处理IO。从JDK 1.4起,NIO
API作为一个基于缓冲区,并能提供非阻塞(non-blo ......
在MVC中配置编码filter可以解决中文乱码问题
--------------------------------------------------------------------------------------------------------------------------------------------------
在web.xml中加入如下filter代码:
<!--Form content submit encoding filter start-->
& ......
下面是用Java实现今天、昨天、前天的日期小例子,大家可以试试,有什么问题我们互相讨论。
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class TestStr {
public static void main(String[] args) {
getDates();
}
public static void getDates() ......
package com.svse.dao;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class UserDAO {
/*
* java调用存储过程
*/
public int addUser(String username,int userage)
{
Connection conn = null;
int useri ......
本文讲述程序开发者怎样使用NetBeans 6.8 IDE和JavaFX技术创建他们的第一个JavaFX应用程序。在文章中,我们将创建一个简单的带有文本的球体。该球体在一个特定的时间周期内改变其透明度。你还可以使用鼠标拖动球体。
同样的原因,因为文内有很多操作截图,这里插入很不方便, ......