Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package conexion;
- import com.mysql.jdbc.*;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class conexion {
- Connection co;
- Statement stm;
- //jdbc:mysql://localhost:3306/java?zeroDateTimeBehavior=convertToNull [root on Default schema]
- public static Connection iniciarConnection(){
- try {
- Class.forName("com.mysql.jdbc.Driver");
- Connection co= DriverManager.getConnection("jdbc:mysql//localhost:3306/java", "root", "");
- Statement stm = co.createStatement();
- System.out.println("Conectado correctamente a la Base de Datos");
- return co;
- } catch (ClassNotFoundException e) {
- System.out.println("Clase no encontrada: "+e);
- } catch (SQLException e) {
- System.out.println("Error de conexion: "+e);
- } catch (Exception e) {
- System.out.println("Error desconocido: "+e);
- }
- return null;
- }
- }
- Connection co= DriverManager.getConnection("jdbc:mysql://localhost:3306/java", "root", "");
- // 2.- Creo un objeto conexión.
- Connection conexion = DriverManager.getConnection
- ("jcbc:mysql://localhost:3306/curso_php_2",
- "root", "");
- // 3.- Creo un objeto Statement a partir del objeto Connection
- Statement statement = conexion.createStatement();
- // 4.- Ejecuto sentencia SQL
- ResultSet resultado = statement.executeQuery(query);
- while (resultado.next())
- {
- System.out.println(resultado.getString("USUARIO") + " " +
- resultado.getString("CONTRASENIA") + " " +
- resultado.getString("DIRECCION"));
- }
- // Libero el resultset
- resultado.close();
- // Cierro la conexion conexion.close();
- } catch (ClassNotFoundException e)
- {
- JOptionPane.showMessageDialog (null,
- "Imposible cargar el driver MySQL", "Error",
- JOptionPane.ERROR_MESSAGE);
- }
- catch (SQLException e)
- {
- String mensajeError = e.getMessage();
- JOptionPane.showMessageDialog (null, mensajeError, "Error",
- JOptionPane.ERROR_MESSAGE);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement