MySQL database basic intructions w/ JavaDoc (Spanish)

import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; /** * Clase SQL - Clase que crea una nueva conexión a la base de datos. * * @author Urko Pineda */ public class SQL { Connection con = null; String url = null; int port = 0; String username = null; String password = null; String dbName = null; /** * Constructor de SQL - Guarda todos los parámetros necesarios para la conexión en un nuevo objeto. * * @param url * @param port * @param username * @param password * @param dbName */ public SQL(String url, int port, String username, String password, String dbName) { this.url = url; this.port = port; this.username = username; this.password = password; this.dbName = dbName; } /** * Devuelve el estado de la conexión a la base de datos. * * @return * @throws SQLException */ public boolean getDataBaseStatus() throws SQLException { if (con == null) { return false; } else if (con.isClosed()) { return false; } else { return true; } } /** * Abre la base de datos con los datos obtenidos de las variables del contructor. * * @throws SQLException * @throws ClassNotFoundException */ public void openDataBase() throws SQLException, ClassNotFoundException { String generalURL = null; Class.forName("com.mysql.jdbc.Driver"); if (dbName == null) generalURL = "jdbc:mysql://"+url+":"+port+"/"+dbName; else generalURL = "jdbc:mysql://"+url+":"+port; con = DriverManager.getConnection(generalURL, username, password); } /** * Cierra la base de datos. * * @throws SQLException */ public void closeDataBase() throws SQLException { con.close(); } /** * Selecciona una base de datos en la conexión. * * @param dbName * @throws SQLException */ public void useDataBase(String dbName) throws SQLException { Statement stmt = con.createStatement(); stmt.execute("USE "+dbName); } /** * Devuelve el número de columnas de una tabla. * * @param tbName * @return * @throws SQLException */ public int getNumberColumns(String tbName) throws SQLException { PreparedStatement prepStmt = con.prepareStatement("SELECT * FROM "+tbName); ResultSet rs = prepStmt.executeQuery(); ResultSetMetaData rsmd = rs.getMetaData(); return rsmd.getColumnCount(); } /** * Devuelve un ArrayList con las columnas de una tabla ordenadas. * * @param tbName * @return * @throws SQLException */ public ArrayList<String> getColumnsNames(String tbName) throws SQLException { ArrayList<String> columnsNames = new ArrayList<>(); PreparedStatement prepStmt = con.prepareStatement("SELECT * FROM "+tbName); ResultSet rs = prepStmt.executeQuery(); ResultSetMetaData rsmd = rs.getMetaData(); for (int i = 0; i < getNumberColumns(tbName); i++) { columnsNames.add(rsmd.getColumnName(i)); } return columnsNames; } /** * Devuelve el número de datos obtenidos de un ResultSet. * * @param tbName * @param rs * @return * @throws SQLException */ public int getNumberRows(String tbName, ResultSet rs) throws SQLException { rs.last(); return rs.getRow(); } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.