📄 dataprovider.java
字号:
package com.blue.web.security.jaas;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Properties;
final public class DataProvider {
private String driver = null;
private String url = null;
private String user = null;
private String password = null;
public DataProvider(String driver, String url, String user, String password) {
this.driver = driver;
this.url = url;
this.user = user;
this.password = password;
}
public Connection getDBConnection() throws Exception {
Connection conn = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
System.out.println("ERROR: Get database connection. " + e);
throw e;
}
return conn;
}
public void closeConnection(Connection conn) throws Exception {
try {
if (conn != null && !conn.isClosed()) {
conn.close();
}
} catch (SQLException e) {
throw e;
}
}
public void closeResultSet(ResultSet rst) throws Exception {
try {
if (rst != null)
rst.close();
} catch (SQLException e) {
throw e;
}
}
public void closeStatement(PreparedStatement stat) throws Exception {
try {
if (stat != null)
stat.close();
} catch (SQLException e) {
throw e;
}
}
public boolean validateUser(String username, String password) throws Exception {
boolean result = false;
Connection conn = null;
CallableStatement stmt = null;
String sql = "{ ? = call dbo.users_ValidateUser(?, ?) }";
try {
conn = getDBConnection();
stmt = conn.prepareCall(sql);
stmt.registerOutParameter(1, Types.INTEGER); // The output parameter.
stmt.setString(2, username);
stmt.setString(3, password);
stmt.execute();
int status = stmt.getInt(1);
System.out.println("validateUser().status=" + status);
if (status == 1)
result = true;
} catch (SQLException e) {
e.printStackTrace();
throw e;
} finally {
closeStatement(stmt);
closeConnection(conn);
}
return result;
}
/**
* Connects to the user role's datasource, retrieves all the roles a given
* user belongs to, and add them to the curret IPrincipal. The roles are retrieved
* from the datasource or from an encrypted cookie.
*
* @param username
* @return
* @throws Exception
*/
public ArrayList getRolesForUser(String username) throws Exception {
ArrayList coll = new ArrayList();
Connection conn = null;
CallableStatement stmt = null;
ResultSet rst = null;
String sql = "{ ? = call dbo.roles_GetRolesForUser(?) }";
try {
conn = getDBConnection();
stmt = conn.prepareCall(sql);
stmt.registerOutParameter(1, Types.INTEGER); // The output parameter.
stmt.setString(2, username);
rst = stmt.executeQuery();
//int status = -1;
//if (stmt.getMoreResults()) {
// status = stmt.getInt(1);
//}
//System.out.println("getRolesForUser().status=" + status);
//if (status != 0) // 用户名不存在
// return coll;
while (rst.next()) {
coll.add(rst.getString(1));
}
} catch (SQLException e) {
e.printStackTrace();
throw e;
} finally {
closeStatement(stmt);
closeConnection(conn);
}
return coll;
}
public static void main(String[] args) {
Properties prop = System.getProperties();
Enumeration keys = prop.keys();
while (keys.hasMoreElements()) {
Object obj = keys.nextElement();
String value = prop.getProperty((String) obj);
System.out.println("System.getProperty(\"" + obj + "\") = \"" + value + "\"");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -