该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
代码是这样的
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBC {
private Connection conn = null;
public void getConnection() {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=ContentManagementSystem";
String USER = "sa";
String PASSWORD = "392703542";
conn = DriverManager.getConnection(URL, USER, PASSWORD);
} catch (java.lang.ClassNotFoundException ce) {
System.out.println("ClassNotFound");
ce.printStackTrace();
} catch (java.sql.SQLException se) {
System.out.println("SQLException");
se.printStackTrace();
} catch (Exception e) {
System.out.println("Exception");e.printStackTrace();
}
}
public void testConnection() {
if (conn == null)this.getConnection();
try {
String sql = "select * from users";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.print(rs.getInt("username") + " ");
}
rs.close();
stmt.close();
} catch (SQLException e) {
System.out.println("cuole");
e.printStackTrace();
} finally {
if (conn != null)
try {
conn.close();
} catch (SQLException e) {}
}
}
public static void main(String[] args) {
JDBC bean = new JDBC();
bean.testConnection();
}
}
//我用断点调试了一下,执行到Statement stmt = conn.createStatement();就没反应了,什么提示也没有