Sunday, August 10, 2008

My Model Class

package com.extreamtech.bean;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NamingException;

import javax.sql.DataSource;

import com.extreamtech.exception.WCMException;

public abstract class MasterBean {

private static final String CONTEXT_NAME = "java:/comp/env";

private static final String RESOURCE_NAME = "jdbc/wcmsdb";

protected long id;

protected final String tablename = "";

protected final String fieldnames = "";

protected String selectquery = "";

protected String savequery = "";

protected String deletequery = "";

protected String updatequery = "";

protected ResultSet resultset;

public abstract void prepareUpdate() throws WCMException;

public MasterBean(){}

public MasterBean(long id){}

private Connection conn;

protected PreparedStatement ps;

protected void getData() throws WCMException {

openConnection();

prepareStatement(selectquery);

executeQuery();

}

public void save() throws WCMException {

try {

openConnection();

prepareStatement(savequery);

executeUpdate();

} finally {

closeConnection();

}

}

public void delete() throws WCMException {

try {

openConnection();

prepareStatement(deletequery);

executeUpdate();

} finally {

closeConnection();

}

}

public void update() throws WCMException {

try {

openConnection();

prepareStatement(updatequery);

prepareUpdate();

executeUpdate();

} finally {

closeConnection();

}

}

protected void executeUpdate() throws WCMException {

try {

ps.executeUpdate();

} catch (SQLException e) {

e.printStackTrace();

throw new WCMException(e);

}

}

protected void executeQuery() throws WCMException {

try {

resultset = ps.executeQuery();

} catch (SQLException e) {

e.printStackTrace();

throw new WCMException(e);

}

}

protected void prepareStatement(String sql) throws WCMException {

try {

ps = conn.prepareStatement(sql);

} catch (SQLException e) {

e.printStackTrace();

throw new WCMException(e);

}

}

protected void openConnection() throws WCMException {

try {

Context initContext = new InitialContext();

Context envContext = (Context) initContext.lookup(CONTEXT_NAME);

DataSource ds = (DataSource) envContext.lookup(RESOURCE_NAME);

conn = ds.getConnection();

} catch (NamingException e) {

e.printStackTrace();

throw new WCMException(e);

} catch (SQLException e) {

e.printStackTrace();

throw new WCMException(e);

}

}

protected void closeConnection() throws WCMException {

try {

if (ps != null) {

ps.close();

ps = null;

}

if (conn != null) {

conn.close();

conn = null;

}

} catch (SQLException e) {

e.printStackTrace();

throw new WCMException(e);

}

}

protected String questionMarks() {

String temp = "";

int fieldlength = fieldnames.split(",").length;

for(int i=0; i

temp = temp + "?";

if(i

temp = temp + ", ";

}

}

return temp;

}

protected String updateQuestionMarks() {

String temp = "";

int fieldlength = fieldnames.split(",").length;

for(int i=0; i

temp = temp + fieldnames.split(",")[i] + " = ?";

if(i

temp = temp + ", ";

}

}

return temp;

}

protected void initeValue() {

selectquery = "SELECT " + fieldnames + " FROM " + tablename

+ " WHERE id = " + id;

savequery = "INSERT INTO " + tablename + "(" + fieldnames + ") "

+ "VALUES (" + questionMarks() + ")";

deletequery = "DELETE FROM " + tablename + " WHERE id = " + id;

updatequery = "UPDATE " + tablename + " SET " + updateQuestionMarks() + " WHERE id = "

+ id;

}

public long getId() {

return id;

}

public void setId(long id) {

this.id = id;

}

}

1 comment:

Coding Kitty said...

package com.extreamtech.bean;

import java.sql.SQLException;
import java.util.Date;

import com.extreamtech.exception.WCMException;

public class ServiceCategory extends MasterBean {
private String name;
protected final String fieldnames = "name";
protected final String tablename = "servicecategory";

public ServiceCategory() {
id = new Date().getTime();
initeValue();
}

public ServiceCategory(long id) throws WCMException {
this.id = id;
initeValue();
getData();
try {
if (resultset.next()) {
this.id = resultset.getLong("id");
this.name = resultset.getString("name");
}
} catch (SQLException e) {
throw new WCMException(e);
} finally {
closeConnection();
}
}

public ServiceCategory(long id, String name) {
this.id = id;
this.name = name;
initeValue();
}

@Override
public void prepareUpdate() throws WCMException {
try {
ps.setString(1, name);
} catch (SQLException e) {
throw new WCMException(e);
}
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}