?? jdbcsubscriberdao.java
字號:
package jsfdb.model.dao;
import jsfdb.model.LoginInfo;
import jsfdb.model.Subscriber;
import jsfdb.model.ModelUtils;
import jsfdb.model.err.IncorrectPasswordException;
import jsfdb.model.err.LoginException;
import jsfdb.model.err.ProfileException;
import jsfdb.model.err.SubscribeException;
import jsfdb.model.err.UnknownSubscriberException;
import jsfdb.model.err.UnsubscribeException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBCSubscriberDAO implements SubscriberDAO {
// private DataSource dataSource;
public JDBCSubscriberDAO() {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
private void close(Connection conn, PreparedStatement ps)
throws SQLException {
if (ps != null)
ps.close();
if (conn != null)
conn.close();
}
public Subscriber select(LoginInfo loginInfo)
throws LoginException,
UnknownSubscriberException,
IncorrectPasswordException {
Connection conn = DriverManager.getConnection("jdbc:odbc:jsfdb");
PreparedStatement ps = null;
try {
conn = dataSource.getConnection();
ps = conn.prepareStatement(
ModelUtils.getResource("SelectStatement"));
ps.setString(1, loginInfo.getEmail());
ResultSet rs = ps.executeQuery();
if (!rs.next())
throw new UnknownSubscriberException();
String password = rs.getString(1);
if (!loginInfo.getPassword().equals(password))
throw new IncorrectPasswordException();
Subscriber subscriber = new Subscriber();
subscriber.setEmail(loginInfo.getEmail());
subscriber.setPassword(loginInfo.getPassword());
subscriber.setName(rs.getString(2));
subscriber.setManager(rs.getBoolean(3));
subscriber.setDeveloper(rs.getBoolean(4));
subscriber.setAdministrator(rs.getBoolean(5));
subscriber.setSubscriptionType(rs.getInt(6));
return subscriber;
} catch (SQLException x) {
ModelUtils.log(x);
throw new LoginException();
} finally {
try {
close(conn, ps);
} catch (SQLException x) {
ModelUtils.log(x);
throw new LoginException();
}
}
}
public void insert(Subscriber subscriber)
throws SubscribeException {
Connection conn = DriverManager.getConnection("jdbc:odbc:jsfdb");
PreparedStatement ps = null;
try {
conn = dataSource.getConnection();
ps = conn.prepareStatement(
ModelUtils.getResource("InsertStatement"));
ps.setString(1, subscriber.getEmail());
ps.setString(2, subscriber.getPassword());
ps.setString(3, subscriber.getName());
ps.setBoolean(4, subscriber.isManager());
ps.setBoolean(5, subscriber.isDeveloper());
ps.setBoolean(6, subscriber.isAdministrator());
ps.setInt(7, subscriber.getSubscriptionType());
int rowCount = ps.executeUpdate();
if (rowCount != 1)
throw new SubscribeException();
} catch (SQLException x) {
ModelUtils.log(x);
throw new SubscribeException();
} finally {
try {
close(conn, ps);
} catch (SQLException x) {
ModelUtils.log(x);
throw new SubscribeException();
}
}
}
public void update(Subscriber subscriber)
throws ProfileException {
Connection conn = DriverManager.getConnection("jdbc:odbc:jsfdb");
PreparedStatement ps = null;
try {
conn = dataSource.getConnection();
ps = conn.prepareStatement(
ModelUtils.getResource("UpdateStatement"));
ps.setString(1, subscriber.getPassword());
ps.setString(2, subscriber.getName());
ps.setBoolean(3, subscriber.isManager());
ps.setBoolean(4, subscriber.isDeveloper());
ps.setBoolean(5, subscriber.isAdministrator());
ps.setInt(6, subscriber.getSubscriptionType());
ps.setString(7, subscriber.getEmail());
int rowCount = ps.executeUpdate();
if (rowCount != 1)
throw new ProfileException();
} catch (SQLException x) {
ModelUtils.log(x);
throw new ProfileException();
} finally {
try {
close(conn, ps);
} catch (SQLException x) {
ModelUtils.log(x);
throw new ProfileException();
}
}
}
public void delete(Subscriber subscriber)
throws UnsubscribeException {
Connection conn = DriverManager.getConnection("jdbc:odbc:jsfdb");
PreparedStatement ps = null;
try {
conn = dataSource.getConnection();
ps = conn.prepareStatement(
ModelUtils.getResource("DeleteStatement"));
ps.setString(1, subscriber.getEmail());
int rowCount = ps.executeUpdate();
if (rowCount != 1)
throw new UnsubscribeException();
} catch (SQLException x) {
ModelUtils.log(x);
throw new UnsubscribeException();
} finally {
try {
close(conn, ps);
} catch (SQLException x) {
ModelUtils.log(x);
throw new UnsubscribeException();
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -