黑马程序员技术交流社区

标题: 一个Java运行异常 [打印本页]

作者: 晏敏    时间: 2014-4-10 21:00
标题: 一个Java运行异常
本帖最后由 晏敏 于 2014-4-10 21:57 编辑

做了一个单例的JdbcUtilsSing类,其中有个关闭资源的static方法
public void free(ResultSet rs,PreparedStatement ps,Connection conn){
                try {
                        if(rs!=null)
                                rs.close();
                } catch(SQLException e){
                        e.printStackTrace();
                }finally{
                        try{
                                if(ps!=null)
                                        ps.close();
                        }catch(SQLException e){
                                e.printStackTrace();
                        }finally{
                                try{
                                        if(conn!=null)
                                                conn.close();
                                        }catch(SQLException e){
                                                e.printStackTrace();
                                        }
                        }
                }
        }
然后在另一个类中调用JdbcUtilsSing.getInstance().free(rs,ps,conn)方法,可是显示
Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class com.ymocean.JdbcUtilsSing

其他包写的别的程序能运行,classpath应该没问题,但是这个包中的程序只要调用了free()都出这个错误。请各位帮忙看一下。
ps:附上此单例程序
package com.ymocean;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public final class JdbcUtilsSing {
        private String url="jdbc:sqlserver://localhost:1433;databaseName=test";
        private String user="ocean";
        private String password="";
        private static JdbcUtilsSing instance=null;
        
        private JdbcUtilsSing(){
               
        }
        
        public static JdbcUtilsSing getInstance(){
                if(instance==null){
                        synchronized (JdbcUtilsSing.class) {
                                if(instance==null){
                                        instance=new JdbcUtilsSing();
                                }
                        }
                }
                return instance;
        }
        
        static{
                try {
                        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                } catch (ClassNotFoundException e) {
                        // TODO: handle exception
                        throw new ExceptionInInitializerError(e);
                }
        }
        
        public Connection getConnection() throws SQLException{
                return DriverManager.getConnection(url,user,password);
        }
        
        public void free(ResultSet rs,PreparedStatement ps,Connection conn){
                try {
                        if(rs!=null)
                                rs.close();
                } catch(SQLException e){
                        e.printStackTrace();
                }finally{
                        try{
                                if(ps!=null)
                                        ps.close();
                        }catch(SQLException e){
                                e.printStackTrace();
                        }finally{
                                try{
                                        if(conn!=null)
                                                conn.close();
                                        }catch(SQLException e){
                                                e.printStackTrace();
                                        }
                        }
                }
        }
        
        
}



好吧 ,我终于知道了,我没引数据库包(前两天删了Eclipse,又装的MyEclipse,一直以为还是有数据库包,- -!)





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2