Shaun
July 3rd, 2010, 18:21
This is an example of a class to hold and communicate a connection to a MySQL database using the JDBC driver.
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* Interfaces with Statement to be able to communicate with a MySQL database
* using the JDBC driver
*
* @author Shaun
*/
public class MySQLConnection
{
// driver we are using
private static final String DRIVER_NAME = "jdbc:mysql";
private String url; // the database
private String user; // username to connect with
private String pass; // password to connect with
private Statement statement; // statement for db communication
/**
* Create MySQLConnection
*
* @param host
* host of the db
* @param database
* which db we are concerned with
* @param user
* username to connect with
* @param pass
* password to connect with
* @throws ClassNotFoundException
* thrown when jdbc driver not found
* @throws SQLException
* thrown if connection/statement fails
*/
public MySQLConnection(String host, String database, String user, String pass) throws ClassNotFoundException, SQLException
{
url = DRIVER_NAME + "://" + host + "/" + database;
this.user = user;
this.pass = pass;
connect();
}
/**
* Connect to the db/create statement
*
* @throws ClassNotFoundException
* thrown when jdbc driver not found
* @throws SQLException
* thrown if connection/statement fails
*/
public void connect() throws ClassNotFoundException, SQLException
{
Class.forName("com.mysql.jdbc.Driver"); // ensure driver is working and found
statement = DriverManager.getConnection(url, user, pass).createStatement();
}
/**
* Execute a query of the db
*
* @param query
* query to be executed
* @return result(s) of the query
* @throws SQLException
* thrown if a database access error occurs
*/
public ResultSet query(String query) throws SQLException
{
return statement.executeQuery(query);
}
/**
* Execute an update to the db(INSERT, UDPATE, DELETE)
*
* @param update
* update to be executed
* @return row count for update, or 0 if nothing
* @throws SQLException
* thrown if a database access error occurs
*/
public int update(String update) throws SQLException
{
return statement.executeUpdate(update);
}
/**
* Closes the statement
*
* @throws SQLException
* thrown if a database access error occurs
*/
public void close() throws SQLException
{
statement.close();
}
}
You can find the driver here: Only the registered members can see the link.
If you are using an IDE, you can just find your project libraries, and add the mysql-connector-java jar to it.
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* Interfaces with Statement to be able to communicate with a MySQL database
* using the JDBC driver
*
* @author Shaun
*/
public class MySQLConnection
{
// driver we are using
private static final String DRIVER_NAME = "jdbc:mysql";
private String url; // the database
private String user; // username to connect with
private String pass; // password to connect with
private Statement statement; // statement for db communication
/**
* Create MySQLConnection
*
* @param host
* host of the db
* @param database
* which db we are concerned with
* @param user
* username to connect with
* @param pass
* password to connect with
* @throws ClassNotFoundException
* thrown when jdbc driver not found
* @throws SQLException
* thrown if connection/statement fails
*/
public MySQLConnection(String host, String database, String user, String pass) throws ClassNotFoundException, SQLException
{
url = DRIVER_NAME + "://" + host + "/" + database;
this.user = user;
this.pass = pass;
connect();
}
/**
* Connect to the db/create statement
*
* @throws ClassNotFoundException
* thrown when jdbc driver not found
* @throws SQLException
* thrown if connection/statement fails
*/
public void connect() throws ClassNotFoundException, SQLException
{
Class.forName("com.mysql.jdbc.Driver"); // ensure driver is working and found
statement = DriverManager.getConnection(url, user, pass).createStatement();
}
/**
* Execute a query of the db
*
* @param query
* query to be executed
* @return result(s) of the query
* @throws SQLException
* thrown if a database access error occurs
*/
public ResultSet query(String query) throws SQLException
{
return statement.executeQuery(query);
}
/**
* Execute an update to the db(INSERT, UDPATE, DELETE)
*
* @param update
* update to be executed
* @return row count for update, or 0 if nothing
* @throws SQLException
* thrown if a database access error occurs
*/
public int update(String update) throws SQLException
{
return statement.executeUpdate(update);
}
/**
* Closes the statement
*
* @throws SQLException
* thrown if a database access error occurs
*/
public void close() throws SQLException
{
statement.close();
}
}
You can find the driver here: Only the registered members can see the link.
If you are using an IDE, you can just find your project libraries, and add the mysql-connector-java jar to it.