连接数据库(已SQL SERVER为例):
private static String username = "sa";
private static String password = "123";
private static String url = "jdbc:sqlserver://localhost:1433;DatabaseName=EmperorSystem";
private static String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
public static Connection getConnection() {
Connection con = null;
try {
Class.forName(driver);
con = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
查询数据条数: