Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.oddlyoko.lom.mysql;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- /**
- * This class allow you to interact with MySQL<br />
- *
- * @author 0ddlyoko<br />
- * <br />
- * Created: 12 oct. 2016 (19:46:01);<br />
- * Last edit: 12 oct. 2016 (19:46:01);<br />
- */
- public class MySQL {
- private String host = ""; // Host name
- private String dbname = ""; // Database name
- private String login = ""; // Login
- private String passwd = ""; // Password
- private int port = 3306; // Port (default is 3306)
- private String url; // Url
- private Connection connection = null; // Connection class
- private Statement statement = null; // Statement class
- private ResultSet resultat = null; // ResultSet class
- private String urlPath = "jdbc:mysql://" + host + ":" + port; // Real url
- // path
- /**
- * public void setHost: This method will set the host.
- *
- * @param host
- * The host of mySQL.
- */
- public void setHost(String host) {
- this.host = host;
- }
- /**
- * public void setDBName: This method will set the database name.
- *
- * @param name
- * The name of the database.
- */
- public void setDBName(String name) {
- this.dbname = name;
- }
- /**
- * public void setLogin: This method will set the login.
- *
- * @param login
- * The login.
- */
- public void setLogin(String login) {
- this.login = login;
- }
- /**
- * public void setPassword: This method will set the password.
- *
- * @param passwd
- * The password.
- */
- public void setPassword(String passwd) {
- this.passwd = passwd;
- }
- /**
- * public void setPort: This method will set the port.
- *
- * @param port
- * The port.
- */
- public void setPort(int port) {
- this.port = port;
- }
- /**
- * public void init: This method will init this class.
- *
- * @throws ClassNotFoundException
- * @throws SQLException
- */
- public void init() throws ClassNotFoundException {
- urlPath = "jdbc:mysql://" + host + ":" + port;
- this.url = urlPath + "/" + this.dbname;
- connectFirstTime();
- }
- /**
- * public void connectToDatabase: This method will init first connection.
- *
- * @throws ClassNotFoundException
- * @throws SQLException
- */
- private void connectFirstTime() throws ClassNotFoundException {
- Class.forName("com.mysql.jdbc.Driver");
- }
- /**
- * public void connect: This method will connect you to mySQL.
- *
- * @throws SQLException
- */
- public void connect() throws SQLException {
- connection = DriverManager.getConnection(url, login, passwd);
- statement = connection.createStatement();
- }
- /**
- * public void disconnect: This method will disconnect you from mySQL.
- *
- * @throws SQLException
- */
- public void disconnect() throws SQLException {
- if (resultat != null)
- resultat.close();
- if (statement != null)
- statement.close();
- if (connection != null)
- connection.close();
- }
- /**
- * public Results select: This method will select objects in database, and
- * put it in Results.
- *
- * @param name
- * The database name.
- * @throws SQLException
- */
- public Results select(String msg) throws SQLException {
- return Results.transform(statement.executeQuery(msg));
- }
- /**
- * public int update: This method will update the code below.
- *
- * @param msg
- * Message to execute.
- *
- * @return The integer value
- * @throws SQLException
- */
- public int update(String msg) throws SQLException {
- return statement.executeUpdate(msg);
- }
- /**
- * public void execute: This method will execute the code below.
- *
- * @param msg
- * Message to execute.
- * @throws SQLException
- */
- public void execute(String msg) throws SQLException {
- statement.execute(msg);
- }
- /**
- * public void createDB: This method will create the specifical database.
- *
- * @param name
- * The database name.
- * @throws SQLException
- */
- public void createDB(String name) throws SQLException {
- url = urlPath;
- this.dbname = name;
- execute("CREATE DATABASE IF NOT EXISTS " + name);
- disconnect();
- url = urlPath + "/" + this.dbname;
- connect();
- }
- /**
- * public void connectToDatabase: This method will connect you to the
- * special database.
- *
- * @param name
- * The database name.
- * @throws SQLException
- */
- public void connectToDatabase(String name) throws SQLException {
- createDB(name);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement