我自己写了个数据库连接池(还没写完)在jcreator下编译的时候遇到下面这个问题而不能通过,请高手指教:
Note: C:\Tomcat 5.5\webapps\book\src\DBConnectionPool.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 errorProcess completed.Tasklist中description为:
missing return statement(最后一行)为什么会这样,难道非得返回statement吗 返回Connection 不行吗? 以下是原程序:
DBConnectionPool.javaimport java.io.*;
import java.util.*;
import java.sql.*;public class DBConnectionPool implements ConnectionPool {
private static Vector pool;
private final int POOL_MAX_SIZE = 10;
private static String url=null;
private static String user=null;
private static String password=null;
private static String DBDriver=null;
/**
*读取数据库配置信息
*@
*@throws IOException
*/
public DBConnectionPool(){
try{
Properties properties = new Properties();
properties.load(new FileInputStream("DataBase.properties"));
url = properties.getProperty("url");
user = properties.getProperty("user");
password = properties.getProperty("password");
DBDriver = properties.getProperty("DBDriver");
}catch (IOException ie){
ie.printStackTrace();
}
}
/*
*获取数据库连接
*如果当前池中有可用连接,则将池中最后一个返回,如果没有,则新建一个
*/
public synchronized Connection getConnection(){
if (pool==null){
pool = new Vector();
}
Connection conn;
if (pool.isEmpty()){
conn = createConnection();
}else{
int last_index = pool.size()-1;
conn = (Connection) pool.get(last_index);
pool.remove(pool.get(last_index));
}
ConnectionHandler connHandler = new ConnectionHandler(this);
return connHandler.bind(conn);
}
/*将使用完毕的数据库连接放回备用池
*
*判断当前池中的连接数是否已经超过阀值(POOL_MAX_SIZE)
*如果超过,则关闭连接
*否则放回池中以备下次重用
*/
public synchronized void releaseConnection(Connection conn){
if (pool.size()>POOL_MAX_SIZE){
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}else{
pool.add(conn);
}
}
/**
*读取数据库配置信息,并从数据库连接池中获得数据库连接
*
*@return conn
*@throws DBException
*/
private static Connection createConnection(){
//Connection conn;
if(url!=null&user!=null&password!=null&DBDriver!=null){
try {
Driver driver = (Driver)Class.forName(DBDriver).newInstance();
DriverManager.registerDriver(driver);
//conn= DriverManager.getConnection(url,user,password);
return DriverManager.getConnection(url,user,password);
}catch (Exception e){
if(e instanceof ClassNotFoundException){
System.out.println("ClassNotFoundException when loading JDBC Driver"+e);
}else{
System.out.println("SQLException when loading JDBC Driver"+e);
}
}
}else{
System.out.println("尚未加载数据库配置信息");
}
}
}
ConnectionHandler.java
import java.util.*;
import java.sql.Connection;
import java.lang.reflect.*;public class ConnectionHandler implements InvocationHandler{
Connection dbconn;
ConnectionPool pool;
public ConnectionHandler(ConnectionPool connPool){
this.pool= connPool;
}
/**
*将动态代理绑定到指定Connection
*@param conn
*@return 绑定代理的Connection
*/
public Connection bind(Connection conn){
this.dbconn = conn;
Connection proxyConn = (Connection) Proxy.newProxyInstance(
conn.getClass().getClassLoader(),conn.getClass().getInterfaces(),this);
return proxyConn;
}
/*
*方法调用拦截器
*
*判断当前调用的方法是否是"close"方法
*如是,调用poo.releaseConnection方法作为标准close方法替代
*/
public Object invoke(Object proxy,Method method,Object[] args)throws Throwable{
Object obj= null;
//如果调用的是close方法,则用pool.releaseConnection方法将其替换
if("close".equals(method.getName())){
pool.releaseConnection(dbconn);
}else{
obj = method.invoke(dbconn,args);
}
return obj;
}
}ConnectionPool.javaimport java.sql.*;
import javax.sql.*;public interface ConnectionPool{
public Connection getConnection();
public void releaseConnection(Connection conn);
}
Note: C:\Tomcat 5.5\webapps\book\src\DBConnectionPool.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 errorProcess completed.Tasklist中description为:
missing return statement(最后一行)为什么会这样,难道非得返回statement吗 返回Connection 不行吗? 以下是原程序:
DBConnectionPool.javaimport java.io.*;
import java.util.*;
import java.sql.*;public class DBConnectionPool implements ConnectionPool {
private static Vector pool;
private final int POOL_MAX_SIZE = 10;
private static String url=null;
private static String user=null;
private static String password=null;
private static String DBDriver=null;
/**
*读取数据库配置信息
*@
*@throws IOException
*/
public DBConnectionPool(){
try{
Properties properties = new Properties();
properties.load(new FileInputStream("DataBase.properties"));
url = properties.getProperty("url");
user = properties.getProperty("user");
password = properties.getProperty("password");
DBDriver = properties.getProperty("DBDriver");
}catch (IOException ie){
ie.printStackTrace();
}
}
/*
*获取数据库连接
*如果当前池中有可用连接,则将池中最后一个返回,如果没有,则新建一个
*/
public synchronized Connection getConnection(){
if (pool==null){
pool = new Vector();
}
Connection conn;
if (pool.isEmpty()){
conn = createConnection();
}else{
int last_index = pool.size()-1;
conn = (Connection) pool.get(last_index);
pool.remove(pool.get(last_index));
}
ConnectionHandler connHandler = new ConnectionHandler(this);
return connHandler.bind(conn);
}
/*将使用完毕的数据库连接放回备用池
*
*判断当前池中的连接数是否已经超过阀值(POOL_MAX_SIZE)
*如果超过,则关闭连接
*否则放回池中以备下次重用
*/
public synchronized void releaseConnection(Connection conn){
if (pool.size()>POOL_MAX_SIZE){
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}else{
pool.add(conn);
}
}
/**
*读取数据库配置信息,并从数据库连接池中获得数据库连接
*
*@return conn
*@throws DBException
*/
private static Connection createConnection(){
//Connection conn;
if(url!=null&user!=null&password!=null&DBDriver!=null){
try {
Driver driver = (Driver)Class.forName(DBDriver).newInstance();
DriverManager.registerDriver(driver);
//conn= DriverManager.getConnection(url,user,password);
return DriverManager.getConnection(url,user,password);
}catch (Exception e){
if(e instanceof ClassNotFoundException){
System.out.println("ClassNotFoundException when loading JDBC Driver"+e);
}else{
System.out.println("SQLException when loading JDBC Driver"+e);
}
}
}else{
System.out.println("尚未加载数据库配置信息");
}
}
}
ConnectionHandler.java
import java.util.*;
import java.sql.Connection;
import java.lang.reflect.*;public class ConnectionHandler implements InvocationHandler{
Connection dbconn;
ConnectionPool pool;
public ConnectionHandler(ConnectionPool connPool){
this.pool= connPool;
}
/**
*将动态代理绑定到指定Connection
*@param conn
*@return 绑定代理的Connection
*/
public Connection bind(Connection conn){
this.dbconn = conn;
Connection proxyConn = (Connection) Proxy.newProxyInstance(
conn.getClass().getClassLoader(),conn.getClass().getInterfaces(),this);
return proxyConn;
}
/*
*方法调用拦截器
*
*判断当前调用的方法是否是"close"方法
*如是,调用poo.releaseConnection方法作为标准close方法替代
*/
public Object invoke(Object proxy,Method method,Object[] args)throws Throwable{
Object obj= null;
//如果调用的是close方法,则用pool.releaseConnection方法将其替换
if("close".equals(method.getName())){
pool.releaseConnection(dbconn);
}else{
obj = method.invoke(dbconn,args);
}
return obj;
}
}ConnectionPool.javaimport java.sql.*;
import javax.sql.*;public interface ConnectionPool{
public Connection getConnection();
public void releaseConnection(Connection conn);
}
在java中,如果一个方法指定了返回值,那它就一定要有返回值(也可以不返回但要抛出异常)。
这条语句返回connection(返回值)的啊??