📄 jdbc.java
字号:
/*
* Jdbc Authentication Module:
* By Wayne Hogue <w.hogue@chiphead.net>
* Last Updated: 04.02.2000
* Based on
* Mysql Authentication Module:
* com.lyrisoft.auth.mysql
* By Leif Jackson <ljackson@jjcons.com>
*/
package com.lyrisoft.chat.server.remote.auth.jdbc;
import java.util.*;
import java.sql.*;
import com.lyrisoft.chat.server.remote.AccessDenied;
import com.lyrisoft.util.properties.*;
import com.lyrisoft.chat.server.remote.ChatServer;
public class Jdbc {
private Connection Conn = null;
private String Table = null;
private String IdField = null;
private String PasswordField = null;
private String AuthField = null;
private boolean CryptPassword = false;
Jdbc() {
try {
Properties p = PropertyTool.loadProperties("jdbcAuth.conf");
String JdbcClass = PropertyTool.getString("jdbc.Class", p);
String DBUrl = PropertyTool.getString("jdbc.DBUrl", p);
String DBUsername = PropertyTool.getString("jdbc.DBUsername", p);
String DBPassword = PropertyTool.getString("jdbc.DBPassword", p);
Table = PropertyTool.getString("jdbc.Table", p);
IdField = PropertyTool.getString("jdbc.IdField", p);
PasswordField = PropertyTool.getString("jdbc.PasswordField", p);
AuthField = PropertyTool.getString("jdbc.AuthField", p);
if(PropertyTool.getString("jdbc.CryptPassword", p).equalsIgnoreCase("true")){
CryptPassword = true;
}else{
CryptPassword = false;
}
Class.forName(JdbcClass).newInstance();
if(DBUsername == null){
Conn = DriverManager.getConnection(DBUrl);
}else{
Conn = DriverManager.getConnection(DBUrl,DBUsername,DBPassword);
}
ChatServer.log("JDBC initialized");
}catch (Exception e) {
ChatServer.log(e);
throw new RuntimeException(e.toString());
}
}
/**
* Gets a JdbcRecord.
*
* @param userId the user Id
* @param password the password in plaintext
* @return the PasswdRecord or null if the user was not found
* @throw AccessException if the user was found,
* but his password did not match the contents of the user record.
*/
public synchronized final JdbcRecord getRecord(String userId,
String password) throws SQLException, AccessDenied
{
Statement Stmt = null;
JdbcRecord r = null;
try {
Stmt = Conn.createStatement();
String query = "SELECT COUNT(*) FROM "+Table+" WHERE "+IdField+"='" + userId + "'";
System.out.println(query);
ResultSet RS = Stmt.executeQuery(query);
if (RS.next()) {
if (RS.getInt(1) == 0) {
// no user record
return null;
}
}
query = "select "+IdField+","+PasswordField+","+AuthField+" from "+Table+" where "+IdField+"='"+userId+"' ";
if(CryptPassword){
// don't know if this sworks with anything but mysql.
// may require a procedure on other databases
query += "and "+PasswordField+"=password('" + password + "')";
}else{
query += "and "+PasswordField+"='" + password + "'";
}
System.out.println(query);
RS = Stmt.executeQuery(query);
if(RS.next()){
r = new JdbcRecord(RS.getString(IdField),
RS.getInt(AuthField),
RS.getString(PasswordField));
return r;
} else {
throw new AccessDenied(userId);
}
} catch (SQLException E) {
ChatServer.log(E);
ChatServer.logError(" SQLException: " + E.getMessage());
ChatServer.logError(" SQLState: " + E.getSQLState());
ChatServer.logError(" VendorError: " + E.getErrorCode());
throw E;
} finally {
// always Clean Up
if (Stmt != null) {
try {
Stmt.close();
} catch (SQLException e) {
ChatServer.log(e);
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -