Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private Connection co;
- public Conexion() {
- this.co = null;
- }
- public Connection get_conection() {
- return this.co;
- }
- public boolean conectar() throws ClassNotFoundException, SQLException {
- /*
- Connection c = null;
- Class.forName("com.mysql.jdbc.Driver");
- //el metodo getConnection() hay que adaptarlo para conectarlo a mi base de daots de hostinguer
- c = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306//");
- Statement stm = c.createStatement();
- System.out.println("Conectado correctamente a la Base de Datos de 'League Of Leguends'");
- this.co = c;
- */
- boolean conectado = false;
- Connection c = null;
- Class.forName("com.mysql.jdbc.Driver");
- c = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/LEAGUE_OF_LEGENDS", "miusuario", "micontraseña");
- Statement stm = c.createStatement();
- conectado = true;
- System.out.println("Conectado correctamente a la Base de datos : LEAGUE OF LEGUENDS");
- if (conectado == true) {
- return true;
- } else {
- return false;
- }
- }
- public int contar_num_personajes() throws SQLException {
- int n = 0;
- Connection dbConnection = get_conection();
- Statement stm = dbConnection.createStatement();
- // almaceno resultado de consulta en ResultSet
- ResultSet rs = stm.executeQuery("SELECT count(*) FROM personajes");
- // chequeo que el result set no sea vacío, moviendo el cursor a la
- // primer fila. (El cursor inicia antes de la primer fila)
- if (rs.next()) {
- //Si hay resultados obtengo el valor.
- n = rs.getInt(1);
- }
- // libero recursos
- stm.close();
- dbConnection.close();
- return n;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement