日期:2014-05-20 浏览次数:20980 次
1.
//普通的连接类
public class ConnectionManager {
/**
* 得到数据库连接
*
* @return 数据库连接对象
* @throws ClassNotFoundException
* @throws SQLException
*/
public static Connection getCon() {
try {
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
Class.forName(driver);
return DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=zf", "sa", "sa");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 关闭所有连接
* @param con 数据库连接
* @param ps
* @param rs
*/
public static void closeAll(Connection con, PreparedStatement ps,
ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (con != null && !con.isClosed()) {
con.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//代理的连接对象
public class ConnectionAdvice implements InvocationHandler {
//获取真实的对象
private Connection realCon;
public ConnectionAdvice(Connection realCon) {
// TODO Auto-generated constructor stub
this.realCon=realCon;
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
// TODO Auto-generated method stub
//获取方法名
String methodName=method.getName();
//判断是否为关闭的方法
if("close".equals(methodName)){
System.out.println("代理该方法");
//将代理对象放到池子中
ConnectionPool.restore((Connection)proxy);
return null;
}else{
//如果不是则用原有连接执行原有的方法
return method.invoke(realCon, args);
}
}
}
//代理工厂通过真实对象创建代理对象
public class ProxyFactory {
//通过真实的对象获取代理对象
public static Connection create(Connection realCon){
//通过代理类Proxy
Connection proxy=(Connection) Proxy.newProxyInstance(
//类加载器
realCon.getClass().getClassLoader(),
//获取它的所有接口
realCon.getClass().getInterfaces(),
//调用处理器
new ConnectionAdvice(realCon));
return proxy;
}
}
//连接池
public class ConnectionPool {
//用LinkedList集合存取连接
private static List<Connection> conList=new LinkedList<Connection>();
//最大连接数
private static final int Total=10;
private static int curNum=5;//最小连接数
static{
for (int i = 0; i < 5; i++) {
//获取代理的连接
Connection proxy=ProxyFactory.create(ConnectionManager.getCon());
//添加到集合中
conList.add(proxy);
}
}
//从池子中获取连接的方法
public static synchronized Connection getCon(){
Connection proxyCon=null;
//如果集合中有连接
if(conList.size()>0){
if(conList.size()<=5){
//从集合中获取一个连接
proxyCon=conList.get(0);
//从集合中删除该连接
conList.remove(0);
return proxyCon;
}else{
curNum-=5;
for (int i = 0; i < 5; i++) {
conList.remove(0);
}
proxyCon=conList.get(0);
//从集合中删除该连接
conList.remove(0);
return proxyCon;
}
}else{
//如果集合中没有连接了
//当前总数小于最大连接数,则增加5
if(curNum<Total){
curNum+=5;
for (int i = 0; i < 5; i++) {
//获取代理的连接
Connection proxy=ProxyFactory.create(ConnectionManager.getCon());
//添加到集合中
conList.add(proxy);
}
proxyCon=conList.get(0);
//从集合中删除该连接
conList.remove(0);
return proxyCon;
}else{
//如果等于最大连接数,则抛出异常让用户等待
try {
throw new Exception("连接已经满,请等待!");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
}
//放回到池子中!
public static synchronized void restore(Connection proxyCon){
conList.add(proxyCon);
}
}
2.
//写一个方法求一个字符串里面的每个字符和它的个数
public void show(String s){
if(s!=null){
for (int i = 0; i < s.length(); i++) {
System.out.println(s.substring(i, i+1));
}
System.out.println("它的个数为:"+s.length());
}else{
System.out.println("为NULL");
}
}
3.不会
4.没看明白
5.
/**
* 用非递归方法实现下列函数的值。
f(1)=1
f(2)=2
f(n)=f(n-1)+f(n-2)
*/
public int fun(int n){
if(n<=0)
return -1;//表示异常
if(n==1)
return 1;
if(n==2)
return 2;
int[] num=new int[n];
num[0]=1;
num[1]=2;
//从第三个数开始计算
for (int i = 2; i < num.length; i++) {
num[i]=num[i-1]+num[i-2];
}
return num[n-1];
}