Código:
Ejemplo de uso: Este es un pequeño ejemplo de un login utilizando dicha clase.
import java.sql.Statement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class MySQLConnection { String userName = "root"; String password = "root"; String url = "jdbc:mysql://localhost/"; String urlDriver = "com.mysql.jdbc.Driver"; String database = "database"; Connection con; Statement st; ResultSet rs; /** * constructor vacio * @usage MySQLConnection connect = new MySQLConnection(); */ public MySQLConnection() { } /** * @return retorna la conexion a la base de datos * @use Statement st = connect.OpenConnection().createStatement(); */ public Connection OpenConnection() { con = null; try { Class.forName(urlDriver).newInstance(); con = DriverManager.getConnection(url + database, userName, password); System.out.println("connect"); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException e) { System.out.println(e.getMessage()); } return con; } /** * * @use connect.CloseConnection(connect.con) */ public void CloseConnection() { try { con.close(); System.out.println("close connection"); } catch (SQLException e) { System.out.println(e.getMessage()); } } /** * * @param con cierra la conexion a la base de datos * @use connect.CloseConnection(connect.con) */ public void CloseConnection(Connection con) { try { con.close(); System.out.println("close connection"); } catch (SQLException e) { System.out.println(e.getMessage()); } } /** * @param sql query para un select * @return rs retorno de la consulta efectuada caso contrario retorna null * @use */ public ResultSet executeQuery(String sql) { try { st = OpenConnection().createStatement(); rs = st.executeQuery(sql); return rs; } catch (Exception e) { System.out.println(e.getMessage()); } return null; } /** * * @param sql query para un select * @return rs retorno de la consulta efectuada caso contrario retorna null */ public boolean executeUpdate(String sql) { try { st = OpenConnection().createStatement(); rs = st.executeQuery(sql); return true; } catch (Exception e) { System.out.println(e.getMessage()); } return false; } /** * * @param pstm query para un select * @return rs retorno de la consulta efectuada caso contrario retorna null */ public ResultSet PreparedStatementExecuteQuery(PreparedStatement pstm) { try { rs = pstm.executeQuery(); return rs; } catch (Exception e) { System.out.println(e.getMessage()); } return null; } /** * * @param pstm query para un select * @return rs retorno de la consulta efectuada caso contrario retorna null */ public boolean PreparedStatementExecuteUpdate(PreparedStatement pstm) { try { pstm.executeUpdate(); return true; } catch (Exception e) { System.out.println(e.getMessage()); } return false; } /** * * @param sql query para crear un PreparedStatement para agregar los datos * @return rs retorno de la consulta efectuada caso contrario retorna null */ public PreparedStatement create(String sql){ try { PreparedStatement pstm=OpenConnection().prepareStatement(sql); return pstm; } catch (SQLException ex) { System.out.println(e.getMessage()); } return null; } }
import java.io.UnsupportedEncodingException; import java.security.NoSuchAlgorithmException; import java.sql.PreparedStatement; import java.sql.Statement; import java.sql.ResultSet; import java.sql.SQLException; public class Operations { Statement st; ResultSet rs; MySQLConnection mysqlconnect; PreparedStatement pstm; public Operations() { mysqlconnect = new MySQLConnection(); } public void insertUser(String user, String password, int accessLevel) { try { pstm = mysqlconnect.create("insert into usuario values(?,?,?)"); pstm.setString(1, user); pstm.setString(2,password); pstm.setInt(3, accessLevel); mysqlconnect.PreparedStatementExecuteUpdate(pstm); mysqlconnect.CloseConnection(mysqlconnect.con); } catch (SQLException | NoSuchAlgorithmException | UnsupportedEncodingException ex) { System.out.println("Error to insert: " + ex.getMessage()); } } public String getPassword(String user) { String pass = null; try { pstm = mysqlconnect.create("select password from usuario where user=?"); pstm.setString(1, user); rs = mysqlconnect.PreparedStatementExecuteQuery(pstm); rs.next(); pass = rs.getString(1); rs.close(); mysqlconnect.CloseConnection(); } catch (SQLException ex) { System.out.println("user not found: "+ex.getMessage()); } return pass; } }