package cart.dbUtils;
import java.sql.Connection;
import java.sql.DriverManager;
//DBConnection - manages one connection to the DataBase
//--->could be moved to be inner class of DBCOnnectionPool
//--->could keep track of things about itself,
//--->such as when last used and by what
//author - Lawrence Truett - FluffyCat.com
//date - May 28, 2003 - San Diego, CA
public class DBConnection {
private Connection connection = null;
//the unique prefix that many web hosts generate
private static String dbPrefix = "xxxx";
public DBConnection() {
//register the driver
try {
// The newInstance() call is a work around for some
// broken Java implementations -- from the MySQL
// documentation
//Class.forName("com.mysql.jdbc.Driver").newInstance();
//using the mmMySQL driver
Class.forName("com.mysql.jdbc.Driver");
//Class.forName("org.gjt.mm.mysql.Driver");
} catch (Exception ex) {
System.out.println("Exception getting driver");
System.out.println("Exception: " + ex);
}
}
public Connection getConnection() {
if (null == connection) {connect();}
return connection;
}
private void connect() {
try {
//with MySQL users are listed my.ini,
//and you must do grant(s) for the users
//String dbName = dbPrefix + "cart";
//String url = "jdbc:mysql://localhost/" + dbName;
//String username = dbPrefix + "xx";
//String password = "xx";
//connection = DriverManager.getConnection(url,
// username, password);
connection = DriverManager.getConnection
("jdbc:mysql://localhost/xx_xx?user=xx&password=xx");
} catch (java.sql.SQLException ex) {
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
}
private void disconnect() {
try {
connection.close();
} catch (java.sql.SQLException sqlException) {
System.out.println("The following error occured in"+
"connection to the database: " + sqlException);
connection = null;
}
}
//because many hosts add a prefix to all
//databases created on them
public static String getDBPrefix() {
return dbPrefix;
}
}