M
Marco_adv
Gast
Hallo ich habe datenbank zugriffe und eine connect klasse geschrieben connect sieht wie folgt aus:
die statments so:
und nun möchte ich die connection irgendwie übergeben an die statments klasse dass die damit arbeiten kann.
kann mir da jemand helfen??
vielen dank marco
Code:
public class DatabaseConnection {
private Connection connection = null;
public DatabaseConnection(String driver, String url, String user, String password) throws SQLException, ClassNotFoundException {
openConnection(driver, url, user, password);
}
/**
* Disconnect from the SCM database.
* @throws SQLException
*/
public void disconnect() throws SQLException {
closeConnection();
}
private void openConnection(String driver, String jdbcURL, String user, String password) throws SQLException, ClassNotFoundException {
if(connection != null) {
closeConnection();
}
Class.forName(driver);
connection = DriverManager.getConnection(jdbcURL, user, password);
connection.setAutoCommit(false);
}
private void closeConnection() throws SQLException {
if(connection != null) {
connection.rollback();
connection.close();
}
}
}
die statments so:
Code:
public class V3CRPRUSStatements {
Connection connection = null;
private PreparedStatement userInsert = null;
private PreparedStatement userDelete = null;
private PreparedStatement userupdate = null;
private PreparedStatement getLastName = null;
private PreparedStatement getFirstName = null;
private PreparedStatement getEmailAdress = null;
public V3CRPRUSStatements() {
}
public void deleteUser(UserDescription user) throws SQLException {
if (userDelete == null) {
userDelete = connection
.prepareStatement("delete from v3crprus where "
+ "USER_ID=?");
}
userDelete.setString(1, user.getUserId());
userDelete.executeUpdate();
}
public void insertUser(UserDescription user) throws SQLException {
if (userInsert == null) {
userInsert = connection.prepareStatement("insert into v3crprus("
+ "USER_ID, LAST_NAME, FIRST_NAME, EMAIL_ADDRESS"
+ ") values( ?, ?, ?, ?) ");
}
userInsert.setString(1, user.getUserId());
userInsert.setString(2, user.getLastName());
userInsert.setString(3, user.getFirstName());
userInsert.setString(4, user.getEMailAdress());
userInsert.executeUpdate();
}
public void updateUser(UserDescription user) throws SQLException {
if (userupdate == null) {
userupdate = connection
.prepareStatement("update v3crprus set LAST_NAME=?, FIRST_NAME=?, EMAIL_ADDRESS=?"
+ "where USER_ID=?");
}
userupdate.setString(4, user.getUserId());
userupdate.setString(1, user.getLastName());
userupdate.setString(2, user.getFirstName());
userupdate.setString(3, user.getEMailAdress());
userupdate.executeUpdate();
}
public String getFirstName(String userId) throws SQLException {
String returnValue = null;
if (getFirstName == null) {
getFirstName = connection
.prepareStatement("select FIRST_NAME from v3crprus where USER_ID=?");
getFirstName.setString(1, userId);
ResultSet result = getFirstName.executeQuery();
if (result != null) {
if (result.next()) {
returnValue = result.getString("FIRST_NAME");
}
}
}
return returnValue;
}
public String getEMailAdress(String userId) throws SQLException {
String returnValue = null;
if (getEmailAdress == null) {
getEmailAdress = connection
.prepareStatement("select EMAIL_ADDRESS from v3crprus where USER_ID=?");
getEmailAdress.setString(1, userId);
ResultSet result = getEmailAdress.executeQuery();
if (result != null) {
if (result.next()) {
returnValue = result.getString("EMAIL_ADDRESS");
}
}
}
return returnValue;
}
public String getLastName(String userId) throws SQLException {
String returnValue = null;
if (getLastName == null) {
getLastName = connection
.prepareStatement("select LAST_NAME from v3crprus where USER_ID=?");
getLastName.setString(1, userId);
ResultSet result = getLastName.executeQuery();
if (result != null) {
if (result.next()) {
returnValue = result.getString("LAST_NAME");
}
}
}
return returnValue;
}
und nun möchte ich die connection irgendwie übergeben an die statments klasse dass die damit arbeiten kann.
kann mir da jemand helfen??
vielen dank marco