import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Test {
private static Connection dbConn = null;
public static void main(String[] args) {
String dbURL = "jdbc:sqlserver://192.168.1.125:1433;DatabaseName=sa";
try {
//1.加载驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("加载驱动成功!");
//2.连接
dbConn = DriverManager.getConnection(dbURL, "sa", "123");
System.out.println("连接数据库成功!");
String sql="select * from school";
PreparedStatement statement=null;
statement=dbConn.prepareStatement(sql);
ResultSet res = statement.executeQuery();
while(res.next()){
String title=res.getString("name");
System.out.println(title);
}
}catch(Exception e) {
e.printStackTrace();
System.out.println("连接数据库失败!");
}
}
}
以上是我的代码
代码是正确的也能识别出来,但是运行的时候就一直在报下面这个错误,是不是版本之类的问题呀?求求大佬帮我看看!感谢!
Exception in thread "main" java.lang.UnsupportedClassVersionError: com/microsoft/sqlserver/jdbc/SQLServerDriver has been compiled by a more recent version of the Java Runtime (class file version 59.0), this version of the Java Runtime only recognizes class file versions up to 52.0