package Forum; import java.sql.*; import org.apache.log4j.Logger; public class DBaseConnection { private static Logger log = Logger.getLogger( DBaseConnection.class ); public static String connectionString = "jdbc:mysql://localhost:3306/forum"; public static String username = "sticky"; public static String password = "wicket"; public static Connection getConnection() throws Exception { Connection con = null; try{ Class.forName("org.gjt.mm.mysql.Driver").newInstance(); con = DriverManager.getConnection(connectionString, username, password); log.info("Logging into " + connectionString + " with U/P " + username + "/" + password); }catch(Exception e){ log.error("Error in DBaseConnection.getConnection(): " + e); } return con; } /* There is no executeUpdate() because using a PreparedStatement instead takes care of * any text formatting issues. Ideally all queries should use a Prepared Statement. */ public static PreparedStatement getPreparedStatement(String insertSQL) throws Exception { PreparedStatement pstatement = null; pstatement = getConnection().prepareStatement(insertSQL); return pstatement; } public static ResultSet executeQuery(String queryString) throws Exception { Statement stmt = null; ResultSet rs = null; stmt = getConnection().createStatement(); rs = stmt.executeQuery(queryString); return rs; } }