JDBC连接数据库的问题
import java.io.IOException;
import java.sql.*;
public class JDBCDemo {
public static void main(String[] args) {
JDBCDemo demo = new JDBCDemo();
try {
demo.getBBC();
} catch(Exception e) {
e.printStackTrace();
}
}
private void getBBC() throws SQLException, IOException, Exception {
Statement stat = null;
Connection conn = null;
ResultSet set = null;
String sqlStr = "";
try {
sqlStr = "SELECT * FROM BBC";
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=Instant;"
+ "user=sa;password=hanxun";
conn = DriverManager.getConnection(url);
stat = conn.createStatement();
set = stat.executeQuery(sqlStr);
while (set.next()) {
System.out.println(set.getString("Name") + "-"
+ set.getString("Region") + "-" + set.getDouble("Area")
+ "-" + set.getDouble("BBCPopulation") + "-"
+ set.getDouble("Gdp"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
set.close();
stat.close();
conn.close();
}
}
}
上面是我的一个用JDBC连接SQL SERVER 2008的例子,运行的时候报出如下信息:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at yidu.nimblewind.test.JDBCDemo.main(JDBCDemo.java:8)
我就郁闷了,我用的是sqljdbc4.jar包,放到了jre\lib\ext里面,为什么会出这么个错误呢,还请大家指教,谢谢了。