Database Wrapper Questions
Just a quicky, currently constructing a project for university and come up against a bit of a hurdle. I have a class called DatabaseConnect, now this connects to a MYSQL database, and has several methods, such as update etc... The problem comes when I call any of these methods from in another class, the class itself is public and i realise how to call a method from another class using:
Code:
DatabaseConnect.DatabaseConnect;
I could always extends the login class, which would allow me to access all these, but has anyone a better idea?
P.s. if relevant here is the full source code:
Code:
import java.sql.*;
/**
*
* @author g9015395
*/
public class DatabaseConnect
{
private Connection mysqlConnection;
private Statement mysqlStatement;
private ResultSet queryResultSet;
public DatabaseConnect(String username, String password) throws SQLException
{
try
{
Class.forName("com.mysql.jdbc.Driver");
mysqlConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/footballLeague",
username, password);
mysqlStatement = mysqlConnection.createStatement();
} catch(ClassNotFoundException e)
{
e.printStackTrace();
}
}
public String hasDatabaseConnection() throws SQLException
{
String databaseStatus;
databaseStatus = "closed";
if(mysqlConnection.isClosed()== false)
{
databaseStatus = "Open";
return databaseStatus;
}
else
{
return databaseStatus;
}
}
public void getDatabaseInfomation() throws SQLException
{
DatabaseMetaData mysqlDatabaseMetaData = mysqlConnection.getMetaData();
System.out.println("Database: "+ mysqlDatabaseMetaData.getDatabaseProductVersion());
System.out.println("Driver: "+ mysqlDatabaseMetaData.getDriverVersion());
System.out.println("URL: "+mysqlDatabaseMetaData.getURL()+ "Username: "+mysqlDatabaseMetaData.getUserName());
}
public void runSelectQuery(String mysqlSelectUserStatement) throws SQLException
{
if(mysqlSelectUserStatement.isEmpty()==false)
{
queryResultSet = mysqlStatement.executeQuery(mysqlSelectUserStatement);
}
else
{
System.out.println("An error has occured, no SQL given");
}
}
public void runDeleteQuery(String mysqlUserDeleteUserStatement) throws SQLException
{
if(mysqlUserDeleteUserStatement.isEmpty()==false)
{
mysqlStatement.executeUpdate(mysqlUserDeleteUserStatement);
}
else
{
System.out.println("An error has occured, no SQL given");
}
}
public void runUpdateQuery(String mysqlUserUpdateUserStatement) throws SQLException
{
if(mysqlUserUpdateUserStatement.isEmpty()==false)
{
mysqlStatement.executeUpdate(mysqlUserUpdateUserStatement);
}
else
{
System.out.println("An error has occured, no SQL given");
}
}
public void runInsertQuery(String mysqlUserInsertUserStatement) throws SQLException
{
if(mysqlUserInsertUserStatement.isEmpty()==false)
{
mysqlStatement.executeUpdate(mysqlUserInsertUserStatement);
}
else
{
System.out.println("An error has occured, no SQL given");
}
}
public void closeDatabaseConnection() throws SQLException
{
mysqlStatement.close();
mysqlConnection.close();
}
}