日期:2014-05-20  浏览次数:20897 次

jdbc连接sql server2008报错
Java code

import java.sql.*;

public class JdbcSqlServer {

    public static void main(String[] args) throws Exception {
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        String connectionURL = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=TEST_DB_RENAME";    //为数据库的地址
        
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");    //加载驱动
        
        con = DriverManager.getConnection(connectionURL,"pengliang","1989923");    //建立与数据库的连接
    

        stmt = con.createStatement();
        
        
        rs = stmt.executeQuery("select * from student");    //ResultSet对象来存储查询的结果
    
        
            while(rs.next()) {
                System.out.println("***********************"
                        +"\n"+"sno:"+"\t"+rs.getString("sno")+"\t"+"sname:"+" \t"+rs.getString("sname")+"\t"+"sgrade:"+"\t"+rs.getString("sgrade")
                        );
            }
    
            con.close();
    
    }

}



显示的错误信息为
Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at JdbcSqlServer.main(JdbcSqlServer.java:11)


------解决方案--------------------
Java code

Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file

sql的驱动版本不对。。下个支持2008的正确版本就行了

------解决方案--------------------
版本问题,把你开发工具中的版本调整好,要不,就是你的jdk的包或.jar的包损坏了,你重新安装。